Java 中有没有办法将字符串转换为 MMddyyyy 或 Mddyyyy 的日期?
Is there a way in Java to convert strings to dates that are either MMddyyyy or Mddyyyy?
我一直在尝试将字符串转换为日期。其中一些显示如下:1011970(如 1970 年 1 月 1 日),有些显示如下:10011970(如 1970 年 10 月 1 日)。这个月在开始的事实给我带来了一个大问题。
我已经想出了一个解决方案,我可以只检查数字有多少位并使用单独的格式化程序,但我更愿意使用更优雅的东西。我一直在尝试使用 DateTimeFormatterBuilder 创建一个 'one size fits all' 格式化程序。
这是我尝试过的示例以及我得到的输出。
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL)
.appendPattern("ddyyyy")
.toFormatter();
System.out.println(LocalDate.parse("10011970", formatter));
System.out.println(LocalDate.parse("1011970", formatter));
Date: 1970-10-01
Exception in thread "main" java.time.format.DateTimeParseException: Text '1011970' could not be parsed at index 4
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at Main.main(Main.java:36)
正如您所见,上述解决方案适用于第一个格式化日期,但不适用于第二个。
如果您有任何想法,请告诉我。
提前致谢!
詹姆斯
你走在正确的轨道上。这有效:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue(ChronoField.MONTH_OF_YEAR)
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendValue(ChronoField.YEAR, 4)
.toFormatter();
System.out.println(LocalDate.parse("10011970", formatter));
System.out.println(LocalDate.parse("1011970", formatter));
输出为:
1970-10-01
1970-01-01
我不知道为什么在通过格式模式指定年月日时不起作用,但它不起作用,我以前见过。
除此之外,相邻值解析(解析数字日期时间字段之间没有分隔符)的经验法则是您需要指定每个字段的确切宽度除了第一个字段。然后格式化程序从字符串的后端计算宽度,以找出第一个值(这里是月份)使用多少个数字。所以你的用例很合适。
我一直在尝试将字符串转换为日期。其中一些显示如下:1011970(如 1970 年 1 月 1 日),有些显示如下:10011970(如 1970 年 10 月 1 日)。这个月在开始的事实给我带来了一个大问题。
我已经想出了一个解决方案,我可以只检查数字有多少位并使用单独的格式化程序,但我更愿意使用更优雅的东西。我一直在尝试使用 DateTimeFormatterBuilder 创建一个 'one size fits all' 格式化程序。 这是我尝试过的示例以及我得到的输出。
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL)
.appendPattern("ddyyyy")
.toFormatter();
System.out.println(LocalDate.parse("10011970", formatter));
System.out.println(LocalDate.parse("1011970", formatter));
Date: 1970-10-01
Exception in thread "main" java.time.format.DateTimeParseException: Text '1011970' could not be parsed at index 4
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at Main.main(Main.java:36)
正如您所见,上述解决方案适用于第一个格式化日期,但不适用于第二个。 如果您有任何想法,请告诉我。
提前致谢! 詹姆斯
你走在正确的轨道上。这有效:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue(ChronoField.MONTH_OF_YEAR)
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendValue(ChronoField.YEAR, 4)
.toFormatter();
System.out.println(LocalDate.parse("10011970", formatter));
System.out.println(LocalDate.parse("1011970", formatter));
输出为:
1970-10-01 1970-01-01
我不知道为什么在通过格式模式指定年月日时不起作用,但它不起作用,我以前见过。
除此之外,相邻值解析(解析数字日期时间字段之间没有分隔符)的经验法则是您需要指定每个字段的确切宽度除了第一个字段。然后格式化程序从字符串的后端计算宽度,以找出第一个值(这里是月份)使用多少个数字。所以你的用例很合适。