天天看點

LocalDate、 LocalTime、 LocalDateTime以及ZonedDate、 ZonedTime、 ZonedDateTime相關操作

目錄

​​引言​​

​​LocalDate、 LocalTime、 LocalDateTime​​

​​Instant 時間戳​​

​​Duration 和 Period​​

​​Duration:用于計算兩個“時間”間隔​​

​​Period:用于計算兩個“日期”間隔​​

​​DateTimeFormatter​​

​​ZonedDate、 ZonedTime、 ZonedDateTime​​

引言

LocalDate、 LocalTime、 LocalDateTime 是java8中全新的時間API,三個類的執行個體是不可變的對象,分别表示使用 ISO-8601月曆系統的日期、時間、日期和時間。它們提供了簡單的日期或時間,并不包含目前的時間資訊。也不包含與時區相關的資訊

LocalDate、 LocalTime、 LocalDateTime

三個使用同樣的方法擷取目前資料

//擷取目前時間
LocalTime time = LocalTime.now();
//擷取目前日期
LocalDate date = LocalDate.now();
//擷取目前日期和時間
LocalDateTime dateTime = LocalDateTime.now();      

三個使用同樣的方法指定資料

LocalTime time = LocalTime.of(8, 10);
System.out.println(time);//08:10
LocalDate date = LocalDate.of(2018, 12, 24);
System.out.println(date);//2018-12-24
LocalDateTime dateTime = LocalDateTime.of(2018, 12, 12, 12, 12, 12);
System.out.println(dateTime);//2018-12-12T12:12:12      

其他操作(對目前日期/時間操作)

//加減操作以及擷取具體的值
LocalDateTime date = LocalDateTime.of(2018, 12, 12, 12, 12, 12);
LocalDateTime dateTime = date.plusYears(2);// +兩年
System.out.println(dateTime);// 2020-12-12T12:12:12
LocalDateTime dateTime1 = date.minusMonths(10);// -10月
System.err.println(dateTime1);// 2018-02-12T12:12:12
System.out.println(date.getYear());// 2018
System.out.println(date.getMonthValue());// 12
System.out.println(date.getDayOfMonth());// 12
System.out.println(date.getHour());// 12
System.out.println(date.getMinute());// 12
System.out.println(date.getSecond());// 12



//比較兩個localdate對象
LocalDate localDate = LocalDate.of(2018, 2, 12);
LocalDate localDate2 = LocalDate.of(2018, 11, 23);
boolean flag = localDate.isAfter(localDate2);
boolean flag1 = localDate.isBefore(localDate2);
System.out.println(flag);//false
System.out.println(flag1);//true

//判斷是否是閏年
LocalDate localDate = LocalDate.now();
boolean flag = localDate.isLeapYear();
System.out.println(flag);//false      

Instant 時間戳

用于“時間戳”的運算。它是以Unix元年(傳統的設定為UTC時區1970年1月1日午夜時分)開始所經曆的描述進行運算

//java7之前擷取時間戳方式
long time = System.currentTimeMillis();
System.out.println(time);//1561599260726
//java8時間戳擷取
Instant instant = Instant.now();//預設是UTC時區
System.out.println(instant);//2019-06-27T01:42:19.578Z
//帶偏移量運算
OffsetDateTime odTime=instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(odTime);//2019-06-27T09:42:19.578+08:00
//擷取毫秒顯示的格式
long time1= instant.toEpochMilli();
System.out.println(time1);//1561599873015      

Duration 和 Period

Duration:用于計算兩個“時間”間隔

//1.如果時間是時間戳的方式(計算機讀的方式),擷取兩個時間間隔
Instant instant1 = Instant.now();
try {
  Thread.sleep(100);
} catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
Instant instant2 = Instant.now();
//比較之後得到的是一個Duration對象,然後調用對象相應的方法
Duration duration = Duration.between(instant1, instant2);
System.out.println(duration.getSeconds());
System.out.println(duration.toMillis());
// 2.如果時間是時間方式(使用者讀的方法是),擷取兩個時間間隔
LocalTime localTime1 = LocalTime.of(12, 4, 33);
LocalTime localTime2 = LocalTime.of(12, 5, 45);
Duration duration1 = Duration.between(localTime1, localTime2);
System.out.println(duration1.getSeconds());// 72
System.out.println(duration1.toMinutes());// 1
          

Period:用于計算兩個“日期”間隔

LocalDate localDate = LocalDate.now();
LocalDate localDate2 = LocalDate.of(2018, 1, 12);
Period period=Period.between(localDate, localDate2);
System.out.println(period.getDays());      

DateTimeFormatter

日期 時間格式化類,

//1.使用DateTimeFormatter預設提供好的格式對時間格式化,如DateTimeFormatter.ISO_DATE;
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.format(formatter));      
//2.自定義時間格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd");
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.format(formatter));      

ZonedDate、 ZonedTime、 ZonedDateTime

//檢視java8中支援的時區有哪些
Set<String> strings = ZoneId.getAvailableZoneIds();
strings.forEach(System.out::println);      
//擷取一個指定的時區
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
//擷取指定時區的目前時間
LocalDateTime localDateTime = LocalDateTime.now(zoneId);
System.out.println(localDateTime);      
// 擷取帶時區的時間和日期
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
LocalDateTime localDateTime = LocalDateTime.now(zoneId);
ZonedDateTime localDateTime2 = localDateTime.atZone(zoneId);
System.err.println(localDateTime2);//2019-06-27T10:44:53.672+08:00[Asia/Shanghai]