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));}
}