JAVA8新时间日期API(掌握时间格式转换)
class DateApiTests {
@Test
void contextLoads() {
LocalDate now1 = LocalDate.now();
System.out.println(now1);
System.out.println(now1.getYear());
System.out.println(now1.getMonthValue());
System.out.println(now1.getDayOfMonth());
System.out.println(now1.getDayOfWeek());
System.out.println("-----------");
LocalTime now2 = LocalTime.now();
System.out.println(now2);
System.out.println(now2.getHour());
System.out.println(now2.getMinute());
System.out.println(now2.getSecond());
LocalDateTime now3 = LocalDateTime.now();
System.out.println(now3);
System.out.println("----------------");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒 SSS毫秒");
String format = formatter.format(now3);
System.out.println(format);
System.out.println("--------");
LocalDate time = LocalDate.of(1999, 9, 9);
boolean after = time.isAfter(now1);
System.out.println(after);
boolean before = time.isBefore(now1);
System.out.println(before);
System.out.println("--------");
LocalDate startDate = LocalDate.of(2015, 2, 20);
LocalDate endDate = LocalDate.of(2017, 1, 15);
Period period = Period.between(startDate, endDate);
System.out.println(period.getYears() + "(年)" +
period.getMonths() + "(月)" + period.getDays() + "(日)");
}
}