- LocalDate转String
- String转LocalDate
- LocalDate转Date
- Date转LocalDate
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Java8DateTimeTester {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println(" original:[" + localDate + "]");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
System.out.println("formatted:[" + localDate.format(dateTimeFormatter) + "]");
System.out.println("formatted:[" + localDate.format(DateTimeFormatter.ofPattern("y/M/d")) + "]");
System.out.println("formatted:[" + localDate.format(DateTimeFormatter.ofPattern("yyyyMMMM")) + "]");
System.out.println("formatted:[" + localDate.format(DateTimeFormatter.ofPattern("yy年MM月dd日星期e -1")) + "]");
System.out.println("formatted:[" + localDate.format(DateTimeFormatter.ofPattern("yy年MM月dd日星期ee -1")) + "]");
System.out.println("formatted:[" + localDate.format(DateTimeFormatter.ofPattern("yy年MM月dd日eee")) + "]");
System.out.println("formatted:[" + localDate.format(DateTimeFormatter.ofPattern("yy年MM月dd日eeee")) + "]");
System.out.println("formatted:[" + localDate.format(DateTimeFormatter.ofPattern("yy年MM月dd日星期eeeee")) + "]");
System.out.println(" changed:[" + localDate.plusDays(60).format(dateTimeFormatter) + "]");
System.out.println(" changed:[" + localDate.withMonth(12).format(dateTimeFormatter) + "]");
System.out.println(" original:[" + LocalTime.now() + "]");
System.out.println(" original:[" + LocalDateTime.now() + "]");
System.out.println();
localDate = LocalDate.parse("2017|04|18", DateTimeFormatter.ofPattern("yyyy|MM|dd"));
System.out.println("parsed:[" + localDate + "]");
System.out.println();
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
System.out.println("dateFromLocalDate:[" + date + "]");
System.out.println();
localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println("localDateFromDate:[" + localDate + "]");
}
}
参考文章:http://www.importnew.com/14140.html