在Java8以前,Date日期API对我们非常的不友好,它无法表示日期,只能以毫秒的精度来表示时 间,并且可以修改,他的线程还不是安全的。所以Java8中引入了全新的日期和时间API就是为了解决这 一问题。
java.util.Date 日期类
java.util.Calendar 日历
java.text.SimpleDateFormat 格式化类
java.time.LocalDate 年月日
java.time.LocalTime 时分秒
java.time.LocalDateTime 年月日时分秒
java.time.format.DateTimeFormatter 格式化 日期 时间 日期时间
LocalDate示例:
public class D2 {
public static void main(String[] args) {
LocalDate ld = LocalDate.of(2022, 2, 23);
//是不是闰年
System.out.println(ld.isLeapYear());//false
System.out.println(ld.getYear());//2022
System.out.println(ld.getMonth());//FEBRUARY
System.out.println(ld.getDayOfMonth());//23
System.out.println(ld.getMonthValue());//2
System.out.println(ld.getDayOfWeek());//WEDNESDAY
System.out.println(ld.getDayOfYear());//54
//增加天数
LocalDate tom = ld.plusDays(1);
System.out.println(tom.getDayOfMonth());//24
//减少天数
LocalDate yes = ld.minusDays(1);
System.out.println(yes.getDayOfMonth());//22
//文本按格式转化成日期
LocalDate ld1 = LocalDate.parse("2022-02-10", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.printf("%tF%n", ld1);//2022-02-10
System.out.println(ld1.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));//2022年02月10日
}
}
使用LocalDate计算两个时间差:
Period方式特有的方式
public class D3 {
public static void main(String[] args) {
LocalDate ldt1 = LocalDate.of(2020, 5, 4);
LocalDate ldt2 = LocalDate.now();
//计算时间差值
System.out.println("方式1相差:" + ldt1.until(ldt2, ChronoUnit.DAYS));
System.out.println("方式2相差:" + Duration.between(ldt1.atStartOfDay(), ldt2.atStartOfDay()).toDays());
System.out.println("方式3相差:" + ChronoUnit.DAYS.between(ldt1, ldt2));
//年月日分别相差
Period period = Period.between(ldt1, ldt2);
System.out.println("方式4相差:" + period.getYears() + "年" + period.getMonths() + "个月" + period.getDays() + "天");
}
}
LocalDate本地时间类示例:
public class D4 {
public static void main(String[] args) {
//获取当前的时间,设置时区,默认使用系统时区
LocalTime lt = LocalTime.now(ZoneId.of("GMT+8"));
System.out.printf("%tT%n", lt);
//设置时间
LocalTime lt1 = LocalTime.of(23, 59, 59);
System.out.println(lt1.getHour());
System.out.println(lt1.getMinute());
System.out.println(lt1.getSecond());
System.out.println(lt1.getNano());
//时间加减
System.out.println(lt1.plusHours(1));
System.out.println(lt1.minusMinutes(50L));
//两个时间差
//方式1
System.out.println(lt.until(lt1, ChronoUnit.HOURS));
System.out.println(lt.until(lt1, ChronoUnit.MINUTES));
System.out.println(lt.until(lt1, ChronoUnit.SECONDS));
//方式2
System.out.println(Duration.between(lt, lt1).toMinutes());
System.out.println(Duration.between(lt, lt1).toMillis());
//方式3
System.out.println(ChronoUnit.HOURS.between(lt, lt1));
System.out.println(ChronoUnit.MINUTES.between(lt, lt1));
}
}
LocalDateTime本地时间日期类示例:
public class D5 {
public static void main(String[] args) {
var dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse("2020-01-25 12:45:56", dtf);
//var t = ldt.format(dtf);
LocalDateTime ldt1 = LocalDateTime.now();
System.out.println(ldt.getYear());
System.out.println(ldt.getMonth().getDisplayName(TextStyle.FULL, Locale.CHINA));
//换算成秒需要设置时区
System.out.println(ldt.toEpochSecond(ZoneOffset.of("+8")));
//获取当前时间的毫秒
System.out.println(ldt1.toInstant(ZoneOffset.of("+8")).toEpochMilli());
//LocalDateTime转换成LocalDate
System.out.println(ldt.toLocalDate().getYear());
System.out.println(ldt.toLocalDate().getDayOfMonth());
System.out.println(ldt.toLocalDate().getMonth());
//LocalDateTime转换成LocalTime
System.out.println(ldt.toLocalTime().getHour());
System.out.println(ldt.toLocalTime().getMinute());
System.out.println(ldt.toLocalTime().getSecond());
//计算两个时间的差值
//方式1
System.out.println(ldt.until(ldt1, ChronoUnit.DAYS));
//方式2
System.out.println(Duration.between(ldt, ldt1).toDays());
//方式3
System.out.println(ChronoUnit.DAYS.between(ldt, ldt1));
}
}