Java8 日期时间类的使用

Java8 日期时间类的使用

Java 8日期时间API的重点

提供了javax.time.ZoneId 获取时区。

提供了LocalDate和LocalTime类。

Java 8 的所有日期和时间API都是不可变类并且线程安全,而现有的Date和Calendar API中的java.util.Date和SimpleDateFormat是非线程安全的。

主包是 java.time,包含了表示日期、时间、时间间隔的一些类。里面有两个子包java.time.format用于格式化, java.time.temporal用于更底层的操作。

时区代表了地球上某个区域内普遍使用的标准时间。每个时区都有一个代号,格式通常由区域/城市构成(Asia/Tokyo),在加上与格林威治或 UTC的时差。例如:东京的时差是+09:00。

获取当前日期

LocalDate now = LocalDate.now();
System.out.println(now);

// 对比Date
Date date = new Date();
System.out.println(date);

打印结果:

2022-02-21
Mon Feb 21 17:33:50 CST 2022

获取当前时间

LocalTime now = LocalTime.now();
System.out.println(now);
System.out.println(now.getHour());
System.out.println(now.getMinute());
System.out.println(now.getSecond());

打印结果:

18:12:33.948
18
12
33

获取年月日信息

System.out.println(now.getYear());
System.out.println(now.getMonth());
System.out.println(now.getMonthValue());
System.out.println(now.getDayOfYear());
System.out.println(now.getDayOfMonth());
System.out.println(now.getDayOfWeek());

System.out.println("==========");
//对比Calender
Calendar instance = Calendar.getInstance();
System.out.println(instance.get(Calendar.YEAR));
System.out.println(instance.get(Calendar.MONTH) + 1);
System.out.println(instance.get(Calendar.DATE));
System.out.println(instance.get(Calendar.HOUR));
System.out.println(instance.get(Calendar.MINUTE));
System.out.println(instance.get(Calendar.SECOND));

打印结果:

2022
FEBRUARY
2
52
21
MONDAY
==========
2022
2
21
5
48
59

处理特定日期

LocalDate birthDate = LocalDate.of(1992, 5, 28);
System.out.println(birthDate);

打印结果:

1998-09-03

周期性时间

LocalDate now = LocalDate.now();
LocalDate birthDate = LocalDate.of(1992, 5, 28);
MonthDay cycleDay = MonthDay.of(birthDate.getMonthValue(), birthDate.getDayOfMonth());
MonthDay currentBirthday = MonthDay.from(cycleDay);
MonthDay today = MonthDay.from(now);

if (today.equals(currentBirthday))
    System.out.println("happy birthday !!!");
else
    System.out.println("happy today ~");

打印结果:

happy today ~

检查闰年

LocalDate date = LocalDate.now();

if (date.isLeapYear())
    consumer.accept("今年是闰年~");
else
    consumer.accept("今年不是闰年~");

打印结果:

今年不是闰年~

日期计算

LocalTime now = LocalTime.now();
System.out.println(now.plusHours(2));
System.out.println(now.plusMinutes(20));
System.out.println(now.plusSeconds(30));

LocalDate date = LocalDate.now();
System.out.println(date.plus(1, ChronoUnit.YEARS));
System.out.println(date.plus(1, ChronoUnit.MONTHS));
System.out.println(date.plus(1, ChronoUnit.DAYS));

打印结果:

20:21:58.008
18:41:58.008
18:22:28.008
2023-02-21
2022-03-21
2022-02-22
LocalTime now = LocalTime.now();
LocalDate date = LocalDate.now();

System.out.println(now.minusHours(2));
System.out.println(now.minusMinutes(20));
System.out.println(now.minusSeconds(30));
System.out.println(date.minus(1, ChronoUnit.YEARS));
System.out.println(date.minus(1, ChronoUnit.MONTHS));
System.out.println(date.minus(1, ChronoUnit.DAYS));

打印结果:

07:26:32.444
09:06:32.444
09:26:02.444
2021-02-22
2022-01-22
2022-02-21

日期比较

LocalDate now = LocalDate.now();
LocalDate birthDate = LocalDate.of(1992, 5, 28);

if (!birthDate.equals(now))
    System.out.printf("%s and %s are not same time!", birthDate, now);

打印结果:

2022-02-21
1998-09-03 and 2022-02-21 are not same time!
LocalDate date = LocalDate.now();
LocalDate someDay = LocalDate.of(2022, 2, 22);

if (date.isBefore(someDay))
    consumer.accept("不急不急,跟他耍耍~");
else if (date.isAfter(someDay))
    consumer.accept("我直接躺平!");
else consumer.accept("赶上了!!!");

LocalTime now = LocalTime.now();
LocalTime someTime = LocalTime.of(10, 0);

if (now.isBefore(someTime))
    consumer.accept("不急不急,跟他耍耍~");
else if (now.isAfter(someTime))
    consumer.accept("我直接躺平!");
else consumer.accept("赶上了!!!");

打印结果:

赶上了!!!
我直接躺平!

计算日期间隔

LocalDate date = LocalDate.now();
LocalDate birthDate = LocalDate.of(1998, 10, 4);

Period between = Period.between(date, birthDate);
System.out.printf("今年她%s岁了", between.getYears());

打印结果:

今年她-23岁了

Clock时钟类

Clock systemUTC = Clock.systemUTC();
Clock defaultZone = Clock.systemDefaultZone();
long millis = defaultZone.millis();
Instant instant = defaultZone.instant();

System.out.printf("根据系统时间返回当前时间并设置为UTC:%s\n", systemUTC);
System.out.printf("当前时区:%s\n", defaultZone);
System.out.printf("当前微毫秒:%s\n", millis);
System.out.printf("当前时间点:%s\n", instant);

打印结果:

根据系统时间返回当前时间并设置为UTC:SystemClock[Z]
当前时区:SystemClock[Asia/Shanghai]
当前微毫秒:1645494788005
当前时间点:2022-02-22T01:53:08.005Z

处理时区

//设置时区
ZoneId zoneId = ZoneId.of("America/New_York");
LocalDateTime now = LocalDateTime.now();

//获取某时区下的时间
ZonedDateTime zonedDateTime = ZonedDateTime.of(now, zoneId);
System.out.printf("美国纽约的当前时间为:%s", zonedDateTime);

打印结果:

美国纽约的当前时间为:2022-02-22T10:17:34.891-05:00[America/New_York]

获取当前时间戳

consumer.accept(Instant.now());

打印结果:

2022-02-22T02:31:19.349Z

格式化日期

System.out.println(LocalDate.parse("20220222", DateTimeFormatter.BASIC_ISO_DATE));

打印结果:

2022-02-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SONNIE在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值