JDK8时间新API

@RunWith(JUnit4.class)  
public class Jdk8DateTime {
    @Test
    public void practiceLocalDate() {
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate.getMonth().getValue());
        System.out.println(localDate.getDayOfMonth());
        System.out.println(localDate);//2021-01-31
    }
    
    @Test
    public void practiceMills() {
        Long millisecond = Instant.now().toEpochMilli();
        Long millisecondold = System.currentTimeMillis();
        System.out.println(millisecond);//1612099377435
        System.out.println(millisecondold);//1612099377435
    }
    
    @Test
    public void practiceMills2Date() {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1575356644592L), ZoneId.systemDefault());
        System.out.println(localDateTime);
    }
    
    @Test
    public void practiceDateFormat() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = now.format(formatter);
        System.out.println(format);//2019-12-03 15:09:38
    }
    
    @Test
    public void practiceString2Date() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse("2019-12-03 13:52:50", dtf);
        System.out.println(parse);
    }
    
    @Test
    public void practiceDuration() {
        LocalDateTime from = LocalDateTime.now();
        LocalDateTime to = LocalDateTime.now().plusDays(1);
        Duration duration=Duration.between(from, to);
        System.out.println(duration.toHours());
        
        Period period=Period.of(2020, 1, 11);
        Period period1 = Period.between(LocalDate.now(), LocalDate.now().plusYears(1));
        System.out.println(period);
        System.out.println(period1);
    }
}

 如何使用jdk8的api计算到今天结束还有多少秒

      LocalTime localTime = LocalTime.of(23,59,59);
        int leftSec = localTime.toSecondOfDay();
        int trueLeft = leftSec - LocalTime.now().toSecondOfDay();

 时间戳的新写法

LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()

 Instant的用法

@Test
    public void testInstant() {
        Instant now = Instant.now();
        System.out.println("now:"+now);

        Instant now2 = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
        System.out.println("now:"+now2);

        System.out.println("秒数:"+now.getEpochSecond());
        System.out.println("毫秒数:"+now.toEpochMilli());

        System.out.println("毫秒数:"+System.currentTimeMillis());
    }
now:2021-07-06T11:43:19.962Z
now:2021-07-06T19:43:20.026Z
秒数:1625571799
毫秒数:1625571799962
毫秒数:1625571800027

Instant和LocalDateTime有啥区别的,其实区别不大。都是可以精确到纳秒。但是Instant是带有时区的,而LocalDateTime是所在机器的时区。

===========================2021-12-02===============

Date 怎么转成LocalDateTime

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            Date date = orderOperationLogDTO.getCreateTime();
            if (date != null) {
                Instant instant = date.toInstant();
                ZoneId zoneId = ZoneId.systemDefault();
                LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
                String payTime = localDateTime.format(formatter);

转成LocalDateTime才能使用  DateTimeFormatter

 

LocalDateTime转Date

LocalDateTime localDateTime = LocalDateTime.now();
            Date date = Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());

 ==============2022-10-21===============

不使用容器或jvm所在时区的写法

ZonedDateTime now = ZonedDateTime.now(ZoneId.of("GMT+8")); // 2022-10-20T15:53:11.637+08:00[GMT+08:00]
Instant instant = now.toInstant(); // 2022-10-20T07:53:11.637Z

 

posted on 2021-01-31 22:31  MaXianZhe  阅读(44)  评论(0编辑  收藏  举报

导航