java SimpleDateFormat 解析重置小时和分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17488375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SimpleDateFormat parse resets hours and minutes
提问by Luser_k
I get a date as String like "start_date": "2013-07-05T11:32:00+00:00"
and need a date object, which represents it with hours and minutes.
我得到一个类似字符串"start_date": "2013-07-05T11:32:00+00:00"
的日期,需要一个日期对象,用小时和分钟表示它。
If I use the format "yyyy-MM-dd'T'HH:mm:ss+HH:mm"
and sdf.parse(startDate)
, in the returned date object the hours and minutes are resetted.
如果我使用格式"yyyy-MM-dd'T'HH:mm:ss+HH:mm"
and sdf.parse(startDate)
,则在返回的日期对象中,小时和分钟将被重置。
But I need also hours and minutes for date comparison. How can I workaround this problem ?
但我还需要小时和分钟来进行日期比较。我该如何解决这个问题?
回答by Uwe Plonus
You need the pattern "yyyy-MM-dd'T'HH:mm:ssXXX"
to parse your date. ("XXX"
stands for the timezone
)
您需要模式"yyyy-MM-dd'T'HH:mm:ssXXX"
来解析您的日期。("XXX"
代表timezone
)
In your case the second "HH:mm"
is resetting the values and using the last parsed data.
在您的情况下,第二个"HH:mm"
是重置值并使用最后解析的数据。
回答by JB Nizet
This part is the problem : +HH:mm
. You tell the parser that the part after the + sign is the hour and minutes part of the date, and these values are 00 and 00, so you get 0 and 0 as a result. The part startring at the + sign is in fact an offset to UTC time, and the symbol for that is XXX
(since Java 7).
这部分是问题:+HH:mm
。你告诉解析器 + 号后面的部分是日期的小时和分钟部分,这些值是 00 和 00,所以你得到 0 和 0 作为结果。以 + 号开始的部分实际上是 UTC 时间的偏移量,其符号是XXX
(自 Java 7 起)。