java8 新特性—java.time包 時間處理類
Instant 時間戳
Duration 持續時間、時間差
LocalDate 只包含日期,比如:2018-09-24
LocalTime 只包含時間,比如:10:32:10
LocalDateTime 包含日期和時間,比如:2018-09-24 10:32:10
Peroid 時間段
ZoneOffset 時區偏移量,比如:+8:00
ZonedDateTime 帶時區的日期時間
Clock 時鐘,可用於獲取當前時間戳
java.time.format.DateTimeFormatter 時間格式化類
用例
LocalDate類
構造
LocalDate date = LocalDate.now();
LocalDate date = LocalDate.of(2019, 12, 11);
......
// 本地日期
LocalDate localDate = LocalDate.of(2019, 12, 11);
System.out.println(localDate);
int year = localDate.getYear(); //哪一年
System.out.println("year = " + year);
Month month = localDate.getMonth(); // 第幾個月
System.out.println("month = " + month);
int dayOfMonth = localDate.getDayOfMonth(); //一個月的第幾天
System.out.println("dayOfMonth = " + dayOfMonth);
DayOfWeek dayOfWeek = localDate.getDayOfWeek(); //一週的第幾天
System.out.println("dayOfWeek = " + dayOfWeek);
int lenth = localDate.lengthOfMonth(); // 一個月的天數
System.out.println("lenth = " + lenth);
boolean leapYear = localDate.isLeapYear(); // 是否是閏年
System.out.println("leapYear = " + leapYear);
// 時間加減操作
LocalDate date = LocalDate.now();
LocalDate localDate2 = date.minusDays(5);
LocalDate localDate3 = date.plusYears(10);
date.plus(5, ChronoUnit.DAYS);
......
獲取年月日信息
LocalDate date = LocalDate.now();
System.out.printf("年=%d, 月=%d, 日=%d", date.getYear(), date.getMonthValue(), date.getDayOfMonth());
......
比較兩個日期是否相等
LocalDate now = LocalDate.now();
LocalDate date = LocalDate.of(2019, 12, 11);
System.out.println("日期是否相等=" + now.equals(date));
......
LocalTime類
獲取當前時間時分秒
LocalTime time = LocalTime.now();
......
// 解析日期
String dateText = "20180924";
LocalDate date = LocalDate.parse(dateText, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("格式化之後的日期=" + date);
// 格式化日期
dateText = date.format(DateTimeFormatter.ISO_DATE);
System.out.println("dateText=" + dateText);
......
比較與計算
日期時間計算
// 時間增量
LocalTime time = LocalTime.now();
LocalTime newTime = time.plusHours(2);
System.out.println("newTime=" + newTime);
......
// 日期增量
LocalDate date = LocalDate.now();
LocalDate newDate = date.plus(1, ChronoUnit.WEEKS);
System.out.println("newDate=" + newDate);
......
日期時間比較
LocalDate now = LocalDate.now();
LocalDate date1 = LocalDate.of(2019, 12, 11);
if (now.isAfter(date1)) {
System.out.println("2019年已經過去了");
}
LocalDate date2 = LocalDate.of(2020, 1, 1);
if (now.isBefore(date2)) {
System.out.println("2020年還未到來");
}
......
LocalDateTime類
//LocalDateTime包括LocalDate和LocalTime
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
System.out.println("localDateTime = " + localDateTime);
//LocalDateTime和LocalDate, LocalTime相互轉換
LocalDate localDate1 = localDateTime.toLocalDate();
LocalTime localTime1 = localDateTime.toLocalTime();
......
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 日期時間轉字符串
LocalDateTime now = LocalDateTime.now();
String nowText = now.format(formatter);
System.out.println("nowText=" + nowText);
// 字符串轉日期時間
String datetimeText = "2019-12-11 23:59:59";
LocalDateTime datetime = LocalDateTime.parse(datetimeText, formatter);
System.out.println(datetime);
......
Instant類
// 一個時間戳
Instant instant = Instant.now();
......
Duration類
// 一個時間段
Duration duration = Duration.between(localDateTime, localTime1);
long toDays = duration.toDays(); // 這個時間段中有幾天
long toHours = duration.toHours(); // 這個時間段中有幾個小時
// 通過of創建時間段
Duration duration1 = Duration.of(7, ChronoUnit.DAYS);
......
Period類
// 以年月日來表示時間段
Period period = Period.between(localDate, localDate1);
......
// 上海時間
ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime shanghaiZonedDateTime = ZonedDateTime.now(shanghaiZoneId);
// 東京時間
ZoneId tokyoZoneId = ZoneId.of("Asia/Tokyo");
ZonedDateTime tokyoZonedDateTime = ZonedDateTime.now(tokyoZoneId);
......