Joda time : How to convert String to LocalDate?

前端 未结 6 1223
旧巷少年郎
旧巷少年郎 2020-12-05 17:05

How to specify the format string to convert the date alone from string. In my case, only the date part is relevant

Constructing it as DateTime fails:

6条回答
  •  攒了一身酷
    2020-12-05 17:45

    In my case, incoming string may be in one of two formats. So I first try to parse string with more specific format:

    String s = "2016-02-12";
    LocalDateTime ldt;
    try {
        ldt = LocalDateTime.parse(s, DateTimeFormat.forPattern("YYYY-MM-dd HH:mm"));
    }
    catch (IllegalArgumentException e) {
        ldt = LocalDateTime.parse(s, DateTimeFormat.forPattern("YYYY-MM-dd"));
    }
    

提交回复
热议问题