jdk8 时间类型转换(LocalDateTime、DateTimeFormatter、Instant、ZonedDate、ZonedTime、ZonedTime、Duration、Period)


/**
 * jdk8将时间日期的类规范到java.time目录下
 * 传统使用SimpleDateFormat
 * 遵循国际ISO-8601标准,多线程安全
 * 推荐jdk8+,开发应用使用
 */
public class DateTimeTest {


    @Test
    public void test1() {
        /**
         * LocalDateTime
         */
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        LocalDateTime of = LocalDateTime.of(2020, 12, 13, 14, 15, 16, 17);
        System.out.println(of);
        LocalDateTime localDateTime1 = localDateTime.plusYears(202);
        System.out.println(localDateTime1);
        LocalDateTime localDateTime2 = localDateTime.minusNanos(74);
        System.out.println(localDateTime2);

        System.out.println(of.getDayOfWeek());
        System.out.println(of.getHour());
        System.out.println(of.getMinute());
        /**
         * 输出
         * 2020-12-14T16:28:57.855
         * 2020-12-13T14:15:16.000000017
         * 2222-12-14T16:28:57.855
         * 2020-12-14T16:28:57.854999926
         * SUNDAY
         * 14
         * 15
         */
    }

    @Test
    public void test2() {
        /**
         * Instant: 时间戳
         */
        Instant instant = Instant.now();// 默认获取UTC时区
        System.out.println(instant);

        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//加时区8h

        System.out.println(instant.toEpochMilli());//获取时间戳毫秒数
        /**
         * 输出:
         * 2020-12-14T08:28:32.697Z
         * 2020-12-14T16:28:32.697+08:00
         * 1607934512697
         */
    }

    @Test
    public void test3() {

        /**
         * Duration:计算2个时间之间的间隔
         */
        Instant now = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant now2 = Instant.now();
        Duration between = Duration.between(now, now2);
        System.out.println(between.toMillis());//1000


        /**
         * Period:计算2个日期之间的间隔
         */
        LocalDate localDate1 = LocalDate.of(1998, 12, 27);
        LocalDate now1 = LocalDate.now();
        Period between1 = Period.between(localDate1, now1);
        System.out.println(between1.getYears());
        System.out.println(between1.getMonths());
        System.out.println(between1.getDays());
        /**输出
         * 21
         * 11
         * 17
         */
    }

    @Test
    public void test4() {
        /**
         * DateTimeFormatter:格式化日期/时间
         */
        DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime now = LocalDateTime.now();
        String strDate = now.format(isoDateTime);
        System.out.println(strDate);//2020-12-14T16:27:04.541

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String strDate2 = now.format(dateTimeFormatter);
        System.out.println(strDate2);//2020年12月14日 16:27:04

        // 字符串 -》 日期格式
        LocalDateTime localDateTime = LocalDateTime.parse(strDate2, dateTimeFormatter);
        System.out.println(localDateTime);//2020-12-14T16:27:04
    }


    @Test
    public void test5() {
        /**
         * ZonedDate、ZonedTime、ZonedTime
         */
        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(now);//2020-12-14T16:26:09.064

        LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        ZonedDateTime zonedDateTime = now2.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);//2020-12-14T16:26:09.065+08:00[Asia/Shanghai]

    }

}

 

你可能感兴趣的:(java,java,jdk)