Convert string into LocalDateTime

前端 未结 4 502
滥情空心
滥情空心 2020-11-30 15:12

I have the following String:

18/07/2019 16:20

I try to convert this string into LocalDateTime with the following code:

<         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 16:10

    Use below to convert the time from String to LocalDateTime, but make sure you are getting the time in String form.

    String str = "2016-03-04 11:30";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
    

    Btw, If your String contains seconds as well like "2016-03-04 11:30: 40", then you can change your date time format to yyyy-MM-dd HH:mm:ss" as shown below:

    String str = "2016-03-04 11:30: 40";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
    

提交回复
热议问题