zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    FreeCommander 学习手册
    String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)
    StringBuffer 详解 (String系列之3)
    StringBuilder 详解 (String系列之2)
    java io系列26之 RandomAccessFile
    java io系列25之 PrintWriter (字符打印输出流)
    java io系列24之 BufferedWriter(字符缓冲输出流)
    java io系列23之 BufferedReader(字符缓冲输入流)
    java io系列22之 FileReader和FileWriter
    java io系列21之 InputStreamReader和OutputStreamWriter
  • 原文地址:https://www.cnblogs.com/juniorMa/p/14354462.html
Copyright © 2011-2022 走看看