JDK8以后新增的時間相關類
Date類 | ZoneId:時區 |
Instant:時間戳 | |
ZoneDateTime:帶時區的時間 | |
日期格式化類 SimpleDateFormat | DateTimeFormatter:用于時間的格式化和解析 |
日歷類 Calendar | LocalDate:年、月、日 |
LocalTime:時、分、秒 | |
LocalDateTime:年、月、日、時、分、秒 | |
工具類 | Period:時間間隔(年,月,日) |
Duration:時間間隔(秒,納秒) | |
ChronoUnit:時間間隔(所有單位) |
優勢
代碼層面:代碼更加簡單
安全層面:規定時間日期對象不可變,修改后的值記錄在一個新的變量中,解決了多線程環境下數據安全問題
Zoneld時區
常見方法:
方法名 | 說明 |
static Set<String> getAvailableZoneIds?() | 獲取Java中支持的所有時區 |
static ZoneId systemDefault () | 獲取系統默認時區 |
static ZoneId of (String zoneId) | 獲取一個指定時區? |
代碼演示:
import java.time.ZoneId;
import java.util.Set;public class Test6 {public static void main(String[] args) {//獲取Java中支持的所有時區Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();//Java中支持600個時區System.out.println(availableZoneIds.size());//600//獲取系統默認時區ZoneId zi1 = ZoneId.systemDefault();System.out.println(zi1);//Asia/Shanghai//獲取一個指定時區ZoneId zi2 = ZoneId.of("Asia/Taipei");System.out.println(zi2);//Asia/Taipei}
}
Instant時間戳
常見方法:
方法名 | 說明 |
static Instant now () | 獲取當前時間的Instant對象 (標準時間) |
static Instant ofXxxx (long epochMilli) | 根據(秒/毫秒/納秒)獲取Instant對象 |
ZonedDateTime atZone (ZoneId zone) | 指定時區 |
boolean isXxx (InstantotherInstant) | 判斷時間系列的方法 |
Instant minusXxx (long millisToSubtract) | 減少時間系列的方法 |
Instant plusXxx (long millisToSubtract) | 增加時間系列的方法 |
代碼演示:
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class Test7 {public static void main(String[] args) {//獲取當前時間的Instant對象Instant it1 = Instant.now();System.out.println(it1);//2025-04-25T08:57:35.873433800Z//根據(秒/毫秒/納秒)獲取Instant對象//秒Instant it2 = Instant.ofEpochSecond(1L);System.out.println(it2);//1970-01-01T00:00:01Z//毫秒Instant it3 = Instant.ofEpochMilli(1000L);System.out.println(it3);//1970-01-01T00:00:01Z//納秒Instant it4 = Instant.ofEpochSecond(1, 1000000000);System.out.println(it4);//1970-01-01T00:00:02Z//指定時區,中國在時間原點上加8小時Instant it5 = Instant.ofEpochMilli(0L);ZoneId zi = ZoneId.of("Asia/Shanghai");ZonedDateTime zdt = it5.atZone(zi);System.out.println(zdt);//1970-01-01T08:00+08:00[Asia/Shanghai]//判斷時間系列的方法Instant it6 = Instant.ofEpochMilli(1000L);Instant it7 = Instant.ofEpochMilli(2000L);boolean flag1 = it6.isBefore(it7);System.out.println(flag1);//trueboolean flag2 = it6.isAfter(it7);System.out.println(flag2);//false//減少時間系列的方法Instant it8 = Instant.ofEpochMilli(3000L);System.out.println(it8);//1970-01-01T00:00:03Z//減1000毫秒Instant it9 = it8.minusMillis(1000L);System.out.println(it9);//1970-01-01T00:00:02Z//減1秒Instant it10 = it8.minusSeconds(1L);System.out.println(it10);//1970-01-01T00:00:02Z//減1000000000納秒Instant it11 = it8.minusNanos(1000000000L);System.out.println(it11);//1970-01-01T00:00:02Z//增加時間系列的方法Instant it12 = Instant.ofEpochMilli(0L);System.out.println(it12);//1970-01-01T00:00:00Z//加1000毫秒Instant it13 = it12.plusMillis(1000L);System.out.println(it13);//1970-01-01T00:00:01Z//加1秒Instant it14 = it12.plusSeconds(1L);System.out.println(it14);//1970-01-01T00:00:01Z//加1000000000納秒Instant it15 = it12.plusNanos(1000000000L);System.out.println(it15);//1970-01-01T00:00:01Z}
}
ZoneDateTime帶時區的時間
常見方法:
方法名 | 說明 |
static ZonedDateTime now () | 獲取當前時間的ZonedDateTime對象 |
static ZonedDateTime ofXxxx (......) | 獲取指定時間的ZonedDateTime對象 |
ZonedDateTime withXxx (時間) | 修改時間系列的方法 |
ZonedDateTime minusXxx (時間) | 減少時間系列的方法 |
ZonedDateTime plusXxx (時間) | 增加時間系列的方法 |
代碼演示:
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class Test8 {public static void main(String[] args) {//獲取當前時間(帶時區)ZonedDateTime zdt1 = ZonedDateTime.now();System.out.println(zdt1);//2025-04-25T17:26:50.183160400+08:00[Asia/Shanghai]//獲取指定時間//年、月、日、時、分、秒、納秒方式指定ZonedDateTime zdt2 = ZonedDateTime.of(2020, 10, 10,10, 10, 10, 0,ZoneId.of("Asia/Shanghai"));System.out.println(zdt2);//2020-10-10T10:10:10+08:00[Asia/Shanghai]//Instant+時區指定Instant it = Instant.ofEpochMilli(0L);ZoneId zi = ZoneId.of("Asia/Shanghai");ZonedDateTime zdt3 = ZonedDateTime.ofInstant(it, zi);System.out.println(zdt3);//1970-01-01T08:00+08:00[Asia/Shanghai]//修改時間系列的方法//例如:修改年份為2021ZonedDateTime zdt4 = zdt3.withYear(2021);System.out.println(zdt4);//2021-01-01T08:00+08:00[Asia/Shanghai]//同理,其他字段也可以修改//減少時間系列的方法//例如:減少1小時ZonedDateTime zdt5 = zdt3.minusHours(1);System.out.println(zdt5);//1970-01-01T07:00+08:00[Asia/Shanghai]//增加時間系列的方法//例如:增加1年ZonedDateTime zdt6 = zdt3.plusYears(1);System.out.println(zdt6);//1971-01-01T08:00+08:00[Asia/Shanghai]}
}
DateTimeFormatter時間的格式化和解析
常見方法:
方法名 | 說明 |
static DateTimeFormatter ofpattern (格式) | 獲取格式對象 |
string format (時間對象) | 按照指定方式格式化 |
代碼演示:
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;public class Test9 {public static void main(String[] args) {//static DateTimeFormatter ofpattern (格式) 獲取格式對象//string format (時間對象) 按照指定方式格式化//創建帶時區的時間對象ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0L),ZoneId.of("Asia/Shanghai"));//獲取格式對象DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//按照指定方式格式化String str = dtf.format(zdt);System.out.println(str);//1970-01-01 08:00:00}
}
LocalDate、LocalTime、LocalDateTime
常見方法:
方法 | 說明 |
static xxx now () | 獲取當前時間的對象 |
static xxx of (......) | 獲取指定時間的對象 |
get開頭的方法 | 獲取日歷中的年、月、日、 時、分、秒等信息 |
isBefore, isAfter | 比較兩個時間 |
with開頭的方法 | 修改時間系列方法 |
minus開頭的方法 | 減少時間系列方法 |
plus開頭的方法 | 增加時間系列方法 |
public LocalDate toLocalDate () | LocalDateTime轉換成一個LocalDate對象 |
public LocalTime toLocalTime () | LocalDateTime轉換成一個LocalTime對象 |
代碼演示:
LocalDate:(年,月,日)
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;public class Test10 {public static void main(String[] args) {//1.獲取當前時間的日歷對象(包含 年月日)LocalDate nowDate = LocalDate.now();System.out.println("今天的日期:" + nowDate);//今天的日期:2025-04-25//2.獲取指定的時間的日歷對象LocalDate ldDate = LocalDate.of(2023, 1, 1);System.out.println("指定日期:" + ldDate);//指定日期:2023-01-01//3.get系列方法獲取日歷中的每一個屬性值//獲取年int year = ldDate.getYear();System.out.println("year: " + year);//year: 2023//獲取月//方式一:Month m = ldDate.getMonth();System.out.println(m);//JANUARYSystem.out.println(m.getValue());//1//方式二:int month = ldDate.getMonthValue();System.out.println("month: " + month);//month: 1//獲取日int day = ldDate.getDayOfMonth();System.out.println("day:" + day);//day:1//獲取一年的第幾天int dayofYear = ldDate.getDayOfYear();System.out.println("dayOfYear:" + dayofYear);//dayOfYear:1//獲取星期DayOfWeek dayOfWeek = ldDate.getDayOfWeek();System.out.println(dayOfWeek);//SUNDAYSystem.out.println(dayOfWeek.getValue());//7//is開頭的方法表示判斷System.out.println(ldDate.isBefore(ldDate));//falseSystem.out.println(ldDate.isAfter(ldDate));//false//with開頭的方法表示修改,只能修改年月日LocalDate withLocalDate = ldDate.withYear(2000);System.out.println(withLocalDate);//2000-01-01//minus開頭的方法表示減少,只能減少年月日LocalDate minusLocalDate = ldDate.minusYears(1);System.out.println(minusLocalDate);//2022-01-01//plus開頭的方法表示增加,只能增加年月日LocalDate plusLocalDate = ldDate.plusDays(1);System.out.println(plusLocalDate);//2023-01-02}
}
LocalTime:(時、分、秒)
import java.time.LocalTime;public class Test11 {public static void main(String[] args) {// 獲取本地時間的日歷對象。(包含 時分秒)LocalTime nowTime = LocalTime.now();System.out.println("今天的時間:" + nowTime);//今天的時間:18:34:24.363704100int hour = nowTime.getHour();//時System.out.println("hour: " + hour);//hour: 18int minute = nowTime.getMinute();//分System.out.println("minute: " + minute);//minute: 34int second = nowTime.getSecond();//秒System.out.println("second:" + second);//second:24int nano = nowTime.getNano();//納秒System.out.println("nano:" + nano);//nano:363704100//指定時分System.out.println(LocalTime.of(8, 20));//08:20//指定時分秒System.out.println(LocalTime.of(8, 20, 30));//08:20:30//時分秒納秒LocalTime mTime = LocalTime.of(8, 20, 30, 150);System.out.println(mTime);//08:20:30.000000150//is系列的方法System.out.println(nowTime.isBefore(mTime));//falseSystem.out.println(nowTime.isAfter(mTime));//true//with系列的方法,只能修改時、分、秒System.out.println(mTime.withHour(10));//10:20:30.000000150//plus系列的方法,只能修改時、分、秒System.out.println(mTime.plusHours(10));//18:20:30.000000150}
}
LocalDateTime:(年、月、日、時、分、秒)
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;public class Test12 {public static void main(String[] args) {// 當前時間的的日歷對象(包含年月日時分秒)LocalDateTime nowDateTime = LocalDateTime.now();System.out.println("今天是:" + nowDateTime);//今天是:2025-04-25T18:40:32.776723800//年System.out.println(nowDateTime.getYear());//2025//月System.out.println(nowDateTime.getMonthValue());//4//日System.out.println(nowDateTime.getDayOfMonth());//25//時System.out.println(nowDateTime.getHour());//18//分System.out.println(nowDateTime.getMinute());//40//秒System.out.println(nowDateTime.getSecond());//32//納秒System.out.println(nowDateTime.getNano());//776723800//日:當年的第幾天System.out.println("dayofYear:" + nowDateTime.getDayOfYear());//dayofYear:115//星期System.out.println(nowDateTime.getDayOfWeek());//FRIDAYSystem.out.println(nowDateTime.getDayOfWeek().getValue());//5//月份System.out.println(nowDateTime.getMonth());//APRILSystem.out.println(nowDateTime.getMonth().getValue());//4//轉化LocalDate ld = nowDateTime.toLocalDate();System.out.println(ld);//2025-04-25LocalTime lt = nowDateTime.toLocalTime();System.out.println(lt.getHour());//18System.out.println(lt.getMinute());//40System.out.println(lt.getSecond());//32}
}
工具類
作用:
Period:用于計算兩個“日期”間隔(年、月、日)
Duration:用于計算兩個“時間”間隔(秒,納秒)
ChronoUnit:用于計算兩個“日期”間隔
代碼演示:
Period:
import java.time.LocalDate;
import java.time.Period;public class Test13 {public static void main(String[] args) {//當前本地 年月日LocalDate today = LocalDate.now();System.out.println(today);//2025-04-25//生日的 年月日LocalDate birthDate = LocalDate.of(2000, 1, 1);System.out.println(birthDate);//2000-01-01Period period = Period.between(birthDate, today);//第二個參數減第一個參數System.out.println("相差的時間間隔對象:" + period);//相差的時間間隔對象:P25Y3M24DSystem.out.println(period.getYears());//25System.out.println(period.getMonths());//3System.out.println(period.getDays());//24//間隔總月份System.out.println(period.toTotalMonths());//303}
}
Duration:
import java.time.Duration;
import java.time.LocalDateTime;public class Test14 {public static void main(String[] args) {//本地日期時間對象。LocalDateTime today = LocalDateTime.now();System.out.println(today);//2025-04-25T18:56:24.858802500//出生的日期時間對象LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1, 0, 0, 0);System.out.println(birthDate);//2000-01-01T00:00Duration duration = Duration.between(birthDate, today);//第二個參數減第一個參數System.out.println("相差的時間間隔對象:" + duration);//相差的時間間隔對象:PT221922H56M24.8588025S//兩個時間差的天數System.out.println(duration.toDays());//9246//兩個時間差的小時數System.out.println(duration.toHours());//221922//兩個時間差的分鐘數System.out.println(duration.toMinutes());//13315376//兩個時間差的毫秒數System.out.println(duration.toMillis());//798922584858//兩個時間差的納秒數System.out.println(duration.toNanos());//798922584858802500}
}
ChronoUnit:(最常用)
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;public class Test15 {public static void main(String[] args) {//當前時間LocalDateTime today = LocalDateTime.now();System.out.println(today);//2025-04-25T19:03:48.048897900//生日時間LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1,0, 0, 0);System.out.println(birthDate);//2000-01-01T00:00System.out.println("相差的年數:" + ChronoUnit.YEARS.between(birthDate, today));System.out.println("相差的月數:" + ChronoUnit.MONTHS.between(birthDate, today));System.out.println("相差的周數:" + ChronoUnit.WEEKS.between(birthDate, today));System.out.println("相差的天數:" + ChronoUnit.DAYS.between(birthDate, today));System.out.println("相差的時數:" + ChronoUnit.HOURS.between(birthDate, today));System.out.println("相差的分數:" + ChronoUnit.MINUTES.between(birthDate, today));System.out.println("相差的秒數:" + ChronoUnit.SECONDS.between(birthDate, today));System.out.println("相差的毫秒數:" + ChronoUnit.MILLIS.between(birthDate, today));System.out.println("相差的微秒數:" + ChronoUnit.MICROS.between(birthDate, today));System.out.println("相差的納秒數:" + ChronoUnit.NANOS.between(birthDate, today));System.out.println("相差的半天數:" + ChronoUnit.HALF_DAYS.between(birthDate, today));System.out.println("相差的十年數:" + ChronoUnit.DECADES.between(birthDate, today));System.out.println("相差的世紀(百年)數:" + ChronoUnit.CENTURIES.between(birthDate, today));System.out.println("相差的千年數:" + ChronoUnit.MILLENNIA.between(birthDate, today));System.out.println("相差的紀元數:" + ChronoUnit.ERAS.between(birthDate, today));}
}