JDK8中的一些日期与时间API

JDK8之前的时间API的一些问题

可变性:像日期和时间这样的类应该是不可变的。
偏移性:Date中的年份是从1900开始的,而月份都从0开始。
格式化:格式化只对Date有用,Calendar则不行。
此外,它们也不是线程安全的;不能处理闰秒。

    @Test
    public void testDate(){
        //偏移量
        Date date = new Date(2020 - 1900, 9 - 1, 8);
        System.out.println(date);//Tue Sep 08 00:00:00 CST 2020
    }

JDK8中日期时间API的测试

1、LocalDate、LocalTime、LocalDateTime的使用

说明:

  1. LocalDateTime相较于LocalDate、LocalTime,使用频率要高
  2. 类似于Calendar
    @Test
    public void test1(){
        //now():获取当前的日期、时间、日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //of():设置指定的年、月、日、时、分、秒。没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
        System.out.println(localDateTime1);

        //getXxx():获取相关的属性
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());

        //体现不可变性
        //withXxx():设置相关的属性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate);
        System.out.println(localDate1);

        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);

        //不可变性
        //plusXxx():在某个属性上加
        //minusXxx():在某个属性上减
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System.out.println(localDateTime);
        System.out.println(localDateTime3);

        LocalDateTime localDateTime4 = localDateTime.minusDays(6);
        System.out.println(localDateTime);
        System.out.println(localDateTime4);
    }

2、Instant的使用

(类似于java.util.Date类)

    @Test
    public void test2(){
        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//本初子午线的市区 东八区需要加8小时

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        //toEpochMilli():获取对应的毫秒数(时间戳)-->Date类的getTime方法
        long l = instant.toEpochMilli();
        System.out.println(l);

        //ofEpochMilli:通过给定的毫秒数,获取Instant实例 -->Date(long millis)
        Instant instant1 = Instant.ofEpochMilli(1623248284874L);
        System.out.println(instant1);
    }

3、DateTimeFormatter:格式化或解析日期、时间

(类似于SimpleDateFormat)

    @Test
    public void test3(){
        //方式一:预定义的标准格式。如
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期  --> 字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String str1 = formatter.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(str1);//2021-06-09T23:56:17.147

        //解析:字符串  --> 日期
        TemporalAccessor parse = formatter.parse("2021-06-09T23:56:17.147");
        System.out.println(parse);

        //方式二:
        // 本地化相关的格式。如:ofLocalizedDateTime()
        //  FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        //格式化
        String str2 = formatter1.format(localDateTime);
        System.out.println(str2);//2021年6月10日 上午11时23分39秒

        // 本地化相关的格式。如:ofLocalizedDate()
        // FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        //格式化
        String str3 = formatter2.format(LocalDate.now());
        System.out.println(str3);//FULL:2021年6月10日 星期四 MEDIUM:2021-6-10

        //方式三:自定义的格式。如:ofPattern("yyyy-MM-dd hh:mm:ss")
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String str4 = formatter3.format(LocalDateTime.now());
        System.out.println(str4);//2021-06-10 11:39:43

        //解析
        TemporalAccessor parse1 = formatter3.parse("2021-06-10 11:39:43");
        System.out.println(parse1);

    }
posted @ 2021-06-09 16:11  koito  阅读(96)  评论(0)    收藏  举报