java8新特性之 日期和时间

在Java 8以前,日期和时间处理一直被广大java程序员抱怨太难用,java.utiljava.sql中,都包含Date类,如果要进行时间格式化,还需要java.text.DateFormat类处理。同时java.util.Date中既包含了日期,又包含了时间,所以java8新的日期和时间库,很好的解决了以前日期和时间类的很多弊端。并且也借鉴了第三方日期库joda很多的优点。

java.time包下主要包含下面几个主要的类:
Instant:时间戳
Duration:持续时间,时间差
LocalDate:只包含日期,比如:2019-02-02
LocalTime:只包含时间,比如:23:12:10
LocalDateTime:包含日期和时间,比如:2019-02-02 23:14:21
Period:时间段
ZoneOffset:时区偏移量,比如:+8:00
ZonedDateTime:带时区的时间
Clock:时钟,比如获取目前美国纽约的时间

1 获取当天日期

      LocalDate localDate =LocalDate.now();
         System.out.println(localDate);
输出结果为:2019-02-02

2 指定日期,进行相应操作

1)获取某月份的第一天的两种方法

     LocalDate localDate =LocalDate.now(); 
     LocalDate firstDay =  localDate.with(TemporalAdjusters.firstDayOfMonth());
     LocalDate firstDay =  localDate.withDayOfMonth(1);

2)取某月的最后一天 

     LocalDate localDate =LocalDate.now();
        LocalDate lastDay =  localDate.with(TemporalAdjusters.lastDayOfMonth());

3)当前日期+1天

     LocalDate localDate =LocalDate.now();
LocalDate tomorrow = localDate.plusDays(1);

4)判断是否为闰年

     LocalDate localDate =LocalDate.now();
        boolean aa =localDate.isLeapYear();

5)指定日期

     LocalDate localDate =LocalDate.of(2019,01,01);
        System.out.println(localDate);

3 生日检查或账单检查

我们在购物时注册会员时都会有生日祝福,例如,用户的生日为1996-03-19,如果今天是2018-03-19,那么今天就是用户的生日(按公历/身份证日期来算),那么通过java8新的日期库,我们该如何来进行实现呢?

     LocalDate birthday = LocalDate.of(1996, 03, 19);
        MonthDay birthdayMd = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
        MonthDay today = MonthDay.from(LocalDate.of(2018, 03, 19));
        System.out.println(today.equals(birthdayMd));
输出结果为 :true

4 获取当前时间(不含日期)

     LocalTime localTime =LocalTime.now();
        ////获取当前时间不显示毫秒 10:13:14
        LocalTime localTime1 =LocalTime.now().withNano(0);
        //指定时间
        LocalTime time =LocalTime.of(0,0,20);
        LocalTime time1 =LocalTime.parse("10:20:20");
        //当前时间+2h
        LocalTime add =localTime.plus(1, ChronoUnit.HOURS);
        LocalTime add1 =localTime.plusHours(1);

5 日期前后比较

     LocalDate today = LocalDate.now();
        LocalDate specifyDate = LocalDate.of(2018, 12, 20);
        System.out.println(today.isBefore(specifyDate)); //false
        System.out.println(today.isAfter(specifyDate)); //true 

6 处理不同时间的时间

     //查看当前的时区
        ZoneId defaultZone = ZoneId.systemDefault();
        System.out.println(defaultZone); //Asia/Shanghai
        //查看美国纽约当前的时间
        ZoneId usa = ZoneId.of("America/New_York");
        LocalDateTime shanghai = LocalDateTime.now();
        LocalDateTime usaTime = LocalDateTime.now(usa);
        System.out.println(shanghai); //2019-02-02T10:28:31.471
        System.out.println(usaTime); //2019-02-01T21:28:31.471,可以看到美国与北京时间差了11小时
        //带有时区的时间
        ZonedDateTime americaZoneDateTime = ZonedDateTime.now(defaultZone);
        System.out.println(americaZoneDateTime); //2019-02-02T11:03:05.926+08:00[Asia/Shanghai]

7 比较两个日期之前时间差

     LocalDate today = LocalDate.now();
        LocalDate date = LocalDate.of(2018, 12, 30);
        System.out.println(date.until(today, ChronoUnit.DAYS)); //34

8 日期时间格式化

在java8之前,我们进行时间格式化主要是使用SimpleDateFormat,而在java8中,主要是使用DateTimeFormatter,java8中,预定义了一些标准的时间格式,我们可以直接将时间转换为标准的时间格式

    LocalDateTime dateTime = LocalDateTime.now();
String strDate1 = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE); // 20190202
String strDate2 = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE); // 2019-02-02
String strDate3 = dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME); // 11:29:43.08
String strDate4 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // 2019-02-02
String strDate5 = dateTime.format(DateTimeFormatter.ofPattern("今天是:YYYY年 MMMM dd日 E", Locale.CHINESE)); // 今天是:2019年 二月 02日 星期六
     //字符串解析日期对象 
String strDate1 = "2019-02-02";
String strDate2 = "2019-02-02 12:30:05";
LocalDate date = LocalDate.parse(strDate1, DateTimeFormatter.ofPattern("yyyy-MM-dd"));//2019-02-02
LocalDateTime dateTime1 = LocalDateTime.parse(strDate2, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));//2019-02-02T12:30:05

但是有些时候标准的时间格式不能满足我们的要求需要我们自定义时间格式

     DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY年MM月dd日00:00:00");
        System.out.println(dateTimeFormatter.format(LocalDate.now()));//2019年02月02日00:00:00

9 Date类于java8时间类相互转化

在转换中,我们需要注意,因为java8之前Date是包含日期和时间的,而LocalDate只包含日期,LocalTime只包含时间,所以与Date在互转中,必定会丢失日期或者时间,或者会使用起始时间。如果转LocalDateTime,就不存在信息误差。

     //Date与Instant的相互转化
        Instant instant  = Instant.now();
        Date date = Date.from(instant);
        Instant instant2 = date.toInstant();
        System.out.println(instant);//2019-02-02T03:22:18.387Z
        System.out.println(date);//Sat Feb 02 11:22:18 CST 2019
        System.out.println(instant2);//2019-02-02T03:22:18.387Z
    //Date转为LocalDateTime Date date2 = new Date(); LocalDateTime localDateTime2 = LocalDateTime.ofInstant(date2.toInstant(), ZoneId.systemDefault()); System.out.println(date2);//Sat Feb 02 11:24:24 CST 2019 System.out.println(localDateTime2);//2019-02-02T11:24:24.213
    //LocalDateTime转Date LocalDateTime localDateTime3 = LocalDateTime.now(); Instant instant3 = localDateTime3.atZone(ZoneId.systemDefault()).toInstant(); Date date3 = Date.from(instant); System.out.println(localDateTime3);//2019-02-02T11:25:38.524 System.out.println(instant3);//2019-02-02T03:25:38.524Z System.out.println(date3);//Sat Feb 02 11:25:38 CST 2019
   //LocalDate转Date,因为LocalDate不包含时间,所以转Date时,会默认转为当天的起始时间,00:00:00 LocalDate localDate4 = LocalDate.now(); Instant instant4 = localDate4.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); Date date4 = Date.from(instant); System.out.println(localDate4);//2019-02-02 System.out.println(instant4);//2019-02-01T16:00:00Z System.out.println(date4);//Sat Feb 02 11:26:35 CST 2019