Java处理日期、日历和时间的方式一直为社区所诟病,将 java.util.Date设定为可变类型,以及SimpleDateFormat的非线程安全使其应用非常受限。
新API基于ISO标准日历系统,java.time包下的所有类都是不可变类型而且线程安全。
package _05date;
import org.joda.time.format.DateTimeFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class _01LocalDate {
//java8 新特性 线程安全
public static void main(String[] args) {
LocalDate today = LocalDate.now();
//todo 1 获取今天日期(不包含时间)
System.out.println(today);//2020-02-25
//todo 2 获取年月日信息
int year = today.getYear();//2020
Month month = today.getMonth();//FEBRUARY
int monthValue = today.getMonthValue();//2
int dayOfMonth = today.getDayOfMonth();//25
System.out.println(year + " " + month + " " + monthValue + " " + dayOfMonth);
//todo 3 处理特定日期
LocalDate date = LocalDate.of(2020, 2, 25);
System.out.println("自定义日期" + date);//2020-02-25
//todo 4 判断两个日期是否相等
if