Converting number only string into LocalDateTime

136 views Asked by At

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.

2

There are 2 answers

2
Tim Biegeleisen On

Use the format mask yyyyMMddHHmmss?

@JsonFormat(pattern = "yyyyMMddHHmmss")
private LocalDateTime xxxxxDate;
0
Anonymous On

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