jdk8之后关于日期和时间的API

目录

一、Instant

二、DateTimeFormatter:格式化和解析

三、LocalDate、LocalTime、LocalDateTime的使用

四、我自己的总结对照


 

一、Instant

@Test
    public void test2(){
//        now()获取本初子午线的时间
        Instant now = Instant.now();
        System.out.println(now);//2022-01-09T09:51:02.487524200Z
//       添加时间偏移量
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2022-01-09T17:54:51.188335700+08:00 这是现在的时间
//       toEpochMilli()获得时间戳的毫秒数
        long l = now.toEpochMilli();
        System.out.println(l);
//        ofEpochMilli():根据时间戳获取Instant实例
        Instant instant = Instant.ofEpochMilli(28288282l);//根据时间戳来创建时间,这是一个静态方法
        System.out.println(instant);
    }

二、DateTimeFormatter:格式化和解析

@Test
    public void test3(){
//        方式一:预定义的标准格式:
        DateTimeFormatter d = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
       //格式化:日期--->字符串
        LocalDateTime now = LocalDateTime.now();
        String format = d.format(now);
        System.out.println(format);
       // 解析:字符串--->日期
        TemporalAccessor parse = d.parse("2022-01-09T18:15:11.3300169");
        System.out.println(parse);//{},ISO resolved to 2022-01-09T18:15:11.330016900
//        方式二:下面的每一个方法有三个常量参数,这是指格式不同,大抵没有区别
        //本地化相关的格式。如: ofLocalizedDateTime()
        //FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT:适用于LocalDateTime
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String format1 = dateTimeFormatter.format(now);
        System.out.println(format1);//2022/1/9 下午6:26
       // 本地化相关的格式。如: ofLocalizedDate()
       // FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUNM/FormatStyle.SHORT︰适用于LocalDate
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format2 = dateTimeFormatter1.format(LocalDate.now());
        System.out.println(format2);//2022年1月9日星期日

//  重点:方式三:自定义的格式。如: ofPattern("yyyy-MM-dd hh :mm: ss E")
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh :mm: ss E");
//        格式化:
        String format3 = formatter.format(LocalDateTime.now());
        System.out.println(format3);//2022-01-09 06 :36: 09 周日

//        解析
        TemporalAccessor parse1 = formatter.parse("2022-01-09 06 :36: 09 周日");
        System.out.println(parse1);//{SecondOfMinute=9, MinuteOfHour=36, NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=6, MilliOfSecond=0},ISO resolved to 2022-01-09
    }

三、LocalDate、LocalTime、LocalDateTime的使用

LocalDateTime的使用频率最高,三者的方法是一样的
@Test
    public void test() {
//        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 time = LocalDateTime.of(2022, 1, 9, 17, 30, 1);
        System.out.println(time);

//        getXxx()
        System.out.println(time.getDayOfMonth());
        System.out.println(time.getDayOfYear());

//      withXxx()设置相关的属性
        LocalDateTime time1 = time.withDayOfMonth(30);//体现了不可变性
        System.out.println(time);//没变
        System.out.println(time1);//变了

//        plusXxx() 在现有的基础上加上一个值
        LocalDateTime time2 = time.plusHours(3);
        System.out.println(time);
        System.out.println(time2);

//        minusXxx() 在现有的基础上减去一个值
        LocalDateTime time3 = time.minusDays(10);
        System.out.println(time);//2022-01-09T17:30:01
        System.out.println(time3);//2021-12-30T17:30:01  居然可以直接回到2021年,还是很智能的
    }

四、我自己的总结对照

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java塑造中...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值