jdk8.0以后的时间类

目录

1.LocalDate LocalTime LocalDateTime 类

2.Instant

3.DateTimeFormatter


1.LocalDate LocalTime LocalDateTime 类

LocalDateTime 相比于其他的  LocalDate LocalTime使用频率高
        //now() 获取当前日期,时间 ,日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("*********************");

        System.out.println(localDate);  //2021-11-19
        System.out.println(localTime);  //15:35:47.491682900
        System.out.println(localDateTime);//2021-11-19T15:35:47.491682900

        System.out.println("*********************");
        //of:设置指定的年月日时分秒,没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 8, 2, 20, 12);
        System.out.println(localDateTime1);

        System.out.println("*********************");
        //getXxx 获取相应属性
        System.out.println(localDateTime.getDayOfMonth());//19
        System.out.println(localDateTime.getDayOfWeek());//FRIDAY
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMonth());

        System.out.println("*********************");
        //withXxx 设置相关的属性 不可变形
        LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(22);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);

        System.out.println("*********************");
        LocalDateTime localDateTime3 = localDateTime.withHour(6);
        System.out.println(localDateTime);
        System.out.println(localDateTime3);

        System.out.println("*********************");
        //不可变性 plus加相关属性 minus减相关属性
        LocalDateTime localDateTime4 = localDateTime.plusMonths(2);
        System.out.println(localDateTime);
        System.out.println(localDateTime4);

        System.out.println("*********************");
        LocalDateTime localDateTime5 = localDateTime.minusMonths(7);
        System.out.println(localDateTime);
        System.out.println(localDateTime5);

2.Instant

Instant 的使用 瞬时点类似于java.util.Date类

        //now() :获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//2021-08-03T11:33:54.821160900Z

        //atOffset() 添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2021-08-03T19:35:49.599421600+08:00

        //toEpochMilli() 获取1970年1月1号0时0分0秒开始的毫秒数
        long milli = instant.toEpochMilli();
        System.out.println(milli);

3.DateTimeFormatter

DateTimeFormatter 的使用类似于simpleDateFormat
 方式1:预定义的标准格式 ISO_OFFSET_DATE_TIME ISO_LOCAL_DATE_TIME ISO_ZONED_DATE_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化 日期--->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = formatter.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(format);//2021-08-03T19:47:00.4514548
        //解析 字符串--->日期
        TemporalAccessor parse = formatter.parse("2021-08-03T19:47:00.4514548");
        System.out.println(parse);
//        方式二:
//        本地化相关格式,如:ofLocalizeDateTime()
//        FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime
        DateTimeFormatter Formatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
//        DateTimeFormatter Formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        //格式化
        String format1 = Formatter1.format(localDateTime);
        System.out.println(format1);//2021年8月3日
//        本地化相关格式,如:ofLocalizeDate()
//         FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format2 = formatter1.format(LocalDate.now());
        System.out.println(format2);//2021年8月3日星期二

        //自定义 ofPattern("yyyy-MM-dd hh:mm:ss")
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//        格式化
        String format3 = formatter3.format(LocalDateTime.now());
        System.out.println(format3);
//        解析
        TemporalAccessor parse1 = formatter3.parse("2021-08-04 09:20:42");
        System.out.println(parse1);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值