• JDK1.8 LocalDateTime 时间类与字符互转


    说明: jdk1.8 严格区分LocalDate(日期) LocalDateTime(时间日期) LocalTime(时间)

    public static void main(String[] args) {
    
        System.out.println(LocalDate.now());//年月日
        System.out.println(LocalDateTime.now());//年月日时分秒
        System.out.println(LocalTime.now());//时分秒
    
        //localDateTime 获取当前时间
        DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ofPattern("yy-MM-dd");
        LocalDate localDateTime=LocalDate.now();
        localDateTime.format(dateTimeFormatter);
        System.out.println(localDateTime);
    
        //字符转成LocalDateTime  
        String time="2018-05-18 23:24:25";
        DateTimeFormatter dateTimeFormatter2=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//严格遵守大小写
        LocalDateTime localDate=LocalDateTime.parse(time,dateTimeFormatter2);
        LocalDate localDate1= LocalDate.of(localDate.getYear(),localDate.getMonth(),localDate.getDayOfMonth());
        System.out.println(localDate1);//2018-05-18
    
        //date 转LocalDateTime
        java.util.Date date = new java.util.Date();
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = date.toInstant();
        LocalDateTime localDateTime2 = LocalDateTime.ofInstant(instant, zone);
        System.out.println(localDateTime2);
    
    
        // LocalDateTime 转date
        LocalDateTime localDateTime3 = LocalDateTime.now();
        Instant instan3 = localDateTime3.atZone(zone).toInstant();
        java.util.Date date3 = Date.from(instant);
        System.out.println(date3);
    
    
         //获取某个时间点的时间
         LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天最小时刻 00:00:00
         LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);//当天最大时刻 23:59:59
    
    
         //比较时间差
         在jdk1.8以后,对表示日期时间的类型进行了重新分类,这里出现了2个新的类,Duraction 和Period
         Duraction表示:时间的区间,用来度量秒和纳秒之间的时间值
         Period表示:一段时间的区间,用来度量年月日和几天之间的时间值
    
         java.time.Duration duration = java.time.Duration.between(LocalDateTime   startTime,  LocalDateTime  endTime );
         duration.toMinutes()    //两个时间差的分钟数
                  toNanos()//纳秒
                  getSeconds() //秒
                  toMillis()//毫秒
                  toMinutes()//分钟
                  toHours()//小时
    
    
        LocalDate localDate1 = LocalDate.of(2021, 9, 1);
        LocalDate localDate2 = LocalDate.of(2020, 10, 2);
        Period next = Period.between(localDate1, localDate2);
        System.out.println("相隔多少天:" + next.getDays());//只比较日差 不比较年和月
        System.out.println(localDate1.toEpochDay() - LocalDate.now().toEpochDay()); //都转换成天数 相减,得到年月日的差
      
    
    
    
    
    }
  • 相关阅读:
    ajax post 数组
    Hello 2018 ABC
    A. The Way to Home
    Codeforces Round #453 (Div. 2) ABC
    Codeforces Round #452 (Div. 2) D
    Codeforces Round #452 (Div. 2) ABC
    Python 常用技巧
    Codeforces Round #451 (Div. 2) E
    Codeforces Round #451 (Div. 2) D. Alarm Clock
    Codeforces Round #451 (Div. 2) ABC
  • 原文地址:https://www.cnblogs.com/jinjian91/p/11507238.html
Copyright © 2020-2023  润新知