• Java8 Time


    LocalDateTime

    1. LocalDateTime.now();
      获取当前时间
    LocalDateTime ldt = LocalDateTime.now();
    

    输出

    2018-06-25T09:43:04.386

    1. LocalDateTime.of()
      获取指定的 LocalDateTime 实例
    LocalDateTime ldt = LocalDateTime.of(2015, 03,21,22,33,22,33);
    
    1. plus
      增加日期
    LocalDateTime ldt = LocalDateTime.now();
    LocalDateTime localDateTime = ldt.plusYears(2).plusDays(1);
    
    1. minus
      减日期
    LocalDateTime localDateTime = ldt.minusMonths(3).minusHours(3);
    
    1. with
      指定年月日
    // 将ldf1 的日指定为3,年指定为2016年
    LocalDateTime localDateTime2 = ldf1.withDayOfMonth(3).withYear(2016);
    
    1. Duration.between
      时间之间的间隔
    Duration between = Duration.between(ldt1, ldt2);
    between.toDays();  // 获取间隔的天数
    between.toHours(); // 获取间隔的小时
    between.toMillis(); // 获取间隔的秒
    between.toMinutes(); // 获取间隔的分钟
    
    1. LocalDateTime 转 String
    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
    String format = dtf.format(now); // 20180625 10:46:25
    
    1. String 转 LocalDateTime
    LocalDateTime parse = LocalDateTime.parse(format1, dtf); // 2018-06-25T10:46:25
    

    LocalDate

    1. Period.between()
      获取连个日期之间的周期
    LocalDate localDate1 = LocalDate.of(2015,05,21);
    LocalDate localDate2 = LocalDate.of(2018,06,25);
    Period period = Period.between(localDate1, localDate2);
    period.getDays();  // 4天
    period.getMonths(); // 1个月
    period.getYears(); // 3年
    
  • 相关阅读:
    [转] 分代垃圾回收的 新旧代引用问题(原标题:Back To Basics: Generational Garbage Collection)
    javascript中数组总结
    mybatis 与 反射
    mybatis 与 缓存
    mybatis 与 日志
    mybatis与 Exception
    mybatis 与 xml
    Redis -- 03 持久化
    Redis -- 02 配置文件解析
    【翻译】Crafting a Next-Gen Material Pipeline for The Order: 1886
  • 原文地址:https://www.cnblogs.com/Godfunc/p/9223055.html
Copyright © 2020-2023  润新知