2015-11-29 226 views
1

我想解析字符串到日期時間。但是,我在運行時遇到錯誤。下面解析字符串日期時間

代碼:

public static void main(String[] args) { 
    String date = LocalDate 
        .parse("2015-11-29") 
        .format(DateTimeFormatter.ISO_DATE_TIME); 
    System.out.println(date); 
} 

我在做什麼錯?

+0

有一個看看[JavaDoc中(https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html):*」 ISO_DATE_TIME \t帶ZoneId的日期和時間\t'2011-12-03T10:15:30 + 01:00 [Europe/Paris]'「*,或許您應該使用*」ISO_LOCAL_DATE \t ISO Local Date \t'2011-12-03 「「* – MadProgrammer

回答

5

ISO_DATE_TIME具有格式模式2011-12-03T10:15:30+01:00。你可以使用ISO_LOCALDATE

String date = LocalDate.parse("2015-11-29").format(DateTimeFormatter.ISO_LOCAL_DATE);