Java8日期的处理以及介绍

一、介绍

1)Clock:时钟

2)Duration:以秒和纳秒为单位模拟一个数量或时间量

3)Instant:在时间线上模拟单个瞬时点

4)LocalDate:表示ISO-8601日历系统中没有时区的日期

5)LocalDateTime:表示ISO-8601日历系统中没有时区的日期时间

6)LocaTime:表示ISO-8601日历系统中没有时区的时间

7)MonthDay:表示ISO-8601日历系统中的月和日

8)OffsetDateTime:表示ISO-8601日历系统中与UTC/Greenwich的偏移量的日期时间

9)OffsetTime:表示在ISO-8601日历系统中与UTC/Greenwich的偏移量的时间

10)Period:根据年,月和日来模拟一个数量或时间量

11)Year:表示ISO-8601日历系统中的一年

12)YearMonth:表示ISO-8601日历系统中的年月

13)ZonedDateTime:表示ISO-8601日历系统中具有时区的日期时间

14)ZoneId:表示时区ID

15)ZoneOffset:表示格林威治/UTC的时区偏移量

二、代码

1、对比代码

Java8与之前的时间对比

        /**
         * Java 8以前 时间精度低:毫秒  date 可读性差  和SimpleDateFormat相比(format calendar是共享变量 ),DateTimeFormatter是线程安全的
         * ==》
         */
        /**
         * date 可读性差
         * 和SimpleDateFormat相比(format calendar是共享变量 ),DateTimeFormatter是线程安全的
         */
System.out.println(new Date());
System.out.println(LocalDateTime.now());
Date date = new Date();
System.out.println( date.getMonth());
System.out.println(LocalDateTime.now().getMonth());

2、Clock

        /**
         * Clock 时钟
         * 获取时钟的当前时刻
         */
    Clock clock = Clock.systemUTC();
    System.out.println("Clock :" + clock.instant()); // 获得当前UTC时刻
    System.out.println("Clock :" + clock.millis()); // 获取豪秒数
    System.out.println(Clock.systemDefaultZone());
    System.out.println(Clock.systemUTC());

3、LocalDate

    // LocalDate  LocalDateTime = LocalDate + LocalTime
    LocalDate localDate = LocalDate.now();
    System.out.println(localDate);
    System.out.println(localDate.getDayOfYear()); // 获取是一年内的第几天  1-365 闰年 1-366
    System.out.println(LocalDate.parse("2018-09-09")); //String 转 LocalDate
    System.out.println(localDate.atStartOfDay());

    // 获取一个指定的时区  获取指定时区的当前时间
    ZoneId zoneId = ZoneId.of("Asia/Shanghai");
    LocalDateTime localDateTime = LocalDateTime.now(zoneId);
    System.out.println(localDateTime);

4、日期的比较

        /**
         * 日期比较 大于 1  小于 -1 等于 0
         */
        System.out.println(LocalDate.parse("2018-08-09").compareTo(LocalDate.parse("2018-09-09")));
        System.out.println(LocalDate.parse("2018-10-09").compareTo(LocalDate.parse("2018-09-09")));
        // 日期调整
        LocalDate date = LocalDate.parse("2017-01-03");
        LocalDate result = date.with(Month.JULY).with(TemporalAdjusters.lastDayOfYear()); //修改月份 调整下一天为下一年
        System.out.println(result);

        // 转化为String
        LocalDateTime now = LocalDateTime.now();
        String text1 = now.format(DateTimeFormatter.ISO_DATE_TIME);
        String text2 = ZonedDateTime.now().format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
        System.out.println(text1);
        System.out.println(text2);
        System.currentTimeMillis();

5、Instant Duration 使用 

        //instant Duration
        Instant detectEndInstant;
        Instant endTime = Instant.now();
        System.out.println(endTime);
        System.out.println(endTime.getEpochSecond());
        detectEndInstant = endTime.truncatedTo(ChronoUnit.HOURS); //小时向前取整 如 9:16:24 => 9:00:00
        System.out.println(detectEndInstant);
        detectEndInstant = endTime.truncatedTo(ChronoUnit.MINUTES); //分钟向前取整 如 9:16:24 => 9:16:00
        System.out.println(detectEndInstant);
        //分钟向前取整10分 如 9:16:24 => 9:10:00 类似查询以10分为点的数据 9:10:00 9:20:00
        long l = endTime.truncatedTo(ChronoUnit.HOURS).until(endTime.truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES) / 10 * 10;
        //System.out.println(l);
        detectEndInstant = endTime.truncatedTo(ChronoUnit.HOURS).plus(Duration.ofMinutes(l));
        System.out.println(detectEndInstant);

        List<Instant> timePointInstants = Stream.iterate(detectEndInstant, t -> t.minus(Duration.ofMinutes(1))).limit(61).collect(
                Collectors.toList());// 60 + 1
        timePointInstants.forEach(System.out::println);

 

参考网站

https://www.yiibai.com/javatime/javatime_clock.html

http://www.matools.com/api/java8

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值