Java JDK8新日期API

一、   JDK8 中增加了一套全新的日期时间 API,这套 API 设计合理,是线程安全的。

  1. java.time – 包含值对象的基础包
  2. java.time.chrono – 提供对不同的日历系统的访问
  3. java.time.format – 格式化和解析时间和日期
  4. java.time.temporal – 包括底层框架和扩展特性
  5. java.time.zone – 包含时区支持的类

     说明:大多数开发者只会用到基础包和format包,也可能会用到temporal包。因此,尽管有68个新的公开类型,大多数开发者,大概将只会用到其中的三分之一 。

新的日期及时间 API 位于 java.time 包下,如下是一些该包下的关键类:

LocalDate:表示日期,包含:年月日。格式为:2020-01-13
LocalTime:表示时间,包含:时分秒。格式为:16:39:09.307
LocalDateTime:表示日期时间,包含:年月日 时分秒。格式为:2020-01-13T16:40:59.138
DateTimeFormatter:日期时间格式化类
Instant:时间戳类
Duration:用于计算 2 个时间(LocalTime,时分秒)之间的差距
Period:用于计算 2 个日期(LocalDate,年月日)之间的差距
ZonedDateTime:包含时区的时间

 提供这些方法:

二、使用JDK8中的日期时间

 (1)获取当前日期

     //当前日期
        LocalDate ld=LocalDate.now();
        System.out.println(ld);

        //当前时间
        LocalTime  time=LocalTime.now();
        System.out.println(time);

        //日期和时间
        LocalDateTime tt=LocalDateTime.now();
        System.out.println(tt);

(2) 设置指定的时间

    //设置指定的年、月、日、时、分、秒。没有偏移量
        LocalDateTime ldt = LocalDateTime.of(2024, 3, 20, 14, 10, 48);
        System.out.println(ldt);

(3)获取年月日时分秒

        System.out.println("年:"+ldt.getYear());
        System.out.println("本月第几天:"+ldt.getDayOfMonth());
        System.out.println("日期周几:"+ldt.getDayOfWeek());    //枚举
        System.out.println("本月英文:"+ldt.getMonth());        //枚举
        System.out.println("本月数字:"+ldt.getMonthValue());
        System.out.println("一年中第几天:"+ldt.getDayOfYear());
        System.out.println("时:"+ldt.getHour());
        System.out.println("分:"+ldt.getMinute());
        System.out.println("秒:"+ldt.getSecond());

 

(4)设置偏移量 :plusXXX,minusXXX

       // 获取当前日期
        LocalDateTime date = LocalDateTime.now();

        // 创建一个日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 使用格式化器格式化日期
        String str = date.format(formatter);
        System.out.println("系统当前日期:"+str);

        //下个月的今天
        LocalDateTime next=date.plusMonths(1);
        String str2 = next.format(formatter);
        System.out.println("下个月的今天: "+str2);

        //去年的今天
        LocalDateTime last=date.minusYears(1);
        String str3 = last.format(formatter);
        System.out.println("去年的今天:   "+str3);

(5)设置时间/日期:withDayOfMonth()/withDayOfYear()/withMonth()/withYear()

// 获取当前日期
        LocalDateTime date = LocalDateTime.now();

        // 创建一个日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 使用格式化器格式化日期
        String str = date.format(formatter);
        System.out.println("系统当前日期:"+str);

 

        //上个月的第三天
        LocalDateTime third=date.minusMonths(1).withDayOfMonth(3);
        String str4 = third.format(formatter);
        System.out.println("上个月的第3天:"+str4);

(6)计算日期相对间隔(计算年龄)

        //现在日期
        LocalDate now=LocalDate.now();

        //出生日期
        LocalDate bir=LocalDate.of(2001,3,1);

        //age=现在日期-出生日期
        Period period= Period.between(bir,now);

        //获取年龄
        int age=period.getYears();

        System.out.println("从"+bir+"出生到现在"+now+"的年龄是:"+age);

 

 // 创建 LocalDate 对象
        LocalDate date = LocalDate.now();
        System.out.println(date);

        // 计算两个日期之间相差的时间
        LocalDate date2 = LocalDate.of(2023, 2, 10);
        System.out.println(date2);
        Period period = Period.between(date2, date);
        System.out.println(period.getYears() + "年" + period.getMonths() + "月" + period.getDays() + "天"); // 输出:0年11月11天


        // 创建 LocalTime 对象
        LocalTime time = LocalTime.now();
        System.out.println(time);

        // 计算两个时间点之间相差的时间
        LocalTime time2 = LocalTime.of(10, 30);
        System.out.println(time2);
        Duration duration = Duration.between(time2, time);
        System.out.println(duration.toHours() + "小时" + duration.toMinutes() % 60 + "分钟"); // 输出:6小时1分钟

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值