将仅包含数字的字符串转换为LocalDateTime

huangapple go评论65阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年10月5日 14:57:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64203746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定