java常用類Time


LocalDate:IOS格式(yyyy-MM-dd)日期

LocalTime:表示一個時間

LocalDateTime:表示時間日期

 

 Instant

  時間線上的瞬時點,可以用來記錄應用程序中的時間時間戳。

  java.time包是基於納秒計算的,所以instant的精度可以達到納秒級。

  java.time包通過值類型instant提供機器視圖,不提供人類意義上的時間單位。

  默認時間戳為格林威治時間。

  可以通過atZone(ZoneId zoneId)和atOffset(ZoneOffset offset)修改為正確時間

 DateTImeFormatter

  預定義標准格式:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME。。。

  本地化相關的格式:ofLocalizedDateTime(FormatStyle.LONG)。。。

  自定義格式:ofPattern(“yyyy-MM-dd hh:mm:ss”)

 

 

 

 

 

 

//ZoneId:類中包含了所有的時區信息 
// ZoneId的getAvailableZoneIds():獲取所有的ZoneId
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
for (String s : zoneIds) {
System.out.println(s);
}
// ZoneId的of():獲取指定時區的時間
LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(localDateTime);
//ZonedDateTime:帶時區的日期時間
// ZonedDateTime的now():獲取本時區的ZonedDateTime對象
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
// ZonedDateTime的now(ZoneId id):獲取指定時區的ZonedDateTime對象
ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(zonedDateTime1);
################################# //Duration:用於計算兩個“時間”間隔,以秒和納秒為基准
LocalTime localTime = LocalTime.now();
LocalTime localTime1 = LocalTime.of(15, 23, 32);
//between():靜態方法,返回Duration對象,表示兩個時間的間隔
Duration duration = Duration.between(localTime1, localTime);
System.out.println(duration);
System.out.println(duration.getSeconds());
System.out.println(duration.getNano()); LocalDateTime localDateTime = LocalDateTime.of(2016, 6, 12, 15, 23, 32);
LocalDateTime localDateTime1 = LocalDateTime.of(2017, 6, 12, 15, 23, 32); Duration duration1 = Duration.between(localDateTime1, localDateTime);
System.out.println(duration1.toDays()); ################################# //Period:用於計算兩個“日期”間隔,以年、月、日衡量
LocalDate localDate = LocalDate.now();
LocalDate localDate1 = LocalDate.of(2028, 3, 18);
Period period = Period.between(localDate, localDate1);
System.out.println(period); System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays()); Period period1 = period.withYears(2);
System.out.println(period1); ################################## // TemporalAdjuster:時間校正器 // 獲取當前日期的下一個周日是哪天?
TemporalAdjuster temporalAdjuster = TemporalAdjusters.next(DayOfWeek.SUNDAY);
LocalDateTime localDateTime = LocalDateTime.now().with(temporalAdjuster);
System.out.println(localDateTime);
// 獲取下一個工作日是哪天?
LocalDate localDate = LocalDate.now().with(new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
LocalDate date = (LocalDate) temporal;
if (date.getDayOfWeek().equals(DayOfWeek.FRIDAY)) {
return date.plusDays(3);
} else if (date.getDayOfWeek().equals(DayOfWeek.SATURDAY)) {
return date.plusDays(2);
} else {
return date.plusDays(1);
}
}
});
System.out.println("下一個工作日是:" + localDate);

 與傳統日期處理的轉換

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM