/**
* Instant:瞬時執行個體
* LocalDate:本地日期 不包含具體時間 例如 2020-02-02 可以用來記錄紀念日
* LocalDTime:本地時間 不包含日期
* LocalDateTime:結合了日期和時間 但不包含時差和時區
* ZonedDateTime:完整的日期時間,包含時區和相對UTC或格林威治的視察
*
* ZoneOffSet,ZoneId:操作時區
*
* DateTimeFormatter:格式化時間
*
* LocalDate等許多類為 final 線程安全不可變, plusHour withDay等操作後要用新的對象來接收
*/
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println(today); // 2020-05-30
/* 處理特定日期 */
LocalDate birthday = LocalDate.of(1994,8,11);
System.out.println(birthday); // 1994-08-11
/* 日期比較 */
if (today.equals(birthday)) {
System.out.println("日期形同");
}
/* MonthDay 和 YearMonth 用來檢查某一天是不是生日這種周期性時間 */
MonthDay myBithday = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
MonthDay currentMonDay = MonthDay.from(today);
if (myBithday.equals(currentMonDay)) {
System.out.println("今天是你的生日");
}
/* 在現有時間基礎上囧算之前之後的年、月、周、時、分、秒 */
LocalDateTime nowTime = LocalDateTime.now();
LocalDateTime afterTime = nowTime.plusHours(3);
LocalDateTime beforeTime = nowTime.minusYears(2);
System.out.println("afterTime:"+afterTime+"- beforeTime:"+beforeTime);
// afterTime:2020-05-30T15:09:28.013- beforeTime:2018-05-30T12:09:28.013
/* 在現有時間基礎上修改年、月、周、時、分、秒 */
LocalDateTime localDateTime = nowTime.withHour(11);
System.out.println("with 11 Time:"+localDateTime); // with 11 Time:2020-05-30T11:09:28.013
/* 使用java8的Clock時鐘類擷取時間戳 */
Clock clock = Clock.systemUTC();
System.out.println(clock.millis()); // 1590811768013
System.out.println(System.currentTimeMillis()); // 1590811768013
Clock defalutClock = Clock.systemDefaultZone();
System.out.println(defalutClock.millis()); // 1590811768013
System.out.println(defalutClock.getZone()); // Asia/Shanghai
/* 判斷早于某時還是晚于某時 */
LocalDateTime tomorrow = nowTime.plusDays(1);
if (tomorrow.isEqual(nowTime)) {
//時間一樣
} else if (tomorrow.isAfter(nowTime)) {
//是否在目前之後
} else if (tomorrow.isBefore(nowTime)) {
//之前
} else {
//
}
/* 時區處理 */
ZoneId beijing = ZoneId.of("America/New_York");
LocalDateTime timenow = LocalDateTime.now();
ZonedDateTime zonedDateTime = ZonedDateTime.of(timenow, beijing);
System.out.println("紐約時間" + zonedDateTime);
// 紐約時間2020-05-30T12:22:38.102-04:00[America/New_York]
/* 檢測閏年 */
LocalDate leafYear = LocalDate.now();
if (leafYear.isLeapYear()){
//閏年
}
/* 計算兩個時間之間的天數和月數 */
LocalDate current = LocalDate.now();
LocalDate yourBirthday = LocalDate.of(1996, 8, 11);
Period between = Period.between(yourBirthday, current);
System.out.println(between.getYears()+"年"+between.getMonths()+"月"+between.getDays()+"天");
// 23年9月19天
/* 包含時差資訊的日期和時間
* ZoneOffset 表示時差 印度與GMT或者UTC飙車事件時差+5:30
* */
LocalDateTime dateTime = LocalDateTime.now();
ZoneOffset offset = ZoneOffset.of("+05:30");
OffsetDateTime offsetDateTime = OffsetDateTime.of(dateTime, offset);
System.out.println(offsetDateTime); //2020-05-30T12:48:54.655+05:30
System.out.println(OffsetDateTime.now()); //2020-05-30T12:48:54.655+08:00
/* 格式化時間 */
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(formatter.format(LocalDateTime.now()));//2020-05-30 12:55:40
}