JDK8新增的时间类

文章介绍了JDK8中对时间日期处理的改进,包括新增的ZoneId用于处理时区,Instant用于表示时间戳,以及DateTimeFormatter用于格式化日期和时间。这些新类提供了更简洁和线程安全的操作,例如通过ZoneId.of()获取时区,Instant.now()获取当前时间,以及使用isBefore()和isAfter()进行时间比较。此外,还展示了如何通过ZonedDateTime进行时间的增加和减少,强调了修改时间不会影响原始对象,而是创建新的时间实例。
摘要由CSDN通过智能技术生成

1、为什么要新增jdk8时间类

jdk7以前的代码较为繁琐,jdk8以后代码进行简化。

jdk7多线程情况下会导致数据安全问题,jdk8中时间日期对象都是不可变得,解决了这个问题。

 ZoneId

 public static void main(String[] args) {

        //获得所有时区的系统名称
        Set<String> zoneIds= ZoneId.getAvailableZoneIds();
        System.out.println(zoneIds.size());
        System.out.println(zoneIds);

        //获取系统的默认时区
        ZoneId zoneId=ZoneId.systemDefault();
        System.out.println(zoneId);

        //获取指定的时区
        ZoneId zoneId1=ZoneId.of("Asia/Pontianak");
        System.out.println(zoneId1);
    }

Instant

public static void main(String[] args) {
         //获取当前时间的Instant对象
        Instant now=Instant.now();
        System.out.println(now);

        //根据秒/毫秒/纳秒获得时间Instant对象
        Instant instant2=Instant.ofEpochMilli(1L);
        System.out.println(instant2);

        //只有静态的才可以用类名调用,比如说static(final可以吗?)。非静态的需要使用对象进行调用
        //指定时区
        ZonedDateTime zonedDateTime=Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);

        //isxxxx判断
        Instant instant3=Instant.ofEpochMilli(0L);
        Instant instant4=Instant.ofEpochMilli(500L);

        boolean result= instant3.isBefore(instant4);//判断3时间是不是在4的前面
        System.out.println(result);

        boolean result1=instant3.isAfter(instant4);//判断3时间是不是在4的后面
        System.out.println(result1);

        //增加删减时间->为了线程安全,修改后会直接创建一个新的对象
        Instant instant6=Instant.ofEpochMilli(5000L);
        System.out.println(instant6);

        Instant instant7=instant6.minusSeconds(1);
        System.out.println(instant7);

    }

DateTimeFormatter 

public static void main(String[] args) {
        /*
        static DateTimeFormatter ofPattern()获取格式对象
        String format(时间对象)按照指定方式格式化
         */

        //获取时间对象
        ZonedDateTime time= Instant.now().atZone(ZoneId.of("Asia/Shanghai"));

         //解析/格式化
        DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");

        //格式化
        System.out.println(dtf1.format(time));
    }

ZoneDateTime

 

public static void main(String[] args) {
        /*
        static ZoneDateTime now()根据当前时间创建对象
        static ZoneDateTime ofxxx()根据指定时间创建对象
        ZoneDateTime withxxx()修改时间系列的方法
        ZoneDateTime minusxxx()减少时间
        ZoneDateTime plusxxx()增加时间
         */

        //1、获取当前时间对象(带时区)
        ZonedDateTime zonedDateTime=ZonedDateTime.now();
        System.out.println(zonedDateTime);

        //2、获取指定的时间对象(带时区)
        //年月日时分秒方式指定
        ZonedDateTime time1=ZonedDateTime.of(2023,10,1,11,12,12,0, ZoneId.of("Asia/Shanghai"));
        System.out.println(time1);

        //通过Instant+时区的方法获取指定对象
        Instant instant=Instant.ofEpochMilli(0L);
        ZoneId zoneId=ZoneId.of("Asia/Shanghai");
        ZonedDateTime time2=ZonedDateTime.ofInstant(instant,zoneId);
        System.out.println(time2);


        //3withxxx修改时间
        ZonedDateTime zonedDateTime2=time2.withYear(2000);
        System.out.println(zonedDateTime2);

        //4减少时间
        ZonedDateTime zonedDateTime3=time2.minusYears(200);
        System.out.println(zonedDateTime3);

        //5增加时间
        ZonedDateTime zonedDateTime4=time2.plusYears(200);
        System.out.println(zonedDateTime4);

        /*
        细节:jdk8中我们修改增加减少的时间,不会修改调用者本身,而是创建一个新的时间对象
         */
    }

Java JDK8中,新增了一些时间处理,主要包括以下几个常用: 1. LocalDate:表示日期,可以用来表示年月日的日期,无时区信息。 2. LocalTime:表示时间,可以用来表示时分秒的时间,无日期和时区信息。 3. LocalDateTime:表示日期和时间,可以用来表示年月日时分秒的完整时间,无时区信息。 4. Instant:表示时间戳,可以用来表示从1970年1月1日开始的毫秒数或纳秒数,具有时区信息。 5. Duration:表示时间间隔,可以用来表示两个时间点之间的差距,以秒或纳秒为单位。 6. Period:表示日期间隔,可以用来表示两个日期之间的差距,以年、月或天为单位。 在使用JDK8的时间处理时,可以使用以下方法进行时间转换: 1. 使用LocalDateTime的parse方法将字符串转换为LocalDateTime对象。 2. 使用LocalDateTime的format方法将LocalDateTime对象格式化为指定格式的字符串。 3. 使用DateTimeFormatter来自定义格式化和解析的模式。 4. 使用Instant对象进行时区转换,可以使用atZone方法指定时区。 5. 使用Duration对象进行时间间隔的计算,可以使用toMinutes、toHours、toDays等方法获取不同单位的时间间隔。 6. 使用Period对象进行日期间隔的计算,可以使用getYears、getMonths、getDays等方法获取不同单位的日期间隔。 总结起来,Java JDK8提供了一系列的时间处理,可以方便地进行时间的表示、转换和计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值