2015-05-19 66 views
1

我写了一个测试直到测试许可证密钥的有效性..并在同一时间,更新代码以使用Java 8 java.time包。DateTimeFormatter格式化失败

所以..创造一个“有效期至”日期时间和格式,为的字符串:

LocalDateTime now = LocalDateTime.now(Clock.systemUTC()); 
LocalDateTime nowPlus1Hour = now.plusHours(1); 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); 
formatter = formatter.withZone(Clock.systemUTC().getZone()); 
String validUntilString = nowPlus1Hour.format(formatter); 

这个..大部分工作。 (我的最后一个版本抛出异常,因为我没有创建一个formatter'withZone'。)

但是,'validUntilString'的值是'2015-05-19 19:46:26 Z'真的。 'Z'。来吧!

因此..给定一个LocalDateTime,你如何得到一个字符串,包括(是的,ambigious,但在这种情况下UTC,一个标准)时区ID?

+1

Z是UTC的同义词。你对我看起来很好。见http://www.timeanddate.com/time/zones/z和http://en.wikipedia.org/wiki/Coordinated_Universal_Time#Time_zones –

+0

好吧..现在我不觉得愚蠢。 – ticktock

+1

Z代表祖鲁时间 – bowmore

回答

1

如说,在评论中,时区已经包含在你的字符串:

"2015-05-19 19:46:26 Z"

这封信Zmilitary - NATO convention

基础来此为Z - 祖鲁时间 - 零子午线时间 - 与UTC相同这是一个将时间区域分配给字母的约定,其中Z =零子午线,Zulu是用于通信的字母Z。它有时在美国军事和北约中与24小时钟一起使用,并且在电影中也用于参考时间。

其他字母/字用于UTC相比其他时区.....

阅读cited reference的全部细节。