LocalDateTime轉換Date 日期比較 日期相差天數
阿新 • • 發佈:2021-11-30
LocalDateTime轉換Date 日期比較 日期相差天數
1、LocalDateTime轉Date
// 從預設時區的系統時鐘獲取當前的日期時間 LocalDateTime localDateTime = LocalDateTime.now(); // 將此日期時間與時區相結合以建立 ZonedDateTime ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault()); // 本地時間線LocalDateTime到即時時間線Instant時間戳 Instant instant = zonedDateTime.toInstant(); // UTC時間(世界協調時間,UTC + 00:00)轉北京(北京,UTC + 8:00)時間 Date date = Date.from(instant); System.out.println("localDateTime: " + localDateTime); System.out.println("zonedDateTime: " + zonedDateTime); System.out.println("instant: " + instant); System.out.println("date: " + date);
執行結果:
localDateTime: 2021-11-30T14:50:50.188
zonedDateTime: 2021-11-30T14:50:50.188+08:00[Asia/Shanghai]
instant: 2021-11-30T06:50:50.188Z
date: Tue Nov 30 14:50:50 CST 2021
2、Date轉LocalDateTime
// Date轉LocalDate Date date = new Date(); // 轉為時間戳 Instant instant = date.toInstant(); // UTC時間(世界協調時間,UTC + 00:00)轉北京(北京,UTC + 8:00)時間 LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); System.out.println("date: " + date); System.out.println("instant: " + instant); System.out.println("localDateTime: " + localDateTime);
執行結果:
date:Tue Nov 30 15:28:39 CST 2021
instant:2021-11-30T07:28:39.994Z
localDateTime:2021-11-30T15:28:39.994
3、LocalDateTime格式化日期
(1)日期字串轉LocalDateTime轉Date
// 日期字串轉LocalDateTime String today = "2021-11-30 15:28:39"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 轉為自定義格式 LocalDateTime localDateTime = LocalDateTime.parse(today, dateTimeFormatter); String format = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(localDateTime); System.out.println("localDateTime:" + localDateTime); System.out.println("自定義格式:" + format); // LocalDateTime轉Date Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); System.out.println("date:" + date);
執行結果:
localDateTime:2021-11-30T15:28:39
自定義格式:20211130
date:Tue Nov 30 15:28:39 CST 2021
(2)日期字串轉LocalDate轉Date
// 日期字串轉LocalDate
String tomorrow = "2021-12-01";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(tomorrow, dateFormatter);
System.out.println("localDate:" + localDate);
// LocalDate轉Date,需要使用atStartOfDay
// 因為Date有時間,LocalDate沒有時間,需要LocalDate與00:00:00時間相結合
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
System.out.println("date:" + date);
執行結果:
localDate:2021-12-01
date:Wed Dec 01 00:00:00 CST 2021
4、LocalDate日期比較
LocalDate startDate = LocalDate.of(2021, 11, 30);
LocalDate today = LocalDate.now();
boolean before = startDate.isBefore(today);
boolean after = startDate.isAfter(today);
boolean equal = startDate.isEqual(today);
System.out.println(before ? "開始日期大於當前日期":"開始日期不大於當前日期");
System.out.println(after ? "開始日期大於當前日期":"開始日期不大於當前日期");
System.out.println(equal ? "開始日期等於當前日期":"開始日期不等於當前日期");
執行結果:
開始日期不大於當前日期
開始日期不大於當前日期
開始日期等於當前日期
5、LocalDate日期增加或減少
// 增加天、周、月、年,ChronoUnit標準的日期單位
LocalDate today = LocalDate.now();
LocalDate nextDays = today.minus(1, ChronoUnit.DAYS);
System.out.println(nextDays);
LocalDate nextWeeks = today.plus(1, ChronoUnit.WEEKS);
System.out.println(nextWeeks);
LocalDate nextMonths = today.plus(1, ChronoUnit.MONTHS);
System.out.println(nextMonths);
LocalDate nextYears = today.plus(1, ChronoUnit.YEARS);
System.out.println(nextYears);
執行結果:
2021-11-29
2021-12-07
2021-12-30
2022-11-30
6、兩個日期之間相差的年月日
// 兩個日期之間相差的年月日
LocalDate startDate = LocalDate.of(2020, 11, 30);
LocalDate endDate = LocalDate.of(2021, 12, 30);
long years = startDate.until(endDate, ChronoUnit.YEARS);
long month = startDate.until(endDate, ChronoUnit.MONTHS);
long day = startDate.until(endDate, ChronoUnit.DAYS);
System.out.println("years:" + years);
System.out.println("month:" + month);
System.out.println("day:" + day);
執行結果:
years:1
month:13
day:395