本示例说明如何在不指定年份的情况下解析日期(1月2日)。
JavaDateExample.java
package com.mkyong.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class JavaDateExample {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM", Locale.US);
String date = "02 Jan";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
输出量
Exception in thread "main" java.time.format.DateTimeParseException: Text '02 Jan' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=2,