【问题标题】:How to convert String of datetime to date using GWT?如何使用 GWT 将日期时间字符串转换为日期?
【发布时间】:2012-07-05 10:41:58
【问题描述】:

在 mysql 中,我有一个 datetime 类型的字段 time_entered(示例数据:2012-06-20 16:00:47)。我还有一个方法 getTimeEntered(),它以字符串形式返回值。我想使用 GWT 中的 DateTimeFormat 以 2012-06-20 这种格式显示日期。

这是我的代码:

String date = aprHeaderDW.getTimeEntered();
DateTimeFormat fmt = DateTimeFormat.getFormat("MM-dd-yyyy");
dateEntered.setText("" + fmt.format(date));

问题是,格式方法不接受字符串形式的参数。因此,如果只有一种方法可以将日期从 String 类型转换为 Date 类型,它可能会起作用。我尝试了类型转换,但没有成功。

【问题讨论】:

    标签: java gwt datetime date


    【解决方案1】:

    您应该可以只使用DateTimeFormat

    Date date = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
    String dateString = DateTimeFormat.getFormat("yyyy-MM-dd").format(date);
    

    否则有一个light-weight version of SimpleDateFormat支持这种模式。

    Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
    

    【讨论】:

    • 我会试试你提供的链接。您的示例可能适用于 JSE,我尝试过,但需要用 Try/Catch 块将其括起来,然后导入 ParseException。我猜 GWT 不支持该异常。我不确定,所以我会试试你的链接。感谢您的快速回答。
    • 我添加了另一种仅使用 DateTimeFormat 的方法。
    • 非常感谢@Keppil,我将您的第二个建议作为我解决方案的一部分。
    • @Keppil 通过使用 GWT 的 DateTimeFormat.format("pattern") 可以获得所需格式的日期作为字符串结果。要将 tihs 字符串结果更改为最新,我使用了相同格式的解析,但这次我没有得到想要的结果。请在下面找到输出 snap First Output : 2020-09-04, after used parse : Fri Sep 04 00:00:00.所以请在这里帮忙。谢谢!
    【解决方案2】:

    您好,有两种选择。

    第一个是因为它已经是一个字符串,你可以使用正则表达式来修改格式。

    第二个是使用 SimpleDateFormater,您可以将字符串解析为日期,然后再返回。 例如:

    public class DateMerge {
    
        public static void main(String arg[]) 
        {
            String out = dateConvert("2012-06-20 16:00:47");
            System.out.println(out);
        }
    
        public static String dateConvert (String inDate)
        {
            try {
             DateFormat formatter ; 
             Date date ; 
              formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              date = (Date)formatter.parse(inDate);
              formatter = new SimpleDateFormat("dd-MM-yyyy");
              String outDate = formatter.format(date);
              return outDate;
    
              } catch (ParseException e)
              {System.out.println("Exception :"+e);  }  
    
    
        return null;
        }
    }
    

    【讨论】:

      【解决方案3】:

      你可以这样使用。

          String date = "2012-06-20 16:00:47";
      
          SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
          String lDate=sf.format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date));
      
          System.out.println(lDate);
      

      输出:

      2012-06-20
      

      【讨论】:

      • 您不能在 GWT 中使用 SimpleDateFormat
      【解决方案4】:

      在尝试了很多次之后,我想出了一个基于@Keppil 并添加我自己的代码的解决方案。

      这是 Keppil 建议的将 String datetime 转换为 Date 类型的解决方案:

      Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
      

      ...但我的第二个要求是仅显示日期,例如 2012-06-20。即使我删除了 HH:mm:ss,它仍然显示像这样的时间 2012-06-20 00:00:00。

      这是我的最终解决方案:

      Date date = null;
      String d = rs.getString(SQL_CREATION_TIME); // assigns datetime value from mysql
      
      // parse String datetime to Date
      try {
      date = new SimpleDateFormat("yyyy-MM-dd").parse(d);
      System.out.println("time entered: "+  date);
      } catch (ParseException e) { e.printStackTrace(); }
      
      // format the Date object then assigns to String
      Format formatter;
      formatter = new SimpleDateFormat("yyyy-MM-dd");
      String s = formatter.format(date);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-31
        • 2019-04-12
        • 2011-09-08
        相关资源
        最近更新 更多