Java8 解析给定字符串的日期或日期时间格式
Java8 parse date or date time format for a given string
我有一个文件可以有日期修改值,格式为日期或日期时间。我曾经将值解析为:
String t = "2012-01-05T21:21:52.834Z";
logger.info(ZonedDateTime.parse(t).toEpochSecond() * 1000);
现在,字符串也可以是
t = "2012-01-05";
引发错误
Exception in thread "main" java.time.format.DateTimeParseException:
Text '2012-01-05' could not be parsed at index 10 at
java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
如果我用 (Reference)
做这个字符串
LocalDate date = LocalDate.parse(t, DateTimeFormatter.ISO_DATE);
logger.info(date.atStartOfDay(ZoneId.of("UTC")).toEpochSecond() * 1000);
这行得通。但是,正如我所提到的,字符串可能是这些类型中的任何一种,我如何识别格式然后相应地获取毫秒数?
一种可能的解决方案是使用具有默认值的可选模式。使用 DateTimeFormatterBuilder
, you can append the wanted pattern with the time part in an optional section, i.e. surrounded by [...]
. In the case where the fields are absent, we provide default values by setting them to 0. The OFFSET_SECONDS
字段为 0 表示与 UTC 没有偏移量。
public static void main(String[] args) {
String[] dates = { "2012-01-05T21:21:52.834Z", "2012-01-05" };
DateTimeFormatter formatter =
new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd['T'HH:mm:ss.SSSz]")
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
.parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
.toFormatter();
for (String date : dates) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse(date, formatter);
System.out.println(zonedDateTime.toEpochSecond() * 1000);
}
}
我有一个文件可以有日期修改值,格式为日期或日期时间。我曾经将值解析为:
String t = "2012-01-05T21:21:52.834Z";
logger.info(ZonedDateTime.parse(t).toEpochSecond() * 1000);
现在,字符串也可以是
t = "2012-01-05";
引发错误
Exception in thread "main" java.time.format.DateTimeParseException: Text '2012-01-05' could not be parsed at index 10 at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
如果我用 (Reference)
做这个字符串LocalDate date = LocalDate.parse(t, DateTimeFormatter.ISO_DATE);
logger.info(date.atStartOfDay(ZoneId.of("UTC")).toEpochSecond() * 1000);
这行得通。但是,正如我所提到的,字符串可能是这些类型中的任何一种,我如何识别格式然后相应地获取毫秒数?
一种可能的解决方案是使用具有默认值的可选模式。使用 DateTimeFormatterBuilder
, you can append the wanted pattern with the time part in an optional section, i.e. surrounded by [...]
. In the case where the fields are absent, we provide default values by setting them to 0. The OFFSET_SECONDS
字段为 0 表示与 UTC 没有偏移量。
public static void main(String[] args) {
String[] dates = { "2012-01-05T21:21:52.834Z", "2012-01-05" };
DateTimeFormatter formatter =
new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd['T'HH:mm:ss.SSSz]")
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
.parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
.toFormatter();
for (String date : dates) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse(date, formatter);
System.out.println(zonedDateTime.toEpochSecond() * 1000);
}
}