【问题标题】:How can I convert UTC to a DateTime?如何将 UTC 转换为 DateTime?
【发布时间】:2011-05-24 15:40:49
【问题描述】:

我有以下日期:

2011-05-24T11:40:41Z

如何将其转换为 Joda DateTime?

编辑: 这是我无法编译的代码:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 
                        Date date = df.parse(aMessage.getString("updated_at"));
                        org.joda.time.DateTime dt = new DateTime(date);

错误:

The method parse(String) is undefined for the type DateFormat   
Type mismatch: cannot convert from SimpleDateFormat to DateFormat

【问题讨论】:

    标签: java datetime date utc


    【解决方案1】:

    来自 Joda 的user guide

    所有打印和解析都是使用DateTimeFormatter 对象执行的。

    在这种情况下,您应该可以直接使用 ISODateTimeFormat 工厂:

    String inputDateString = "2011-05-24T11:40:41Z";
    
    DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
    DateTime result = fmt.parseDateTime(inputDateString);
    

    【讨论】:

      【解决方案2】:

      使用DatFormat。它有一个您可以使用的parse() 方法。

          DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
          df.parse("2011-05-24T11:40:41Z");
          org.joda.time.DateTime dt = new DateTime(date);
      

      【讨论】:

      • 那不编译。不能将 SimpleDateFormat 分配给 df。并且 df.parse 不能做字符串。
      • 为什么不呢?我在这里粘贴我编译的代码: public static void main(String[] args) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");日期日期 = df.parse("2011-05-24T11:40:41Z"); org.joda.time.DateTime dt = new DateTime(date); }
      • 确保它是 java.text.DateFormat
      猜你喜欢
      • 2016-06-19
      • 2012-12-05
      • 2011-09-29
      • 2015-04-29
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多