zoukankan      html  css  js  c++  java
  • java8时间有关新特性


    //
    LocalDate、LocalTime、LocalDateTime LocalDateTime ldt = LocalDateTime.now(); System.out.println(ldt); //2019-08-13T22:30:11.982 LocalDateTime ldt2 = LocalDateTime.of(2015, 10, 19, 13, 22, 22); System.out.println(ldt2); //2015-10-19T13:22:22 LocalDateTime ldt3 = ldt.plusYears(2); System.out.println(ldt3); //2021-08-13T22:30:11.982 LocalDateTime ldt4 = ldt.minusMonths(2); System.out.println(ldt4); //2019-06-13T22:30:11.982 System.out.println(ldt.getYear()); //2019 System.out.println(ldt.getMonthValue()); //8 System.out.println(ldt.getDayOfMonth()); //13 System.out.println(ldt.getHour()); //22 System.out.println(ldt.getMinute()); //30 System.out.println(ldt.getSecond()); //11
    Instant 时间戳(以unix元年:1970年1月1日00:00:00到某个时间之间的毫秒值)
    // Instant 时间戳(以unix元年:1970年1月1日00:00:00到某个时间之间的毫秒值)
    Instant instant=Instant.now();
    System.out.println(instant.toEpochMilli());

    // Duration 用来计算两个时间之间的间隔

    // Duration 用来计算两个时间之间的间隔
    Instant instant=Instant.now();
    Thread.sleep(1000);
    Instant instant1=Instant.now();
    Duration duration=Duration.between(instant,instant1);
    System.out.println(duration.toMillis());
    //1009
    LocalTime localTime=LocalTime.now();
    Thread.sleep(1000);
    LocalTime localTime1= LocalTime.now();
    Duration duration=Duration.between(localTime,localTime1);
    System.out.println(duration.toMillis());
    //1003
                    

    // Period 用来计算两个日期之间的间隔

    LocalDate localDate = LocalDate.of(2015, 1, 1);
    LocalDate localDate1 = LocalDate.now();
    Period between = Period.between(localDate, localDate1);
    System.out.println(between.getYears());
    // 4
    System.out.println(between.getMonths());
    // 7
    System.out.println(between.getDays());
    // 12
  • 相关阅读:
    php内存管理机制、垃圾回收机制
    Redis 3.2.1集群搭建
    centos开启IPV6配置方法
    /etc/hosts.allow和/etc/hosts.deny详解
    3元购买微信小程序解决方案一个月
    linux下使用ntfs-3g挂载NTFS出错
    腾讯云微信小程序域名变更指南
    nginx开启gzip压缩
    centos 7使用yum安装docker容器
    linux中启动网卡报错:Bringing up interface eth1: Error: Connection activation failed
  • 原文地址:https://www.cnblogs.com/windy13/p/11349279.html
Copyright © 2011-2022 走看看