自分で作ったり提供したりするものは、まず自分自身で使ってみろろということです。自分じゃ使わないものなら人はいくらでも無責任にも無思考にもなれる。そういう投げやりな「サービス」やら「プロダクツ」なんて、だれだってイヤだ。自分が作り手と同時に利用者の立場になれば、ちゃんと使えるレベルのものを提供しようとします。

2010年4月13日火曜日

数字と日付の書式フォーマット

Google Web Toolkitの数値と日付の書式フォーマット機能を紹介します。

サンプル

Webtestsample.java
public String GetShowContent()
{
    GWT.log("helo GWT log.");

    StringBuilder content = new StringBuilder();
   
    NumberFormat fmt = NumberFormat.getDecimalFormat();
    double value = 12345.6789;
    String formatted = fmt.format(value);
    content.append("Formatted string is" + formatted + "<br/>");
   
    value = NumberFormat.getDecimalFormat().parse("12345.6789");
    content.append("Parsed value is" + value + "<br/>");
   
    value = 12345.6789;
    formatted = NumberFormat.getScientificFormat().format(value);
    content.append("Formatted string is" + formatted + "<br/>");
   
    value = 12345.6789;
    formatted = NumberFormat.getFormat("000000.000000").format(value);
    content.append("Formatted string is" + formatted + "<br/>");
   
    Date today = new Date();

    // prints Tue Dec 18 12:01:26 GMT-500 2007 in the default locale.
    content.append(today.toString() + "<br/>");

    content.append(DateTimeFormat.getFormat("yyyy年MM月dd日").format(today) + "<br/>");
   
    // prints 12/18/07 in the default locale
    content.append(DateTimeFormat.getShortDateFormat().format(today) + "<br/>");

    // prints December 18, 2007 in the default locale
    content.append(DateTimeFormat.getLongDateFormat().format(today) + "<br/>");

    // prints 12:01 PM in the default locale
    content.append(DateTimeFormat.getShortTimeFormat().format(today) + "<br/>");

    // prints 12:01:26 PM GMT-05:00 in the default locale
    content.append(DateTimeFormat.getLongTimeFormat().format(today) + "<br/>");

    // prints Dec 18, 2007 12:01:26 PM in the default locale
    content.append(DateTimeFormat.getMediumDateTimeFormat().format(today) + "<br/>");
   
    return content.toString();
}
一部のコードを省略します…

なお、DateTimeFormatクラスの仕様詳細をこちらにご参照ください。
http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/i18n/client/DateTimeFormat.html

0 件のコメント:

コメントを投稿

ホームページ