您的当前位置:首页正文

JAVA8新特性_06 新日期时间类

2024-02-09 来源:东饰资讯网

七、新日期时间类

        个人觉得特别好用,比老日期类好用的太多了。。。

优点:多线程下也是数据安全的

老日期时间类在多线程下数据不安全

案例(解释都在里面):

import com.afei.AppApplication;
import org.junit.jupiter.api.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

public class NewDateDemo extends AppApplication {

    /**
     *  LocalDate 表示日期类,年月日
     * */
    @Test
    void LocalDate(){
        LocalDate now = LocalDate.now(); // now()获取当前日期
        System.out.println(now);

        // 也可以指定日期of();
        LocalDate of = LocalDate.of(2019, 3, 3);
        System.out.println(of);

        // 获取日期的一些方法
        // of.getMonth() 返回的是一个枚举,如果想要返回数字月份使用getMonthValue()
        System.out.println(of.getYear());
        System.out.println(of.getMonth());
        System.out.println(of.getMonthValue());
        System.out.println(of.getDayOfMonth());
    }

    /**
     *  LocalDate 表示时间类,时分秒
     * */
    @Test
    void LocalTime(){
        LocalTime time=LocalTime.now(); // now()获取当前时间
        System.out.println(time);

        // 也可以指定时间of();
        LocalTime of = LocalTime.of(9, 10, 30);
        System.out.println(of);



        // 获取时间的一些方法
        System.out.println(of.getHour());
        System.out.println(of.getSecond());
        System.out.println(of.getMinute());
    }

    /**
     *  LocalDateTime 表示日期时间类,年月日时分秒
     * */
    @Test
    void LocalDateTime(){
        LocalDateTime time=LocalDateTime.now(); // now()获取当前日期和时间
        System.out.println(time);

        // 也可以指定日期时间of();
        LocalDateTime of = LocalDateTime.of(2019, 4, 26, 15, 30, 30);
        System.out.println(of);

        // 获取日期时间的一些方法
        System.out.println(of.getYear());
        System.out.println(of.getMonth());
        System.out.println(of.getMonthValue());
        System.out.println(of.getDayOfMonth());
        System.out.println(of.getHour());
        System.out.println(of.getSecond());
        System.out.println(of.getMinute());
    }

    /**
     *  修改时间
     *  以LocalDateTime为例
     * */
    @Test
    void SetLocalDateTime(){
        LocalDateTime of = LocalDateTime.of(2019, 4, 26, 15, 30, 30);

        // 修改时间    with方法  with+时间单位   每次修改都会返回一个新的时间类,不会改变以前的时间对象
        LocalDateTime localDateTime = of.withHour(9);
        System.out.println(localDateTime);

        /*
        * 增加或减去时间
        * */
        //增加时间plusYears(2)  增加两年,并返回一个新的时间类对象
        LocalDateTime localDateTime1 = of.plusYears(2);
        System.out.println(localDateTime1);

        //减去时间minusYears(3)  减去三年,并返回一个新的时间类对象
        LocalDateTime localDateTime2 = of.minusYears(3);
        System.out.println(localDateTime2);

    }


    /**
     *  比较时间
     * */
    @Test
    void BiJiaoLocalDateTime(){
        LocalDateTime time=LocalDateTime.now(); // now()获取当前日期和时间

        // 也可以指定日期时间of();
        LocalDateTime of = LocalDateTime.of(2019, 4, 26, 15, 30, 30);

        // time.isAfter(of) time日期是否在of之后
        System.out.println(time.isAfter(of));
        // time.isBefore(of) time日期是否在of之前
        System.out.println(time.isBefore(of));
        // ime.isEqual(of) time日期是否和of日期一样
        System.out.println(time.isEqual(of));

    }

    /**
     *  日期格式化
     * */
    @Test
    void DateTimeFormatter(){
        LocalDateTime time=LocalDateTime.now(); // now()获取当前日期和时间

        // JDK自带的格式话  DateTimeFormatter.ISO_DATE_TIME;   还可以.更多ISO_*****的自带的格式化
        //DateTimeFormatter dtf=DateTimeFormatter.ISO_DATE_TIME;
        //System.out.println(time.format(dtf));

        // 自定义格式化
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:ss");
        System.out.println(time.format(dtf));


        // 解析  将时间字符串解析成LocalDateTime对象
        LocalDateTime parse = LocalDateTime.parse("2022-01-08 10:36:40", dtf);
        System.out.println(parse);


        for(int i=0;i<50;i++){
            new Thread(()->{
                LocalDateTime parse1 = LocalDateTime.parse("2022-01-08 10:36:40", dtf);
                System.out.println(parse);
             }).start();

        }

    }


