jdk1.8时间API

 一、说明
    Java 8 在java.time包下推出了一组全新的时间日期API,涵盖了日期、时间、日期时间、时区、时刻、间隔、时钟等
     新的java.time包下的所有类都是不可变类型而且线程安全的,解决了在此之前日期时间中存在的线程安全、横跨多包、使用复杂等诸多问题
二、示例
    1、时间和日期

 // 本地时间
 LocalTime lt = LocalTime.now();
 
 // 本地日期
 LocalDate ld = LocalDate.now();
 
 // 本地日期时间
 LocalDateTime ldt = LocalDateTime.now();
 
 // 创建一个指定的时间
 LocalDateTime ldt = LocalDateTime.of(2012, 2, 12, 12, 12, 12);
 
 // 日期时间转日期或时间
 LocalDate ld = ldt.toLocalDate();
 LocalTime lt = ldt.toLocalTime();
 
  2、字符串互转

 // 格式化模版
 DateTimeFormatter DATETIME19 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
 // 时间转字符串
 String dtStr = DATETIME19.format(LocalDateTime.now());
 
 // 字符串转时间
 LocalDateTime ldt = LocalDateTime.parse(dtStr, DATETIME19);
 
3.时间运算

 //获取指定单位的值
 int year = ldt.getYear();
 int day = ldt.getDayOfMonth();
 int week = ldt.getDayOfWeek().getValue();
 int hour = ldt.getHour();
 
 // 指定时间单位的值
 LocalDateTime ldt2 = ldt1.withDayOfMonth(10).withYear(2020);  // 返回的是一个新的对象,需要接收
 
 // 在现有时间上做加法
 LocalDateTime ldt2 = ldt1.plusYears(2).plusMonths(-2);
 
 // 在现有时间上做减法
 LocalDateTime ldt2 = LocalDateTime.now().minus(-2, ChronoUnit.MONTHS).minusDays(3)
 
 // 获取一天的开始或结束
 LocalDateTime ldtStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
 LocalDateTime ldtEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
 
 // 时间是否在指定时间之前
 boolean isBefore = ldt1.isBefore(ldt2);
 
 // 时间是否在指定时间之后
 boolean isAfter = ldt1.isAfter(ldt2);
 
 // 比较两个日期是否相等 重写的equals可以直接比较
 boolean equality = ld1.equals(ld2);
 
 // 比较是否是周期性日期,比如 生日 节假日 账单日 等
 MonthDay holiday = MonthDay.of(5, 1); // 五一
 boolean equality = holiday.equals(MonthDay.from(LocalDateTime.now())); // 今天是否是五一
 
4、间隔计算

 LocalDateTime ldt1 = LocalDateTime.of(2012, 2, 12, 12, 12, 12);
 LocalDateTime ldt2 = LocalDateTime.of(2015, 5, 15, 15, 15, 15);
 
 // 时间的间隔    Duration 表示时分秒的时间量(累计向上单位的差,即计算实际的总共的差)
 Duration duration = Duration.between(ldt1, ldt2);
 long durnMill = duration.toMillis();   // 计算毫秒差
 long durnMin = duration.toMinutes();   // 计算分钟差
 long durnHour = duration.toHours();    // 计算小时差
 long durnDay = duration.toDays();      // 计算天数差
 
 // 日期的间隔    Period 表示年月日的时间量(只计算当前单位的差,不累计向上单位的差距)
 Period period = Period.between(ldt1.toLocalDate(), ldt2.toLocalDate());
 long perdDay = period.getDays();        // 只计算当前差,不累计年月差带来的天数差
 long perdMonth = period.getMonths();    // 只计算当前差,不累计年数差带来的月数差
 long perdYear = period.getYears();
 
 // 计算实际总间隔天数的第二种方法
 long diffEehDay =ldt1.toLocalDate().toEpochDay() - ldt2.toLocalDate().toEpochDay();
 
 // 计算指定时间单位之差
 long diffChrDay =ChronoUnit.DAYS.between(ldt1, ldt2);    // 日期单位之差
 long diffChrMin =ChronoUnit.MINUTES.between(ldt1, ldt2); // 分钟单位之差
 
5、时间戳、瞬时点、Date、本地时间、转换

 // 时间戳
 long timestamp = System.currentTimeMillis();
 
 // 瞬时点
 Instant instant = Instant.now();
 
 // Date
 Date  date =  new Date();
 
 // 时间戳 转 瞬时点
 Instant instant = Instant.ofEpochMilli(timestamp);
 
 // 瞬时点 转 时间戳
 long timestamp = instant.toEpochMilli();
 
 // Date 转 瞬时点
 Instant instant = date.toInstant();
 
 // 瞬时点 转 Date
 Date date = Date.from(instant);
 
 // 瞬时点 转 本地时间
 LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
 
 // 本地时间 转 时间戳
 long timestamp = ldt.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
 long timestamp = ldt.toInstant(ZoneOffset.of("+08:00")).toEpochMilli();
 long timestamp = ldt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
 
6、时区时间

    // 时区ID的集合
    Set<String> zoneSet = ZoneId.getAvailableZoneIds();
 
    // 默认时区
    ZoneId zoneId = ZoneId.systemDefault();
 
    // 时区时间
    LocalDateTime cur = LocalDateTime.now();                                    // 本地默认时间 2019-04-29T14:45:07.156
    LocalDateTime ldt = LocalDateTime.now(ZoneId.of("America/Los_Angeles"));    // 时区当前时间 2019-04-28T23:45:07.156
    OffsetDateTime odt = OffsetDateTime.now(ZoneId.of("America/Los_Angeles"));  // 带偏移量时间 2019-04-28T23:45:07.156-07:00
    ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));    // 带时区的时间 2019-04-28T23:45:07.156-07:00[America/Los_Angeles]
    LocalDateTime ldto = odt.toLocalDateTime();                                 // 转本地类时间 2019-04-28T23:45:07.156
    LocalDateTime ldtz = zdt.toLocalDateTime();                                 // 转本地类时间 2019-04-28T23:45:07.156
   
    // 时钟 类似时间戳
    Clock clockDefault = Clock.systemDefaultZone();               //系统默认
    Clock clockUtc = Clock.systemUTC();                           // UTC
    Clock c1ockZone = Clock.system(ZoneId.of("+08:00"));          //指定时区
    Clock clockRegion = Clock.system(ZoneId.of("Asia/Shanghai")); //指定区域
    long timestamp = clockDefault.millis();                       //获取时间戳,等于System.currentTimeMillis()
 
 
原文链接:https://blog.csdn.net/guoxilen/article/details/89671698

猜你喜欢

转载自www.cnblogs.com/superming/p/12693697.html