合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
### 新的日期API ``` @Test public void test() { LocalDateTime now = LocalDateTime.now(); System.out.println(now); System.out.println(now.getYear()); System.out.println(now.getMonth()); System.out.println(now.getDayOfMonth()); System.out.println(now.getHour()); System.out.println(now.getMinute()); System.out.println(now.getSecond()); System.out.println(now.getNano()); // 初始化日期 LocalDateTime localDateTime = LocalDateTime.of(2019,12,10,8,30); System.out.println(localDateTime); // 加10天 localDateTime = localDateTime.plusDays(10); System.out.println(localDateTime); // 减少2天 localDateTime = localDateTime.minusYears(2); System.out.println(localDateTime); } @Test public void test2() { // 时间戳 1970年1月1日00:00:00 到某一个时间点的毫秒值 // 默认获取UTC时区 Instant now = Instant.now(); System.out.println(now); // 时区设置 long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); System.out.println(epochMilli); System.out.println(System.currentTimeMillis()); System.out.println(Instant.now().toEpochMilli()); System.out.println(Instant.now().atOffset(ZoneOffset.ofHours(8)).toInstant().toEpochMilli()); } @Test public void test3() throws InterruptedException { // Duration:计算两个时间之间的间隔 // Period:计算两个日期之间的间隔 Instant start = Instant.now(); Thread.sleep(800); Instant end = Instant.now(); Duration between = Duration.between(start, end); System.out.println(between); System.out.println(between.toMillis()); // 毫秒 System.out.println(between.getSeconds()); // 秒 System.out.println(between.toNanos()); // 纳秒 System.out.println("------------------"); LocalTime start2 = LocalTime.now(); Thread.sleep(800); LocalTime end2 = LocalTime.now(); Duration be = Duration.between(start2, end2); System.out.println(be); System.out.println(be.toMillis()); System.out.println(be.getSeconds()); System.out.println(be.getNano()); } @Test public void test4() throws InterruptedException{ LocalDate end = LocalDate.now(); LocalDate start = LocalDate.of(2018, 5, 25); Period between = Period.between(start, end); System.out.println(between); System.out.println(between.getDays()); // 相隔天数 System.out.println(between.getUnits()); // 单位 System.out.println(between.getMonths()); // 相隔月份 System.out.println(between.getYears()); // 相隔年 System.out.println(between.get(between.getUnits().get(2))); System.out.println(between.getChronology()); } @Test public void test5() { // temperalAdjust 时间校验器 例如获取下周日 下一个工作日 LocalDateTime now = LocalDateTime.now(); System.out.println("本月第一天:"+now.withDayOfMonth(1)); // 本月第一天 System.out.println("本年第一天:"+now.withDayOfYear(1)); // 本年第一天 System.out.println("本年第二月:"+now.withMonth(2)); // 本年第二月 System.out.println("下个星期四:"+now.with(TemporalAdjusters.next(DayOfWeek.THURSDAY))); // 下个星期四 System.out.println("上个星期天:"+now.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY))); // 上个星期天 // LocalDateTime plusDays = now.plusDays(1); // 获取下个工作日 LocalDateTime nextWorkDay = now.with((t)->{ LocalDateTime n = (LocalDateTime)t; DayOfWeek dayOfWeek = n.getDayOfWeek(); if(dayOfWeek.equals(DayOfWeek.FRIDAY)) { n = n.plusDays(3); }else if(dayOfWeek.equals(DayOfWeek.SATURDAY)) { n = n.plusDays(2); }else { n = n.plusDays(1); } return n; }); System.out.println("下个工作日:"+nextWorkDay); } @Test public void test7() { // DateTimeFormatter: 格式化时间/日期 // 自定义格式 LocalDateTime now = LocalDateTime.now(); DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(now.format(ofPattern)); System.out.println(ofPattern.format(now)); DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME; System.out.println(LocalDateTime.now().format(isoDateTime)); LocalDateTime now2 = LocalDateTime.now(); LocalDateTime parse = LocalDateTime.parse("2020-01-02 15:12:22", ofPattern); System.out.println("当前LocalDateTime对象转换成String: "+now2.format(ofPattern)); System.out.println("指定的时间转换成LocalDateTime: "+parse); } // ZoneTime ZoneDate ZoneDateTime @Test public void test8() { LocalDateTime now = LocalDateTime.now(ZoneId.of("America/Chicago")); System.out.println(now); LocalDateTime now2 = LocalDateTime.now(); ZonedDateTime atZone = now2.atZone(ZoneId.of("Asia/Shanghai")); System.out.println(atZone); Set<String> s = ZoneId.getAvailableZoneIds(); s.forEach(System.out::println); } @Test public void test9() { // 5月第一个星期天 LocalDate day = LocalDate.parse("2020-05-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)); System.out.println(day); // 本月第三个星期五 LocalDate day2 = LocalDate.now().with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.FRIDAY)); System.out.println(day2); } ``` ***** 原文链接:https://blog.csdn.net/qq_29411737/article/details/80835658