Convert string into LocalDateTime

前端 未结 4 510
滥情空心
滥情空心 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 15:44

    I think this will answer your question:

    val stringDate = expiration_button.text.toString()
    val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
    val dt = LocalDate.parse(stringDate, formatter);
    

    Edit 1:

    It's probably crashing because you are using a 12hr Hour, instead of a 24hr pattern.

    Changing the hour to 24hr pattern by using a capital H should fix it:

    val dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
    

提交回复
热议问题