持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第15天,点击查看活动详情
JDK1.8 中增加了一套全新的日期时间API,这套API设计合理,是线程安全的,API的位于java.time包下。
- LocalDate: 表示日期,返回格式为 yyyy-MM-HH 即 1970-01-01
- LocalTime: 表示时间,返回格式为 hh:mm:ss 即 23:59:59.999999999
- LocalDateTime:表示日期时间,返回格式为 yyyy-MM-HH T hh:mm:ss 即 2007-12-03T10:15:30.11
- DateTimeFormatter: 日期时间格式转化类
- Instant:时间戳,表示一个时间戳
- Duration:计算2个时间的距离
- Period:计算2个日期的距离
- ZoneDateTime: 包含时区的时间 JDK1.8还提供了 4 套其他的历法
- ThaiBuddhistDate:泰国的佛教历
- JapaneseDate:日本历
- HijrahDate:伊斯兰历 这里我们说三个感性却可以多了解一下第四个。
java.time 包下的类
常见的操作
创建指定日期 以及一些获取
测试:
public static void main(String[] args) {
//创建指定日期
LocalDate f= LocalDate.of(2021,05,23);
System.out.println("f = " + f);
//获取当前日期
LocalDate now = LocalDate.now();
System.out.println("now = " + now);
//获取LocalDate 对应的 年 月 日
System.out.println("now.getYear() = " + now.getYear());
//获取月份
System.out.println("now.getMonth() = " + now.getMonth().getValue());
//这个月 的第几天
System.out.println("now.getDayOfMonth() = " + now.getDayOfMonth());
//一周的星期几
System.out.println("now.getDayOfWeek() = " + now.getDayOfWeek());
//一年的第几天
System.out.println("now.getDayOfYear() = " + now.getDayOfYear());
}
输出结果:
f = 2021-05-23
now = 2022-06-05
now.getYear() = 2022
now.getMonth() = 6
now.getDayOfMonth() = 5
now.getDayOfWeek() = SUNDAY
now.getDayOfYear() = 156
创建时间
测试:
public static void main(String[] args) {
//获取当前时间
LocalTime now = LocalTime.now();
System.out.println("now = " + now);
System.out.println("now.getHour() = " + now.getHour());
System.out.println("now.getMinute() = " + now.getMinute());
System.out.println("now.getSecond() = " + now.getSecond());
System.out.println("now.getNano() = " + now.getNano());
//获取指定的 时间
LocalTime of = LocalTime.of(11, 23, 20, 1111);
System.out.println("of = " + of);
}
输出结果:
now = 11:25:16.782
now.getHour() = 11
now.getMinute() = 25
now.getSecond() = 16
now.getNano() = 782000000
of = 11:23:20.000001111
日期的修改和比较
测试 日期修改:
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);
//修改年份 获取到新的LocalDateTime 不会修改原来的 LocalDateTime对象 其他的值修改也是同理
LocalDateTime localDateTime = now.withYear(2088);
System.out.println("localDateTime = " + localDateTime);
//两天后的时间 当然也有 年 月的方法
LocalDateTime localDateTime1 = now.plusDays(2);
System.out.println("localDateTime1 = " + localDateTime1);
//两天前的时间 当然也有 年 月的方法
LocalDateTime localDateTime2 = now.minusDays(2);
System.out.println("localDateTime2 = " + localDateTime2);
}
输出结果:
now = 2022-06-05T11:33:46.725
localDateTime = 2088-06-05T11:33:46.725
localDateTime1 = 2022-06-07T11:33:46.725
localDateTime2 = 2022-06-03T11:33:46.725
测试 日期比较
public static void main(String[] args) {
LocalDate now = LocalDate.now();
LocalDate of = LocalDate.of(2023, 5, 1);
System.out.println("now.isAfter(of) = " + now.isAfter(of));//在他之后
System.out.println("now.isBefore(of) = " + now.isBefore(of));//在他之前
System.out.println("now.isEqual(of) = " + now.isEqual(of));//是否是同一个时间
}
输出结果:
now.isAfter(of) = false
now.isBefore(of) = true
now.isEqual(of) = false
日期格式化
测试:
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String format = now.format(dateTimeFormatter);
System.out.println("format = " + format);
}
DateTimeFormatter.ISO_LOCAL_DATE_TIME;
public static final DateTimeFormatter ISO_LOCAL_DATE_TIME;
static {
ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_DATE)
.appendLiteral('T')
.append(ISO_LOCAL_TIME)
.toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
}
输出结果:
format = 2022-06-05T11:43:30.964
当然我们也可以指定特定的格式进行格式化
测试:
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = now.format(dateTimeFormatter);
System.out.println("format = " + format);
}
输出结果:
format = 2022-06-05 11:45:05
计算时间日期的差值
测试:
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println("now = " + now);
LocalTime of = LocalTime.of(13, 48, 59);
System.out.println("of = " + of);
Duration duration = Duration.between(now, of);
//差几天
System.out.println("duration.toDays() = " + duration.toDays());
//差几小时
System.out.println("duration.toHours() = " + duration.toHours());
//差几分钟
System.out.println("duration.toMinutes() = " + duration.toMinutes());
//差几秒
System.out.println("duration.toMillis() = " + duration.toMillis());
System.out.println("========================================");
LocalDate now1 = LocalDate.now();
System.out.println("now1 = " + now1);
LocalDate of1 = LocalDate.of(2025, 6, 5);
System.out.println("of1 = " + of1);
Period between = Period.between(now1, of1);
//差几年
System.out.println("between.getYears() = " + between.getYears());
//差几月
System.out.println("between.getMonths() = " + between.getMonths());
//差几日
System.out.println("between.getDays() = " + between.getDays());
}
输出结果:
now = 12:02:53.234
of = 13:48:59
duration.toDays() = 0
duration.toHours() = 1
duration.toMinutes() = 106
duration.toMillis() = 6365766
========================================
now1 = 2022-06-05
of1 = 2025-06-05
between.getYears() = 3
between.getMonths() = 0
between.getDays() = 0
TemporalAdjusters 和 TemporalAdjuster 的使用
public static void main(String[] args) {
LocalDateTime localDateTime =LocalDateTime.now();
System.out.println("localDateTime = " + localDateTime);
//函数时接口实现
TemporalAdjuster adjuster =(temporal)->{
LocalDateTime lo= (LocalDateTime) temporal;
LocalDateTime localDateTime1 = lo.plusMonths(1).withDayOfMonth(1);
System.out.println("函数式接口 TemporalAdjuster="+localDateTime1);
return localDateTime1;
};
LocalDateTime with1 = localDateTime.with(adjuster);
System.out.println("TemporalAdjuster "+with1);
//这里直接使用TemporalAdjusters的静态方法
LocalDateTime with = localDateTime.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println("TemporalAdjusters static = " + with);
}
输出结果:
localDateTime = 2022-06-05T12:16:51.863
函数式接口 TemporalAdjuster=2022-07-01T12:16:51.863
TemporalAdjuster 2022-07-01T12:16:51.863
TemporalAdjusters static = 2022-07-01T12:16:51.863
实践是检验真理的唯一方法! 明天见🥰🥰🥰