在Joda-Time中,将日期时间分别为日期和时间,时区为“Z”(Separate DateTime to Date and Time with time zone `Z` at the end, in Joda-Time)
我有一个Joda-Time DateTime对象,需要分别设置日期和时间,最后是时区标签:
DateTime dateTime = new DateTime(); System.out.println(dateTime.toString("YYYY-MM-ddZ")); System.out.println(dateTime.toString("HH:mm:ssZ"));在这种情况下,输出将是:
2014-02-27+0000 15:10:36+0000几乎就是我需要的东西,但它有可能像这样吗?
2014-02-27Z 15:10:36ZI have a Joda-Time DateTime object and need to have date and time separately, with time zone label at the end:
DateTime dateTime = new DateTime(); System.out.println(dateTime.toString("YYYY-MM-ddZ")); System.out.println(dateTime.toString("HH:mm:ssZ"));In this case the output will be:
2014-02-27+0000 15:10:36+0000Almost exactly what I need, but it is possible to have it like this?
2014-02-27Z 15:10:36Z最满意答案
这是一个有效的例子
DateTime dateTime = new DateTime(); DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd") .appendTimeZoneOffset("Z", false, 2, 2) .toFormatter(); System.out.println(formatter.print(dateTime.withZone(DateTimeZone .forID("Zulu"))));基本上,如果时区偏移为零,则打印Z 由于Zulu或UTC的偏移量为0,因此将打印出来。
Here's a working example
DateTime dateTime = new DateTime(); DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd") .appendTimeZoneOffset("Z", false, 2, 2) .toFormatter(); System.out.println(formatter.print(dateTime.withZone(DateTimeZone .forID("Zulu"))));Basically, if the time zone offset is zero, you print Z. Since Zulu or UTC has an offset of 0, that's what will be printed.
更多推荐
发布评论