java - 客户端GWT中String与Date的转换

标签 java gwt type-conversion

使用 SimpleDateFormat 是在 Java 中进行字符串 <-> 日期转换的标准方法。

但是 GWT 中不存在此类 JRE Emulation library所以我们不能在 GWT 客户端代码中使用它。

那么,进行这种转换的标准/推荐方式是什么?

最佳答案

Date Time Format 中给出的示例,这可能会有所帮助。

public class DateTimeFormatExample implements EntryPoint {

  public void onModuleLoad() {
    Date today = new Date();

    // prints Tue Dec 18 12:01:26 GMT-500 2007 in the default locale.
    GWT.log(today.toString(), null);

    // prints 12/18/07 in the default locale
    GWT.log(DateTimeFormat.getShortDateFormat().format(today), null);

    // prints December 18, 2007 in the default locale
    GWT.log(DateTimeFormat.getLongDateFormat().format(today), null);

    // prints 12:01 PM in the default locale
    GWT.log(DateTimeFormat.getShortTimeFormat().format(today), null);

    // prints 12:01:26 PM GMT-05:00 in the default locale
    GWT.log(DateTimeFormat.getLongTimeFormat().format(today), null);

    // prints Dec 18, 2007 12:01:26 PM in the default locale
    GWT.log(DateTimeFormat.getMediumDateTimeFormat().format(today), null);

    // A custom date format
    DateTimeFormat fmt = DateTimeFormat.getFormat("EEEE, MMMM dd, yyyy");
    // prints Monday, December 17, 2007 in the default locale
    GWT.log(fmt.format(today), null);
  }
}

关于java - 客户端GWT中String与Date的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10472756/

相关文章:

java.lang.NumberFormatException : For input string error while converting hex to decimal 异常

java - ANDROID数据库找不到列_id,即使它是KEY_ID=_id

java - 模式和更多用于干净简单的 gui 代码

java - GWT JSNI - 调用特定对象的java方法

c - C中什么时候发生类型转换

c# - 为什么 ? : cause a conversion error while if-else does not?

c++ - 使用 "operator T*()"而不是 "T* operator->()"进行成员访问

java - 使用 setText 清除 TextField 的内容在 AWT 中不起作用

java - 排序 ListView 的最佳方式

java - Flex 和 Java GWT 集成