public class DateTest {
@Test
public void test1(){
LocalDate date = LocalDate.of(2022, 3, 1);
System.out.println(date.getYear());
System.out.println(date.getMonth());
System.out.println(date.getDayOfWeek());
System.out.println(date.getDayOfMonth());
System.out.println(date.lengthOfMonth());
}
@Test
public void test2(){
LocalDate now = LocalDate.now();
System.out.println(now);
LocalTime time = LocalTime.now();
System.out.println(time);
System.out.println(time.getHour());
}
@Test
public void test3(){
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.of(date, time);
System.out.println(dateTime);
System.out.println(dateTime.toLocalDate());
}
@Test
public void test4(){
LocalTime time1 = LocalTime.now();
LocalTime time2 = LocalTime.now();
Duration duration = Duration.between(time1, time2);
System.out.println(duration);
Period period = Period.between(LocalDate.of(2022, 3, 1), LocalDate.of(2022, 2, 25));
System.out.println(period);
}
@Test
public void test5(){
LocalDate date = LocalDate.of(2022, 2, 20);
LocalDate date1 = date.withMonth(4);
System.out.println(date1);
LocalDate date2 = date.plusDays(5);
System.out.println(date2);
LocalDate date3 = date.minusDays(5);
System.out.println(date3);
}
@Test
public void test6(){
LocalDate date = LocalDate.of(2022, 2, 20);
String s1 = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
String s2 = date.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(s1);// 2022-02-20
System.out.println(s2);// 20220220
}
@Test
public void test7(){
LocalDate date1 = LocalDate.parse("2022-02-20", DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate date2 = LocalDate.parse("20220220", DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(date1);
System.out.println(date2);
}
@Test
public void test8(){
//模板创建DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate date = LocalDate.now();
String s = date.format(formatter);
System.out.println(s);
//根据模板转回date对象
LocalDate localDate = LocalDate.parse(s, formatter);
System.out.println(localDate);
}
}
|