Java 中 String 转 LocalDateTime 出现错误
Java 中 String 转 LocalDateTime 出现错误
场景
在 Java 中使用 LocalDateTime
解析 String
失败
代码如下
1 | final LocalDateTime result = LocalDateTime.parse("2000-01-01", DateTimeFormatter.ofPattern("yyyy-MM-dd")); |
抛出异常
1 | java.time.format.DateTimeParseException: Text '2000-01-01' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-01-01 of type java.time.format.Parsed |
吾辈也在 SegmentFault 上提出了这个问题,然而直到写出这篇记录时然而没有人告诉吾辈答案。。。
解决
先转换为 LocalDate 再二次转换
吾辈首先找到了一种笨方法
- 先解析为
LocalDate
- 将
LocalDate
转换为LocalDateTime
。
1 | final LocalDateTime localDateTime = LocalDate.parse("2018-12-11", DateTimeFormatter.ISO_DATE).atStartOfDay(); |
使用 DateTimeFormatter 先解析,然后转换为 LocalDateTime
- 使用
DateTimeFormatter.ISO_DATE
解析文本并得到TemporalAccessor
对象 - 使用
temporalAccessor.get
方法获取指定属性 - 使用
LocalDateTime.of
构造一个LocalDateTime
对象
1 | final TemporalAccessor temporalAccessor = DateTimeFormatter.ISO_DATE.parse("2018-12-11"); |
secureGet
是吾辈自定义的一个工具方法,具体看下面的代码
1 | /** |
使用 DateTimeFormatterBuilder 构建器
吾辈在 StackOverflow 找到了一个好的方法
- 使用
DateTimeFormatterBuilder
构建DateTimeFormatter
对象 - 赋予可选匹配项默认值(非常重要)
- 使用
LocalDateTime.parse
进行解析
1 | final DateTimeFormatter formatter = new DateTimeFormatterBuilder() |
最后一种方法满足了吾辈的需求,所以,也便是在这里记录一下啦