无法使用 "HH:mm E d MMM YYYY" 模式解析 DateTimeFormatter
DateTimeFormatter could not be parsed using "HH:mm E d MMM YYYY" pattern
我正在从外部数据源检索 date/time,这是 return 格式为“14:30 Sat 05 May”,没有年份。
我一直试图将其解析为 LocalDateTime,但未成功。数据 returned 不会 return 一年,因为它假设我们总是在当年运营。
//date to parse
String time = "14:30 Sat 05 May";
//specify date format matching above string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm E d MMM YYYY") ;
//we do not have a year returned but i can make the assumption we use the current year
LocalDateTime formatDateTime = LocalDateTime.parse(time, formatter).withYear(2018);
以上代码抛出如下异常
线程 "main" java.time.format.DateTimeParseException 中出现异常:无法在索引 16 处解析文本“14:30 Sat 05 May”
感谢任何帮助。
LocalDateTime.parse()
期望 String
表示有效日期,即 year
部分。
调用此方法后无法设置年份:
LocalDateTime.parse(time, formatter).withYear(2018);
必须在之前设置年份,否则 parse()
会抛出 DateTimeParseException
。
作为解决方法,您可以在输入中连接当前年份。
一些补充说明:
- 您使用的模式和文本格式的输入日期不完全匹配。
- 您没有为解析操作指定
Locale
。
意思是会根据JVM所在的local运行.
为确保它在任何情况下都能正常工作,您应该指定 Locale
。
所以您可以尝试类似的方法:
//date to parse
String time = "14:30 Sat 05 May";
time += " " + LocalDate.now().getYear();
//specify date format matching above string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm EEE dd MMM yyyy", Locale.US) ;
//we do not have a year returned but i can make the assumption we use the current year
LocalDateTime formatDateTime = LocalDateTime.parse(time, formatter);
默认年份
在 DateTimeFormatter
, using the DateTimeFormatterBuilder
class by calling parseDefaulting
and specifying the year-field with ChronoField.YEAR
中指定默认年份。
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("HH:mm E d MMM")
.parseDefaulting(ChronoField.YEAR, 2018) // <------
.toFormatter(Locale.ENGLISH);
使用这个格式化程序而不是你的:
LocalDateTime.parse( "14:30 Sat 05 May" , formatter )
…我得到:
2018-05-05T14:30
看到 code run live at IdeOne.com.
注意事项:
- 您的格式模式字符串需要与解析的字符串端到端匹配。因此,当您的日期时间字符串中没有年份时,请不要在您的格式模式中包含
YYYY
。
- 无论如何不要在这里使用大写
YYYY
。它适用于基于周的年份,仅对周数有用。如果您的字符串中有年份,您应该使用 uuuu
或小写 yyyy
.
- 养成为您的格式化程序提供明确区域设置的习惯,这样您就知道它也可以在其他计算机上工作,并且当有一天您使用它的设置时也可以在您的计算机上工作。
我正在从外部数据源检索 date/time,这是 return 格式为“14:30 Sat 05 May”,没有年份。
我一直试图将其解析为 LocalDateTime,但未成功。数据 returned 不会 return 一年,因为它假设我们总是在当年运营。
//date to parse
String time = "14:30 Sat 05 May";
//specify date format matching above string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm E d MMM YYYY") ;
//we do not have a year returned but i can make the assumption we use the current year
LocalDateTime formatDateTime = LocalDateTime.parse(time, formatter).withYear(2018);
以上代码抛出如下异常
线程 "main" java.time.format.DateTimeParseException 中出现异常:无法在索引 16 处解析文本“14:30 Sat 05 May”
感谢任何帮助。
LocalDateTime.parse()
期望 String
表示有效日期,即 year
部分。
调用此方法后无法设置年份:
LocalDateTime.parse(time, formatter).withYear(2018);
必须在之前设置年份,否则 parse()
会抛出 DateTimeParseException
。
作为解决方法,您可以在输入中连接当前年份。
一些补充说明:
- 您使用的模式和文本格式的输入日期不完全匹配。
- 您没有为解析操作指定
Locale
。
意思是会根据JVM所在的local运行.
为确保它在任何情况下都能正常工作,您应该指定Locale
。
所以您可以尝试类似的方法:
//date to parse
String time = "14:30 Sat 05 May";
time += " " + LocalDate.now().getYear();
//specify date format matching above string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm EEE dd MMM yyyy", Locale.US) ;
//we do not have a year returned but i can make the assumption we use the current year
LocalDateTime formatDateTime = LocalDateTime.parse(time, formatter);
默认年份
在 DateTimeFormatter
, using the DateTimeFormatterBuilder
class by calling parseDefaulting
and specifying the year-field with ChronoField.YEAR
中指定默认年份。
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("HH:mm E d MMM")
.parseDefaulting(ChronoField.YEAR, 2018) // <------
.toFormatter(Locale.ENGLISH);
使用这个格式化程序而不是你的:
LocalDateTime.parse( "14:30 Sat 05 May" , formatter )
…我得到:
2018-05-05T14:30
看到 code run live at IdeOne.com.
注意事项:
- 您的格式模式字符串需要与解析的字符串端到端匹配。因此,当您的日期时间字符串中没有年份时,请不要在您的格式模式中包含
YYYY
。 - 无论如何不要在这里使用大写
YYYY
。它适用于基于周的年份,仅对周数有用。如果您的字符串中有年份,您应该使用uuuu
或小写yyyy
. - 养成为您的格式化程序提供明确区域设置的习惯,这样您就知道它也可以在其他计算机上工作,并且当有一天您使用它的设置时也可以在您的计算机上工作。