LocalDate类
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Hello{
public static void main(String[] args) {
//now()方法可以获取当前的日期
LocalDate d = LocalDate.now();
System.out.println(d);
//可以单独获取年、月、日、周几、一年的多少天
System.out.println(d.getYear());
System.out.println(d.getMonthValue());
System.out.println(d.getDayOfMonth());
//of()方法可以手动输入日期
LocalDate yesterday = LocalDate.of(2021, 9, 8);
//isBefore()和isAfter()方法比较日期的先后
System.out.println(d.isBefore(yesterday));
//parse()方法格式化日期
LocalDate date = LocalDate.parse("20210909", DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(date);
}
}
LocalTime类
import java.time.LocalTime;
public class Hello{
public static void main(String[] args) {
//now()方法可以获取当前的时间
LocalTime t = LocalTime.now();
System.out.println(t);
//可以单独获取时、分、秒
System.out.println(t.getHour());
System.out.println(t.getMinute());
System.out.println(t.getSecond());
//of()方法可以手动输入时间
LocalTime past = LocalTime.of(20, 9, 8);
//isBefore()和isAfter()方法比较日期的先后
System.out.println(t.isAfter(past));
}
}