Java8(5)新增日期类,了解一下

网上查看了一下该类的文章,都没有看到涉及的操作。所以个人查看Java8API总结了一下。

Java8新增的所有日期类都在包java.time下,而且都是final class和线程安全。
PS:Java8以前的日期类Date、Calendar、SimpleDateFormat等都存在线程安全问题。

LocalDateTime:日期+时间 yyyy-MM-dd HH:mm:ss,替代Calendar
LocalDate:日期,yyyy-MM-dd
LocalTime:时间,HH:mm:ss
YearMonth:年月,比如信用卡的日期类型
MonthDay:月日,比如生日
DateTimeFormatter:日期格式化类,替代SimpleDateFormat
Instant:替代Date
还有很多枚举类,月份枚举Month、星期枚举DayOfWeek、Year类(可以判断闰年)等等。

LocalDateTime类:

LocalDateTime localDateTime = LocalDateTime.parse("2018-01-01 17:30:30", pattern);
System.out.println("当天是周几: " + localDateTime.getDayOfWeek());
System.out.println("ISO_LOCAL_DATE_TIME: " + localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
System.out.println("判断日期大小: " + localDateTime.isBefore(LocalDateTime.now()));

打印结果:

当天是周几: MONDAY
ISO_LOCAL_DATE_TIME: 2018-01-01T17:30:30
判断日期大小: true

LocalDate类:

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDate localDate = LocalDate.parse("2018-06-07 17:30:30", pattern);
System.out.println("localDate: " + localDate);
System.out.println("ISO_LOCAL_DATE: " + localDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println("当月第一天:" + LocalDate.now().withDayOfMonth(1));
System.out.println("当前年2月最后一天: " + LocalDate.now().withMonth(2).with(TemporalAdjusters.lastDayOfMonth()));
//下周的日期两个方式,日、月、年类似
System.out.println("下周日期: " + localDate.plus(1, ChronoUnit.WEEKS));
System.out.println("下周日期: " + localDate.plusWeeks(1));
System.out.println("2018年1月第一个星期天: " + LocalDate.of(2018, 1, 1).with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)));

打印结果:

localDate: 2018-06-07
ISO_LOCAL_DATE: 2018-06-07
当月第一天:2018-06-01
当前年2月最后一天: 2018-02-28
下周日期: 2018-06-14
下周日期: 2018-06-14
2018年1月第一个星期天: 2018-01-07

LocalTime类:

System.out.println("LocalTime: " + LocalTime.of(1, 1, 1));

打印结果:

LocalTime: 01:01:01

这三个类很多方法都一样,就不多介绍了。

Instant类:

获取毫秒数:

Instant.now().toEpochMilli();

Clock类:

可以转换成Instant。

Clock clock = Clock.systemUTC();
System.out.println(clock.instant());
// 获取毫秒数
System.out.println(clock.millis());

打印结果:

2018-06-08T16:59:45.771Z
1528477185817

YearMonth类:

YearMonth yearMonth = YearMonth.now();
System.out.println("是否过期: " + yearMonth.isAfter(YearMonth.of(2018, 12)));
System.out.println("lengthOfMonth: " + yearMonth.lengthOfMonth());
System.out.println("lengthOfYear: " + yearMonth.lengthOfYear());

打印结果:

是否过期: false
lengthOfMonth: 30
lengthOfYear: 365

MonthDay 类:

MonthDay monthDay = MonthDay.of(6, 9);
System.out.println("是否同一天生日:" + MonthDay.now().equals(monthDay));

打印结果:

是否同一天生日:true

我们往往需要获取两个日期相差天数、周数、月份等,再也不用像以前那么苦逼的算了,Java8直接提供了方法。

LocalDate startDate = LocalDate.parse("2017-02-01");
LocalDate endDate = LocalDate.parse("2018-01-31");
System.out.println("周数差: " + startDate.until(endDate, ChronoUnit.WEEKS));

打印结果:

周数差: 52

有时候我们也需要获取某一天是当年的第几周,这时候需要使用WeekFields类,主要用于周的运算。
WeekFields.ISO代表每周从周一开始算。
WeekFields.SUNDAY_START代表每周从周日开始算。

TemporalField weekFields = WeekFields.ISO.weekOfYear();
System.out.println("第几周:" + LocalDate.of(2017, 1, 1).get(weekFields));

比较诡异的是打印结果是:

第几周:0

官方解释是:

  • if the 1st day of the year is a Monday, week one starts on the 1st and there is no week zero
  • if the 2nd day of the year is a Monday, week one starts on the 2nd and the 1st is in week zero
  • if the 4th day of the year is a Monday, week one starts on the 4th and the 1st to 3rd is in week zero
  • if the 5th day of the year is a Monday, week two starts on the 5th and the 1st to 4th is in week one

意思就是1月第一周天数<=3(周一是星期起始天),那么这些天都是第0周。
所以要获取一年有多少周的时候,需要判断一下:

int firstWeek = LocalDate.of(2017, 1, 1).get(weekFields);
int lastWeek = LocalDate.of(2017, 12, 31).get(weekFields);
System.out.println("2017年的周数:" + (firstWeek == 0 ? lastWeek + 1 : lastWeek));

打印结果:

2017年的周数:53

以上。

你可能感兴趣的:(Java8(5)新增日期类,了解一下)