jdk8 时间操作

jdk8 时间操作

java.time类简介

Instant时间戳(瞬时时间,带时区)
LocalDate日期(比如:2018-09-24,不带时区)
LocalTime时间(比如:10:32:10,不带时区)
LocalDateTime日期时间(比如:2018-09-24 10:32:10,不带时区)
Duration两个时间的差,精确到秒或纳秒
Peroid两个日期的差(精确到日)
DateTimeFormatter日期时间格式化类
ZoneId时区
ZoneOffset时区偏移量(比如:+8:00)
ZonedDateTime带时区的日期时间
ChronoUnit日期枚举类(在时间加减操作可用到)
MonthDay月日
YearMonth年月

时间格式间相互转换

1.LocalDate转Date
 
LocalDate nowLocalDate = LocalDate.now();
Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
 
2.LocalDateTime转Date
 
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
 
3.Date转LocalDateTime(LocalDate)
 
Date date =newDate();
LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
 
4.LocalDate转时间戳
 
LocalDate localDate = LocalDate.now();
longtimestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
 
5.LocalDateTime转时间戳
 
LocalDateTime localDateTime = LocalDateTime.now();
longtimestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
 
6.时间戳转LocalDateTime(LocalDate)
 
longtimestamp = System.currentTimeMillis();
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();

localDate API

       //localDate 年月日
        LocalDate now = LocalDate.now();
        System.out.println(now); //2022-09-14  localDate 年月日

        //创建localDate
        LocalDate of = LocalDate.of(2022, 9, 15);// 2022-9-15

        //创建localDate
        LocalDate date = LocalDate.parse("2020-01-01");

        //atStartOfDay  localDate 转 localDateTime  时分秒为0
        LocalDateTime localDateTime = now.atStartOfDay();

        //plusDays(± X) 获取前后多少天的时间
        LocalDate localDate = now.plusDays(1);
        System.out.println(localDate); //2022-09-15

        //plusWeeks(± x) 获取前后几周的时间
        LocalDate localDate1 = now.plusWeeks(1);

        // 获取本周一的时间   DayOfWeek 中可选    周一到周日
        LocalDate with = now.with(DayOfWeek.MONDAY);
        System.out.println(with);

        //获取本月第一天日期
        LocalDate with1 = now.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(with1);

        //获取本月最后一天日期
        LocalDate with2 = now.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(with2);

        //获取本月总天数
        int i = now.withMonth(now.getMonthValue()).lengthOfMonth();
        System.out.println(i);

        //获取本年第一天
        LocalDate with3 = now.with(TemporalAdjusters.firstDayOfYear());
        System.out.println(with3);

        //字符串转时间
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String s = "2022-09-14";
        LocalDate parse = LocalDate.parse(s, dateTimeFormatter);
        System.out.println(parse);

        //时间转字符串
        String format = now.format(dateTimeFormatter);
        System.out.println(format);

        //判断两个时间是否相等
        boolean equal = now.isEqual(of);
        System.out.println(equal); //false

        // 判断now是否在of之后
        boolean after = now.isAfter(of);
        System.out.println(after);

        //判断now是否在of之前
        boolean before = now.isBefore(of);
        System.out.println(before);

localDateTime API 与localdate 类似

       LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        LocalDateTime of = LocalDateTime.of(2022, 9, 15, 15, 30, 20, 1);

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyy-MM-dd HH:ss:mm");
        System.out.println(now.format(dateTimeFormatter));

        //减去多少天
        LocalDateTime localDateTime = now.minusDays(2);
        System.out.println(localDateTime);
        //加上多少天后的时间
        LocalDateTime localDateTime1 = now.plusDays(2);

        //now和of时间做比较 相等为0 now>of 为 1    now<of 为  -1
        int i = now.compareTo(of);
        System.out.println(i);

Instant

  • 理解 :个人理解是一个带有时区的时间 个人常用来处理时间戳的转换
       Instant now = Instant.now();
        System.out.println(now);
        LocalDateTime localDateTime = now.atZone(ZoneId.systemDefault()).toLocalDateTime();
        System.out.println(localDateTime);

        //获得当前时间的时间戳
        long epochSecond = now.getEpochSecond();
        System.out.println(epochSecond);


        //获得当前时间的毫秒的时间戳
        long l = now.toEpochMilli();
        System.out.println(l);

        LocalDateTime localDateTime1 = Instant.ofEpochSecond(epochSecond).atZone(ZoneId.systemDefault()).toLocalDateTime();
        System.out.println(localDateTime1);

ZoneId java.time.ZoneId 是java8的新时区类

//获取系统时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId); //Asia/Shanghai
        ZoneId zoneId1 = TimeZone.getDefault().toZoneId();

参考:https://blog.csdn.net/kfepiza/article/details/115433132
参考: 跳转

Peroid

  • 主要是Period类方法getYear(),getMonth() 和 getDays() 来计算,只能精确到年月日
  • 用于LocalDate之间的比较
   LocalDate today = LocalDate.now(); //2022-09-14
        System.out.println(today);
        LocalDate birthDay = LocalDate.of(1995, 10, 17);
        System.out.println(birthDay);
        Period period = Period.between(birthDay, today);
        int years = period.getYears(); //年份相差多少 27
        int months = period.getMonths();// 月份相差多少 10 如果大于本月会算到明年本月时间 
        int days = period.getDays();//日期相差多少 28
        System.out.println(months);

Duration

  • 在Java8中,我们可以使用以下类来计算时间间隔差异:java.time.Duration

  • 提供了使用基于时间的值测量时间量的方法

  • 用于LocalDateTime之间的比较,也可以用于Instant之间的比较

// 当前本地 年月日
        LocalDateTime today = LocalDateTime.now();
        System.out.println(today);

        // 生日的年月日
        LocalDateTime birthDate = LocalDateTime.of(2000, 12, 16, 8, 32, 57);
        System.out.println(birthDate);

        Duration duration = Duration.between(birthDate, today);  //参数二减去参数一
        System.out.println("隔了多少天" + duration.toDays());
        System.out.println("隔了多少时" + duration.toHours());
        System.out.println("隔了多少分" + duration.toMinutes());
        System.out.println("隔了多少秒" + duration.toMillis());
        System.out.println("隔了多少纳秒" + duration.toNanos());

ChronoUnit

  • 包:java.time.temporal.ChronoUnit
  • ChronoUnit类可用于在单个时间单位内测量一段时间,这个工具类是最全的了,可以用于比较所有的时间单位
   // 本地日期时间对象
        LocalDateTime today = LocalDateTime.now();
        System.out.println(today);

        LocalDateTime birthDate = LocalDateTime.of(2000,12,16,20,32);
        System.out.println(birthDate);

        LocalDate now = LocalDate.now();
        LocalDate of = LocalDate.of(2000, 12, 16);

        long between = ChronoUnit.DAYS.between(of, now);
        System.out.println("localDate相差天数:" + between);
        // ChronoUnit通过点调用它的对象(查看源码可知通过枚举实现),使用该对象调用between

        System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate,today));
        System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate,today));
        System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today));
        System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate,today));
        System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate,today));
        System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate,today));
        System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate,today));
        System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate,today));
        System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate,today));
        System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate,today));
        System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate,today));
        System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate,today));
        System.out.println("相差的世纪数:" + ChronoUnit.CENTURIES.between(birthDate,today));
        System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate,today));
        System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate,today));

参考: https://www.jianshu.com/p/b4bafe07c17c
参考: 跳转

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值