英文:
Converting number only string into LocalDateTime
问题
我已经在网上搜索过,但并没有找到我希望的方法。
我的数据看起来像是"20201005114527","20201002173838"......
并且希望将它们转换为LocalDateTime。
之后它们将会再次转换为json。
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "xxx/xxx")
private LocalDateTime xxxxxDate;
但我只是困惑如何将这些"仅含数字的字符串"转换为LocalDateTime。
英文:
I have searched online but couldn't really find the way to do it as I hope.
My data look like "20201005114527", "20201002173838" .......
and would like to convert them into LocalDateTime.
It will be converted into json again afterwards.
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "xxx/xxx")
private LocalDateTime xxxxxDate;
But I'm just confused of converting those "number-only strings" into LocalDateTime.
答案1
得分: 2
使用格式掩码 yyyyMMddHHmmss
?
@JsonFormat(pattern = "yyyyMMddHHmmss")
private LocalDateTime xxxxxDate;
英文:
Use the format mask yyyyMMddHHmmss
?
@JsonFormat(pattern = "yyyyMMddHHmmss")
private LocalDateTime xxxxxDate;
答案2
得分: 0
解析仅包含数字的日期时间字符串与解析任何其他格式的日期时间字符串没有任何区别。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMddHHmmss");
String numberOnlyString = "20201005114527";
LocalDateTime xxxxxDate = LocalDateTime.parse(numberOnlyString, formatter);
System.out.println(xxxxxDate);
输出结果:
> 2020-10-05T11:45:27
英文:
Parsing a date-time string containing only digits isn’t any different from parsing a date-time string in any other format.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMddHHmmss");
String numberOnlyString = "20201005114527";
LocalDateTime xxxxxDate = LocalDateTime.parse(numberOnlyString, formatter);
System.out.println(xxxxxDate);
Output:
> 2020-10-05T11:45:27
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论