将字符串解析为LocalDate

时间:2015-08-11 18:47:31

标签: java

我有:

String date = "05051990";

我需要以“dd.MM yyy”格式将其解析为java.time.LocalDate

我似乎无法使其发挥作用,请帮助!!

2 个答案:

答案 0 :(得分:1)

试试这个:

    String date="05051990";
    LocalDate localDate =  LocalDate.parse(date,  DateTimeFormatter.ofPattern("ddMMyyyy"));
    date = localDate.format(DateTimeFormatter.ofPattern("dd. MM yyyy"));
    System.out.println(date);

答案 1 :(得分:1)

这是另一种选择:

public class Main {
    public static void main (String[] args) {
        String date = "05051990";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("ddMMyyyy");
        LocalDate localDate = LocalDate.parse(date, dateTimeFormatter);
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("dd. MM yyy.");
        System.out.print(localDate.format(dateTimeFormatter1));
    }
}

输出:

05. 05 1990.

LocalDateDateTimeFormatter的JavaDoc。