其他分享
首页 > 其他分享> > JDK8中新日期时间API

JDK8中新日期时间API

作者:互联网

LocalDate、LocalTime、LocalDateTime的使用

 

 

@Test
public void test4() {
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(date);//2022-08-18
System.out.println(time);//22:04:17.866
System.out.println(dateTime);//2022-08-18T22:04:54.487

//设置指定时间,没有偏移量2022-08-16T20:45:30
LocalDateTime time2 = LocalDateTime.of(2022, 8, 16, 20, 45, 30);
System.out.println(time2);
}

 

瞬时Instant 

 

 

@Test
    public void test4() {
        Instant now = Instant.now();
        long time = now.toEpochMilli();
        //返回时间戳
        System.out.println(time);//1660831907291
    }

  

LocalDateTime格式化日期

@Test
    public void test4() {
        LocalDateTime now = LocalDateTime.now();
        //自定义格式
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = dtf.format(now);
        System.out.println(time);//2022-08-18 22:17:32
    }

  

LocalDateTime解析日期

@Test
    public void test4() {
        String time = "2022-08-18 22:17:32";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        TemporalAccessor datetime = dtf.parse(time);

        System.out.println(datetime);//{},ISO resolved to 2022-08-18T22:17:32
    }

  

 

 

标签:LocalDateTime,System,API,中新,2022,time,now,out,JDK8
来源: https://www.cnblogs.com/ixtao/p/16600351.html