java8新日期类学习

1,583 阅读3分钟

引论:

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

java8中提供三个类(LocalDate,LocalTime,LocalDateTime),它们分别对年月日、时分秒和年月日时分秒进行单独的处理,使用时可以根据自己的实际情况进行选择,它们的API类似

获取一个实例都是,获取当前时间

        LocalDateTime localDateTime=LocalDateTime.now();
        //其方法getYear(),getMonthValue等可以获取年月日时分秒
        LocalTime localTime=LocalTime.now();
        LocalDate localDate=LocalDate.now();

还有很多枚举类,月份枚举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()));

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)));

LocalTime类

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

周数差

LocalDate startDate = LocalDate.parse("2017-02-01");
LocalDate endDate = LocalDate.parse("2018-01-31");
System.out.println("周数差: " + startDate.until(endDate, ChronoUnit.WEEKS));
//**之差只要改变后面的枚举类型就行

本年第几周

//WeekFields.ISO代表每周从周一开始算。
//WeekFields.SUNDAY_START代表每周从周日开始算。
TemporalField weekFields = WeekFields.ISO.weekOfYear();
System.out.println("第几周:" + LocalDate.of(2017, 1, 1).get(weekFields));
//注意
//     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周。

指定时间之后或之前XXX的时间

LocalDateTime now = LocalDateTime.now();
System.out.println("当前时间"+now);
System.out.println("当前时间之前2天:"+now.minusDays(2));
System.out.println("当前时间之后2天:"+now.plusDays(2));
System.out.println("当前时间之前2周:"+now.minusWeeks(2));
System.out.println("当前时间之后2周:"+now.plusWeeks(2));
System.out.println("当前时间之前2月:"+now.minusMonths(2));
System.out.println("当前时间之后2月:"+now.plusMonths(2));
System.out.println("当前时间之前2年:"+now.minusYears(2));
System.out.println("当前时间之后2年:"+now.plusYears(2));
System.out.println("当前时间之前2小时:"+now.minusHours(2));
System.out.println("当前时间之后2小时:"+now.plusHours(2));
System.out.println("当前时间之前2分钟:"+now.minusMinutes(2));
System.out.println("当前时间之后2分钟:"+now.plusMinutes(2));
System.out.println("当前时间之前2秒:"+now.minusSeconds(2));
System.out.println("当前时间之后2秒:"+now.plusSeconds(2));

比较时间先后

LocalDateTime now = LocalDateTime.now();
System.out.println("当前时间:"+now);
LocalDateTime appointTime = LocalDateTime.of(2019, 1, 17, 9, 32, 12);
System.out.println("指定时间:"+appointTime);
System.out.println(now.isAfter(appointTime));
System.out.println(now.isBefore(appointTime));

格式化时间格式及按照一定格式解析

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
//格式化
String format = now.format(formatter);
String format2 = now.format(formatter2);
String format3 = now.format(formatter3);
System.out.println("格式化1:" + format);
System.out.println("格式化2:" + format2);
System.out.println("格式化3:" + format3);
//解析
LocalDateTime localDateTime = LocalDateTime.parse(format, formatter);
LocalDate localDate = LocalDate.parse(format2, formatter2);
LocalTime localTime = LocalTime.parse(format3, formatter3);
System.out.println("解析1:" + localDateTime);
System.out.println("解析2:" + localDate);
System.out.println("解析3:" + localTime);