【问题标题】:Java Datetime with Timezone format具有时区格式的 Java 日期时间
【发布时间】:2013-04-04 22:16:04
【问题描述】:

我目前正在使用SimpleDateFormat 创建yyyy-MM-dd HH:mm:ss 格式的日期时间值。这是我的代码:

     Date date = new SimpleDateFormat("yy-M-d H:m:ss").parse(datetime);
     String Dtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

但我希望格式改为:

  2013-04-12T13:38:48+02:00

如何添加时区以获得所需的格式?

谢谢!

【问题讨论】:

标签: java datetime timezone


【解决方案1】:

有一种简单的方法。您可以定义格式,如: String Dtime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(date);

应该这样做。

【讨论】:

    【解决方案2】:

    如果您使用“z”或“Z”,那么您仍然无法确定您的时区会是什么样子。这取决于您的本地人和 JVM 版本,将选择哪个 ISO8601 版本。

    让时区始终采用“+02:00”格式的一种方法是请求它。使用以下日期格式:

    DateTimeFormatter formatter = 
        new DateTimeFormatterBuilder()
            .appendPattern("yyyy-MM-dd'T'HH:mm:ss")
            .appendOffset("+HH:MM","+00:00")
            .toFormatter();
    

    并始终将其与 ZonedDateTime 类型一起使用。它是线程安全的。

    【讨论】:

      【解决方案3】:

      看看the API documentation 的 SimpleDateFormat。在那里您可以看到,您可以使用字符“z”作为时区信息。

      示例:“yyyy.MM.dd G 'at' HH:mm:ss z”将为您提供“2001.07.04 AD at 12:08:56 PDT”

      要设置日期的时区,请查看this question and the answers

      【讨论】:

      • 谢谢!但 z 最后加上 +0200。有没有办法让“+02:00”?我需要时区中的“:”..
      • @diego10 - 仅供参考 - 带或不带 :,带或不带 T 都是可接受的 ISO8601 格式形式。因此,假设您希望这种格式与某些东西互换 - 您应该没问题。不过,如果你想要更好的日期处理,你可能想看看Joda Time
      【解决方案4】:

      我一直在寻找完全相同的答案。我必须向后兼容现有的日期格式,例如示例中的“2013-04-12T13:38:48+02:00”,我的另一个限制是我不能使用除Java 6 提供了什么。因此,在没有在这里找到解决方案之后,我想出了这个简单的转换技术,这很丑陋,但我发现我把它贴在这里以防有人需要一个快速而肮脏的解决方案。如果有人知道如何做得更好(有我提到的限制),请纠正我。

      因此,要生成所需格式的输出:

      private DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
      
      public GregorianCalendar stringToCalendar(String v) 
      {
          // 22 is an idx of last ':', you can replace with v.lastIndex(':') to be neat  
          String simpleV = v.substring(0, 22).concat(v.substring(23));   
      
          GregorianCalendar gc = (GregorianCalendar) df.getCalendar();
      
          gc.setTime(df.parse(simpleV));
          return gc;
      }
      
      public String calendarToString(GregorianCalendar v) 
      {
          df.setCalendar(v); 
      
          String out = df.format(v.getTime());
      
          return out.substring(0, 22).concat(":").concat(out.substring(22)); // read above on '22'
      }   
      

      就像我说的,这远非完美,但我没有找到更好的..

      【讨论】:

      • 我知道这是一个古老的问题报告,但如果有人对如何以格式本身完成此操作感到好奇,只需使用 ZZ 而不是 Z。使用 Z 给出格式 +XXXX 而ZZ 将以 +XX:XX 的格式给出它
      【解决方案5】:

      你可以通过包java.util.Calendar轻松实现

      试试下面的代码

             import java.util.Calendar; //Import package
      
              String month_order[]={"Select...","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
              Calendar cal = Calendar.getInstance();
              int month = cal.get(Calendar.MONTH) + 1;
              int dom = cal.get(Calendar.DAY_OF_MONTH);
              int doy=cal.get(Calendar.YEAR);
              System.out.println("Current date: "+month_order[month]);
              System.out.println("Current Month:  "+dom);
              System.out.println("Current year: "+doy);
      

      【讨论】:

        【解决方案6】:

        这是 XML/XSD dateTime 数据类型格式。使用 javax.xml.datatype.XMLGregorianCalendar

            XMLGregorianCalendar gc = DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-04-12T13:38:48+02:00");
            System.out.println(gc);
        

        输出

        2013-04-12T13:38:48+02:00
        

        【讨论】:

        • 就是这个。顺便可以使用 toGregorianCalendar.getTime 获取一个 java.util.Date。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-28
        • 2015-11-19
        • 1970-01-01
        • 2015-08-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多