Java8-java.time-常用API
基于之前博客文章中列举了time的一些基本API,本文主要列举实际开发中需要经常用到的。
1 package com.gzh.time; 2 3 import java.time.DayOfWeek; 4 import java.time.Instant; 5 import java.time.LocalDate; 6 import java.time.LocalDateTime; 7 import java.time.LocalTime; 8 import java.time.Period; 9 import java.time.ZoneId; 10 import java.time.format.DateTimeFormatter; 11 import java.time.temporal.TemporalAdjusters; 12 import java.util.Calendar; 13 import java.util.Date; 14 15 import org.junit.Test; 16 17 public class TimeTest { 18 19 // LocalDate;LocalTime;LocalDateTime 20 @Test 21 public void test() { 22 23 // 获取当前时间:2020-03-08 24 System.out.println(LocalDate.now()); 25 // 获取当前时间:15:20:40.158 26 System.out.println(LocalTime.now()); 27 // 获取当前时间:2020-03-08T15:20:40.158 28 System.out.println(LocalDateTime.now()); 29 30 // DateTimeFormatter---ISO系类转化 31 // BASIC_ISO_DATE :20200308 32 System.out.println("BASIC_ISO_DATE :" + DateTimeFormatter.BASIC_ISO_DATE.format(LocalDateTime.now())); 33 // ISO_DATE :2020-03-08 34 System.out.println("ISO_DATE :" + DateTimeFormatter.ISO_DATE.format(LocalDateTime.now())); 35 // ISO_DATE_TIME :2020-03-08T16:33:27.489 36 System.out.println("ISO_DATE_TIME :" + DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now())); 37 // ISO_LOCAL_DATE :2020-03-08 38 System.out.println("ISO_LOCAL_DATE :" + DateTimeFormatter.ISO_LOCAL_DATE.format(LocalDateTime.now())); 39 // ISO_LOCAL_DATE_TIME :2020-03-08T16:33:27.49 40 System.out.println("ISO_LOCAL_DATE_TIME :" + DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now())); 41 // ISO_LOCAL_TIME :16:33:27.49 42 System.out.println("ISO_LOCAL_TIME :" + DateTimeFormatter.ISO_LOCAL_TIME.format(LocalDateTime.now())); 43 // ISO_ORDINAL_DATE :2020-068 44 System.out.println("ISO_ORDINAL_DATE :" + DateTimeFormatter.ISO_ORDINAL_DATE.format(LocalDateTime.now())); 45 // ISO_TIME :16:33:27.49 46 System.out.println("ISO_TIME :" + DateTimeFormatter.ISO_TIME.format(LocalDateTime.now())); 47 // ISO_WEEK_DATE :2020-W10-7 48 System.out.println("ISO_WEEK_DATE :" + DateTimeFormatter.ISO_WEEK_DATE.format(LocalDateTime.now())); 49 50 // DateTimeFormatter格式转化 51 DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // ISO_DATE;ISO_LOCAL_DATE 52 DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("HH:mm:ss"); 53 DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 54 // 转化日期:2020-03-08 55 System.out.println(dtf1.format(LocalDate.now())); 56 // 转化日期:2020-03-08 57 System.out.println(dtf1.format(LocalDateTime.now())); 58 // 转化日期:15:20:40 59 System.out.println(dtf2.format(LocalTime.now())); 60 // 转化日期:15:20:40 61 System.out.println(dtf2.format(LocalDateTime.now())); 62 // 转化日期:2020-03-08 15:20:40 63 System.out.println(dtf3.format(LocalDateTime.now())); 64 65 LocalDateTime ldt1 = LocalDateTime.of(2020, 12, 12, 0, 0, 0); 66 // LocalDateTime-->LocalDate 67 LocalDate localDate1 = ldt1.toLocalDate(); 68 // LocalDateTime-->LocalTime 69 LocalTime localTime1 = ldt1.toLocalTime(); 70 71 // java.util.Date --> LocalDateTime 72 LocalDateTime ldt2 = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()); 73 // LocalDateTime -->java.util.Date 74 LocalDate localDate2 = LocalDate.now(); 75 LocalTime localTime2 = LocalTime.now(); 76 LocalDateTime localDateTime2 = LocalDateTime.of(localDate2, localTime2); 77 Instant instant = localDateTime2.atZone(ZoneId.systemDefault()).toInstant(); 78 System.out.println(Date.from(instant)); 79 80 // LocalDate常用 81 // 计算时间差--天数差 82 LocalDate localDate3 = LocalDate.of(2008, 1, 1); 83 LocalDate localDate4 = LocalDate.of(2018, 12, 12); 84 // 天数差:3998 85 System.out.println("天数差 :" + (localDate4.toEpochDay()-localDate3.toEpochDay())); 86 87 // 计算当年是否是闰年:isLeapYear() 88 System.out.println("isLeapYear :" + LocalDate.now().isLeapYear()); 89 90 // 计算生日 91 Period betweenDate = Period.between(LocalDate.now(), LocalDate.of(2000, 12, 31)); 92 int age = betweenDate.getYears(); 93 System.out.println(age); 94 95 // 利用时间矫正器TemporalAdjuster 96 LocalDate localDate5 = LocalDate.now(); 97 // LocalDate.with(TemporalAdjuster adjuster):利用TemporalAdjuster的工具类TemporalAdjusters 98 LocalDate firstDayOfMonth = localDate5.with(TemporalAdjusters.firstDayOfMonth()); 99 System.out.println("firstDayOfMonth :"+ firstDayOfMonth); 100 TemporalAdjusters.firstDayOfYear(); 101 TemporalAdjusters.lastDayOfMonth(); 102 TemporalAdjusters.lastDayOfYear(); 103 TemporalAdjusters.firstDayOfNextMonth(); 104 TemporalAdjusters.firstDayOfNextYear(); 105 // 计算某一天的下一个工作日 :@FunctionalInterface--TemporalAdjuster 106 LocalDate nextWorkDay = localDate5.with((time)->{ 107 LocalDate localDate =(LocalDate) time; 108 if(localDate.getDayOfWeek().equals(DayOfWeek.FRIDAY)) { 109 return localDate.plusDays(3); 110 }else if(localDate.getDayOfWeek().equals(DayOfWeek.SATURDAY)){ 111 return localDate.plusDays(2); 112 }else { 113 return localDate.plusDays(1); 114 } 115 116 }); 117 System.out.println("nextWorkDay" + nextWorkDay); 118 } 119 120 }
posted on 2020-03-08 17:43 betterLearing 阅读(433) 评论(0) 编辑 收藏 举报