JDK8中新增的日期时间API——LocalDate,LocalTime,LocalDateTime

LocalDate,LocalTime,LocalDateTime的使用

介绍:JDK8新增类,主要特征:不可变性

  1. 实例化对象
    • 调用now方法:返回当前日期时间
    • 调用of方法:返回指定的日期时间
  2. 方法
    • getXxx:获取指定时间
    • with:设置当前的时间
    • plus:在当前时间基础上加上一段时间
    • minus:在当前时间寄出上减去一段时间
@Test
    public void test01(){
        //1.实例化对象
        //创建当前时间的对象调用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(2021, 9, 2, 11, 30, 30);
        System.out.println(localDateTime1);
        System.out.println("****************");
        //2.方法
        //getXxx:获取指定时间
        System.out.println(localDateTime1.getDayOfMonth());
        System.out.println(localDateTime1.getMonth());
        System.out.println(localDateTime1.getMonthValue());
        System.out.println(localDateTime1.getHour());
        System.out.println(localDateTime1.getYear());

        //with:设置当前日期时间
        LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(10);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);

        //plus:在当前日期时间加上一段时间
        LocalDateTime localDateTime3 = localDateTime.plusDays(10);
        System.out.println(localDateTime);
        System.out.println(localDateTime3);

        //minus:在当前日期时间减去一段时间
        LocalDateTime localDateTime4 = localDateTime.minusDays(10);
        System.out.println(localDateTime);
        System.out.println(localDateTime4);

    }

instant使用

此方法类记录当前时间(默认是子午线的时间)与1970年1月1日00:00:00的时间差(时间戳)

 @Test
    public void test02(){
        //默认是子午线的时间
        Instant instant = Instant.now();
        System.out.println(instant);
        //添加时间偏移量,转换成东八区的时间
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        long milli = instant.toEpochMilli();
        System.out.println(milli);
    }

DateTimeFormatter使用

方式

  • 预定义的标准模式,例如:ISO_LOCAL_DATE_TIME
  • 本地化相关格式 调用ofLocalizedDateTime方法
  • 自定义格式,ofPattern(“yyyy-MM-dd HH:mm:ss”) 最常用格式!!!
@Test
    public void test03(){
        //方式一:预定义的标准模式,例如:ISO_LOCAL_DATE_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //1.格式化:日期转字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = formatter.format(localDateTime);
        System.out.println(localDateTime);  //2021-07-07T16:57:09.227
        System.out.println(format);         //2021-07-07T16:57:09.227
        //2.解析:字符串转日期
        LocalDateTime localDateTime1 = LocalDateTime.parse(format,formatter);
        System.out.println("格式化:" + localDateTime1);          //2021-07-08T16:09:26.458

        //方式二:本地化相关格式 调用ofLocalizedDateTime方法
        //例如:FormatStyle.FULL、FormatStyle.SHORT
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        //1.格式化
        String format1 = formatter1.format(localDateTime);
        System.out.println(format1);    //21-7-7 下午5:01
        //2.解析
        LocalDateTime localDateTime2 = LocalDateTime.parse(format1,formatter1);
        System.out.println("本地化" + localDateTime2);    //2021-07-08T16:11

        //方式三:自定义格式
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //1.格式化
        String format2 = formatter2.format(localDateTime);
        System.out.println(format2);   //2021-07-07 17:04:19
        //2.解析
        LocalDateTime localDateTime3 = LocalDateTime.parse(format2,formatter2);
        System.out.println(localDateTime3);    //2021-07-08T16:11:34

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值