Java8 DateTimeFormatter上午/下午 [英] Java8 DateTimeFormatter am/pm
问题描述
我正在尝试解析一些日期,但DateTimeParser似乎不同意我的有效内容
I am trying to parse some dates, but the DateTimeParser seems to disagree with me on what is valid
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
ZonedDateTime.parse("Wed Jul 16, 2016 4:38pm EDT", DateTimeFormatter.ofPattern("EEE MMM dd, yyyy hh:mma z", Locale.US))
我试试这个它说
java.time.format.DateTimeParseException: Text 'Wed Jul 16, 2016 4:38pm EDT' could not be parsed at index 17
因此小时出现问题?当我放下其中一个'h'时它会变得更远(尽管它应该只用0-pad我的小时),但是它不喜欢pm-stuff
So something is wrong with the hours? When I drop one of the 'h' it gets further ( altough it should just 0-pad my hours ), but then it doesn't like the pm-stuff
ZonedDateTime.parse("Wed Jul 16, 2016 4:38pm EDT", DateTimeFormatter.ofPattern("EEE MMM dd, yyyy h:mma z", Locale.US))
java.time.format.DateTimeParseException: Text 'Wed Jul 16, 2016 4:38pm EDT' could not be parsed at index 21
我不知道他的确切问题是什么。当我尝试'hh:mmaa'作为模式时,它表示它不喜欢两个a而现在我被卡住了,因为错误消息没有帮助。
I don't know what his exact problem is. When I try 'hh:mmaa' as a pattern it says that it doesn't like two a and now i am stuck, since the error messages are not helpful.
推荐答案
a
要求 PM
或 AM
大写。要获得不区分大小写的格式化程序,您需要手动构建它:
a
expects either PM
or AM
in upper case. To get a case insensitive formatter you need to build it manually:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("EEE MMM dd, yyyy h:mma z")
.toFormatter(Locale.US);
请注意,由于7月16日不是星期三,您将收到新错误。
Note that you will get a new error because the 16th of July is not a Wednesday.
这篇关于Java8 DateTimeFormatter上午/下午的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!