SimpleDateFormat的解析时间和日期错分和秒 [英] SimpleDateFormat Parsing Time and Date wrong minutes and seconds

查看:401
本文介绍了SimpleDateFormat的解析时间和日期错分和秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我解释一下什么是错在这code:

Can anyone explain to me what's wrong in this code:

System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'").parse("2015-04-22T19:54:11.827Z"));

System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'").parse("2015-04-22T19:54:11.0Z"));

OUT输出是:

Out put is:

Wed Apr 22 20:07:47 GMT+02:00 2015
Wed Apr 22 19:54:00 GMT+02:00 2015

请注意在几分钟的差别时,有毫秒内输入时间。

Please notice the difference in minutes when there are milli seconds in the input time.

推荐答案

有关的 <$ C $>的SimpleDateFormat ,毫秒格式值包含资本取值字符,而不是小写<$ C $ç>取值字符秒。

For SimpleDateFormat, the milliseconds format value contains capital S characters, not lowercase s characters for seconds.

s秒的分钟数55

取值毫秒数978

这是除preting 827 为秒,并将这些秒(847秒有13分钟47秒),以你的价值。

It's interpreting 827 as seconds, and adds those seconds (847 seconds is 13 minutes, 47 seconds) to your value.

使用 SSS 为毫秒。

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

顺便说一句,你不需要重新创建的SimpleDateFormat 不止一次,如果是相同的。您可以一次创建它,将它保存到一个变量,并调用解析多次,一次要分析每个日期/时间字符串。

As an aside, you don't need to re-create your SimpleDateFormat more than once if it's the same. You can create it once, save it to a variable, and call parse multiple times, once for each date/time string you wish to parse.

这篇关于SimpleDateFormat的解析时间和日期错分和秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