Java 日期轉字串範列

1.//目前時間
2.Date date = new Date();
3.//設定日期格式
4.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
5.//進行轉換
6.String dateString = sdf.format(date);
7.System.out.println(dateString);

Java字串轉日期範例
 
1.//欲轉換的日期字串
2.String dateString = "20010-03-02 20:25:58";
3.//設定日期格式
4.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
5.//進行轉換
6.Date date = sdf.parse(dateString);
7.System.out.println(date);

以上不支援 gwt, 若要在 gwt 字串日期互轉, 必須使用 

DateTimeFormat

原因為:
 
GWT Doesnt support this java APi. It's explicit in documentation pages 

GWT does not provide full emulation for the date and number formatting 
classes (java.text.DateFormat, java.text.DecimalFormat, 
java.text.NumberFormat, java.TimeFormat, et cetera). Instead, a subset 
of the functionality of the JRE classes is provided by 
com.google.gwt.i18n.client.NumberFormat and 
com.google.gwt.i18n.client.DateTimeFormat 

GWT provides the DateTimeFormat class to replace the functionality of 
the DateFormat and TimeFormat classes from the JRE.

詳細用法, 請參考 http://code.google.com/intl/fr/webtoolkit/doc/1.6/DevGuideCodingBasics.html#DevGuideDateAndNumberFormat 

參考資料: GWT官方文件 and http://cooking-java.blogspot.com/2010/03/java-string-to-date.html 



arrow
arrow
    全站熱搜

    Frank 發表在 痞客邦 留言(0) 人氣()