    /**
     *  Instant   时间戳
     *  内部保存了秒和纳秒,一般用于程序来用,不是给用户
     * */
    @Test
    void Instant(){
        Instant now = Instant.now();
        System.out.println(now);
        System.out.println(now.plusMillis(10));
        System.out.println(now.minusMillis(20));

        System.out.println(now.getEpochSecond());  //秒
        System.out.println(now.getNano());   //纳秒

    }


    /**
     *  Duration/Period 计算时间差
     *  Duration 计算时间:时分秒
     *  Period  计算日期  年月日
     * */
    @Test
    void DurationAndPeriod(){
        //  计算时间
        LocalTime now = LocalTime.now();
        LocalTime of = LocalTime.of(15, 30, 30);

        Duration between = Duration.between(of, now);   // 比较时 第一个参数需要给前面的时间。第二个参数是后面的时间 ,不然会是负数
        System.out.println("相差的天数:"+between.toDays());
        System.out.println("相差的小时:"+between.toHours());
        System.out.println("相差的分钟:"+between.toMinutes());


        LocalDate now1 = LocalDate.now();
        // 比较时 第一个参数需要给前面的日期。第二个参数是后面的日期 ,不然会是负数
        LocalDate of1 = LocalDate.of(2001, 04, 26);
        Period between1 = Period.between(of1, now1);
        System.out.println("相差的年份:"+between1.getYears());
        System.out.println("相差的月份:"+between1.getMonths());
        System.out.println("相差的天数:"+between1.getDays());

    }


    /**
     * 时间校正器
     * TemporalAdjuster 时间校正器
     * TemporalAdjusters  静态方法提供了大量常用的TemporalAdjuster的实现
     *
     * */
    @Test
    void TemporalAdjuster(){
        // 将日期调整到下个月的第10天
        LocalDateTime now = LocalDateTime.now();  //获取当前日期时间

         // 需要知道的是:LocalDate,LocalDateTime,LocalTime 都是实现了Temporal 接口
        TemporalAdjuster temporalAdjuster=temporal -> {  //TemporalAdjuster 创建一个校正器
            LocalDateTime time=(LocalDateTime) temporal;
            return time.plusMonths(1).withDayOfMonth(10);  // 设置增加一个月和第10天
        };

        LocalDateTime with = now.with(temporalAdjuster);
        System.out.println(with);


        /****************************
         *                          *
         *                          *
         *   TemporalAdjusters      *
         *                          *
         *                          *
         ****************************/

        //  TemporalAdjusters 提供了很多静态方法,可以看看
        LocalDateTime now1 = LocalDateTime.now();
        LocalDateTime with1 = now1.with(TemporalAdjusters.firstDayOfNextMonth());
        System.out.println(with1);


    }

    /**
     * 设置日期时间的时区
     * 时区格式:区域/城市
     * */
    @Test
    void ZoneDemo(){
        // 获取时区列表
       // ZoneId.getAvailableZoneIds().forEach(System.out::println);

        //  不带时区,获取当前的计算机时间
        LocalDateTime now = LocalDateTime.now();  // 中国使用的是东八区的时区  比世界标准世界快8个小时
        System.out.println(now);

        // 操作时区
        // now(Clock.systemUTC());创建世界标准时间
        ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC());
        System.out.println(now1);

        // now(); 使用计算机默认的时区时间
        ZonedDateTime now2=ZonedDateTime.now();
        System.out.println(now2);

        // now(ZoneId.of("Canada/Yukon")); 使用指定时区创建日期时间
        ZonedDateTime now3 = ZonedDateTime.now(ZoneId.of("Canada/Yukon"));
        System.out.println(now3);


        /*
         * 修改时区
         */

        //.withZoneSameInstant(ZoneId.of("Asia/Shanghai")); 既修改时区也修改时间
        ZonedDateTime zonedDateTime = now3.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));
        System.out.println("zonedDateTime:"+zonedDateTime);

        //.withZoneSameLocal(ZoneId.of("Asia/Shanghai")); 只修改时区不修改时间
        ZonedDateTime zonedDateTime1 = now3.withZoneSameLocal(ZoneId.of("Asia/Shanghai"));
        System.out.println("zonedDateTime1:"+zonedDateTime1);
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容