DateTimeFormatterBuilder 无法选择格式
DateTimeFormatterBuilder fail to chose format
我已经配置了格式化程序:
public static final DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.append(forPattern("yyyy-MM-dd"))
.append(forPattern("MM/dd/yy"))
.append(forPattern("MMM dd, yyyy"))
.toFormatter();
并尝试解析字符串 2017-08-29
LocalDate.parse(dt, DATE_FORMATTER).toDateTimeAtStartOfDay().toLocalDateTime()
我遇到错误:
IllegalArgumentException: Invalid format: "2017-08-29" is too short
如果我在构建器中保留 "yyyy-MM-dd" 唯一格式,错误就会消失。
我是在滥用 API 吗?如果第一种格式失败,我希望解析器尝试另一种格式。
当您使用 append
方法时,您正在创建一个接受所有三种模式的格式化程序,一个接一个(三个都是必需的)。
如果您想接受三种格式中的任何一种(只是其中一种),您必须使用 appendOptional
代替:
DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormat.forPattern("yyyy-MM-dd").getParser())
.appendOptional(DateTimeFormat.forPattern("MM/dd/yy").getParser())
.appendOptional(DateTimeFormat.forPattern("MMM dd, yyyy").getParser())
.toFormatter();
现在您可以解析三种格式中的任何一种:
System.out.println(LocalDate.parse("2017-08-29", DATE_FORMATTER).toDateTimeAtStartOfDay().toLocalDateTime());
System.out.println(LocalDate.parse("08/29/17", DATE_FORMATTER).toDateTimeAtStartOfDay().toLocalDateTime());
System.out.println(LocalDate.parse("Aug 29, 2017", DATE_FORMATTER).toDateTimeAtStartOfDay().toLocalDateTime());
以上所有输出:
2017-08-29T00:00:00.000
请注意:第三个格式化程序使用月份的简称(MMM
),上面的代码假定系统的默认语言环境是英语(当你创建一个格式化程序,默认情况下它使用与系统默认语言环境相对应的语言。
但这可以在不通知的情况下更改,即使在运行时也是如此,因此最好在格式化程序中指定 java.util.Locale
。
示例:如果月份名称始终为英文,则只需使用等效的语言环境:
DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormat.forPattern("yyyy-MM-dd").getParser())
.appendOptional(DateTimeFormat.forPattern("MM/dd/yy").getParser())
.appendOptional(DateTimeFormat.forPattern("MMM dd, yyyy").getParser())
// use English locale
.toFormatter().withLocale(Locale.ENGLISH);
只需将区域设置更改为最适合您需要的区域。 Check the javadoc了解更多详情。
正如评论中所提醒的,您还可以创建一个解析器数组并在 DateTimeFormatterBuilder
:
中使用
// array with all possible patterns
DateTimeParser[] parsers = new DateTimeParser[] {
DateTimeFormat.forPattern("yyyy-MM-dd").getParser(),
DateTimeFormat.forPattern("MM/dd/yy").getParser(),
DateTimeFormat.forPattern("MMM dd, yyyy").getParser() };
DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
// use array of all possible parsers
.append(null, parsers)
// use English locale
.toFormatter().withLocale(Locale.ENGLISH);
这与上一个的工作方式相同。
Java 新 Date/Time API
Joda-Time 处于维护模式,正在被新的 APIs 取代,所以我不建议用它开始一个新项目。即使在 joda's website 它说:"Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).".
如果您不能(或不想)从 Joda-Time 迁移到新的 API,您可以忽略此部分。
如果您正在使用 Java 8,请考虑使用 new java.time API. It's easier, less bugged and less error-prone than the old APIs.
如果您使用 Java <= 7,您可以使用 ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, you'll also need the ThreeTenABP (more on how to use it ).
下面的代码适用于两者。
唯一的区别是包名称(在 Java 8 中是 java.time
,在 ThreeTen Backport(或 Android 的 ThreeTenABP)中是 org.threeten.bp
),但是 类和方法名称相同。
在创建格式化程序和解析它时,API非常相似:
DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yy"))
.appendOptional(DateTimeFormatter.ofPattern("MMM dd, yyyy"))
// use English locale
.toFormatter(Locale.ENGLISH);
System.out.println(LocalDate.parse("2017-08-29", DATE_FORMATTER).atStartOfDay());
System.out.println(LocalDate.parse("08/29/17", DATE_FORMATTER).atStartOfDay());
System.out.println(LocalDate.parse("Aug 29, 2017", DATE_FORMATTER).atStartOfDay());
以上所有创建一个LocalDateTime
,其值对应于2017-08-29T00:00
。
您还可以使用可选模式(由 []
分隔):
DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("[yyyy-MM-dd][MM/dd/yy][MMM dd, yyyy]", Locale.ENGLISH);
这与上面的工作方式相同。
我已经配置了格式化程序:
public static final DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.append(forPattern("yyyy-MM-dd"))
.append(forPattern("MM/dd/yy"))
.append(forPattern("MMM dd, yyyy"))
.toFormatter();
并尝试解析字符串 2017-08-29
LocalDate.parse(dt, DATE_FORMATTER).toDateTimeAtStartOfDay().toLocalDateTime()
我遇到错误:
IllegalArgumentException: Invalid format: "2017-08-29" is too short
如果我在构建器中保留 "yyyy-MM-dd" 唯一格式,错误就会消失。
我是在滥用 API 吗?如果第一种格式失败,我希望解析器尝试另一种格式。
当您使用 append
方法时,您正在创建一个接受所有三种模式的格式化程序,一个接一个(三个都是必需的)。
如果您想接受三种格式中的任何一种(只是其中一种),您必须使用 appendOptional
代替:
DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormat.forPattern("yyyy-MM-dd").getParser())
.appendOptional(DateTimeFormat.forPattern("MM/dd/yy").getParser())
.appendOptional(DateTimeFormat.forPattern("MMM dd, yyyy").getParser())
.toFormatter();
现在您可以解析三种格式中的任何一种:
System.out.println(LocalDate.parse("2017-08-29", DATE_FORMATTER).toDateTimeAtStartOfDay().toLocalDateTime());
System.out.println(LocalDate.parse("08/29/17", DATE_FORMATTER).toDateTimeAtStartOfDay().toLocalDateTime());
System.out.println(LocalDate.parse("Aug 29, 2017", DATE_FORMATTER).toDateTimeAtStartOfDay().toLocalDateTime());
以上所有输出:
2017-08-29T00:00:00.000
请注意:第三个格式化程序使用月份的简称(MMM
),上面的代码假定系统的默认语言环境是英语(当你创建一个格式化程序,默认情况下它使用与系统默认语言环境相对应的语言。
但这可以在不通知的情况下更改,即使在运行时也是如此,因此最好在格式化程序中指定 java.util.Locale
。
示例:如果月份名称始终为英文,则只需使用等效的语言环境:
DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormat.forPattern("yyyy-MM-dd").getParser())
.appendOptional(DateTimeFormat.forPattern("MM/dd/yy").getParser())
.appendOptional(DateTimeFormat.forPattern("MMM dd, yyyy").getParser())
// use English locale
.toFormatter().withLocale(Locale.ENGLISH);
只需将区域设置更改为最适合您需要的区域。 Check the javadoc了解更多详情。
正如评论中所提醒的,您还可以创建一个解析器数组并在 DateTimeFormatterBuilder
:
// array with all possible patterns
DateTimeParser[] parsers = new DateTimeParser[] {
DateTimeFormat.forPattern("yyyy-MM-dd").getParser(),
DateTimeFormat.forPattern("MM/dd/yy").getParser(),
DateTimeFormat.forPattern("MMM dd, yyyy").getParser() };
DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
// use array of all possible parsers
.append(null, parsers)
// use English locale
.toFormatter().withLocale(Locale.ENGLISH);
这与上一个的工作方式相同。
Java 新 Date/Time API
Joda-Time 处于维护模式,正在被新的 APIs 取代,所以我不建议用它开始一个新项目。即使在 joda's website 它说:"Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).".
如果您不能(或不想)从 Joda-Time 迁移到新的 API,您可以忽略此部分。
如果您正在使用 Java 8,请考虑使用 new java.time API. It's easier, less bugged and less error-prone than the old APIs.
如果您使用 Java <= 7,您可以使用 ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, you'll also need the ThreeTenABP (more on how to use it
下面的代码适用于两者。
唯一的区别是包名称(在 Java 8 中是 java.time
,在 ThreeTen Backport(或 Android 的 ThreeTenABP)中是 org.threeten.bp
),但是 类和方法名称相同。
在创建格式化程序和解析它时,API非常相似:
DateTimeFormatter DATE_FORMATTER = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yy"))
.appendOptional(DateTimeFormatter.ofPattern("MMM dd, yyyy"))
// use English locale
.toFormatter(Locale.ENGLISH);
System.out.println(LocalDate.parse("2017-08-29", DATE_FORMATTER).atStartOfDay());
System.out.println(LocalDate.parse("08/29/17", DATE_FORMATTER).atStartOfDay());
System.out.println(LocalDate.parse("Aug 29, 2017", DATE_FORMATTER).atStartOfDay());
以上所有创建一个LocalDateTime
,其值对应于2017-08-29T00:00
。
您还可以使用可选模式(由 []
分隔):
DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("[yyyy-MM-dd][MM/dd/yy][MMM dd, yyyy]", Locale.ENGLISH);
这与上面的工作方式相同。