前面我们学习了JDK7的时间类,下面我们来继续学习一下JDK8的时间类吧。
下面我们一个一个来学习吧:
Data时间类
- ZoneId:时区
- Instant:时间戳
- ZoneDateTime:带时区的时间
ZoneId时区
| 方法名 | 说明 |
|---|---|
| static Set getAvailableZoneIds() | 获取Java中支持的所有时区 |
| static ZoneId systemDefault() | 获取系统默认时区 |
| static ZoneId of(String zoneId) | 获取一个指定时区 |
获取所有时区
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
System.out.println(zoneIds);
System.out.println(zoneIds.size()); // 时区长度
我们来打印看一下吧:
是不是全部都打印出来了呀,在Java中一共有599个时区。
获取系统默认时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
是不是就把我们现在所在的时区给打印出来了呀。接着看下一个:
获取一个指定时区
ZoneId zoneid1 = ZoneId.of("America/Cuiaba");
System.out.println(zoneid1);
是不是就能指定某一个时区了呀,下面我们来学习一下时间戳吧:
Instant时间戳
| 方法名 | 说明 |
|---|---|
| static Instant now() | 获取当前时间的Instant对象(标准时间) |
| static Instant ofXxxx(long epochMilli) | 根据(秒/毫秒/纳秒)获取Instant对象 |
| ZonedDateTime atzone(ZoneId zone) | 指定时区 |
| boolean isXxx(Instant otherInstant) | 判断系列的方法 |
| Instant minusXxx(long millisToSubtract) | 减少时间系列的方法 |
| Instant plusXxx(long millisToSubtract) | 增加时间系列的方法 |
获取当前时间的Instant对象(标准时间)
Instant now = Instant.now();
System.out.println(now);
根据(秒/毫秒/纳秒)获取Instant对象
Instant instant1 = Instant.ofEpochMilli(0L);
System.out.println(instant1); // 毫秒
Instant instant2 = Instant.ofEpochSecond(1);
System.out.println(instant2); // 秒
指定时区
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(time);
判断系列的方法
Instant instant3 = Instant.ofEpochMilli(0L);
Instant instant4 = Instant.ofEpochMilli(1000L);
boolean result = instant3.isBefore(instant4);
System.out.println(result); // true 表示instant3在instant4前面,返回true
boolean result1 = instant3.isAfter(instant4);
System.out.println(result1); // false 表示instant3不在instant4的后面,返回false
isBefore:判断调用者代表的时间是否在参数表示时间的前面
isAfter:判断调用者代表的时间是否在参数表示时间的后面
减少时间系列的方法
Instant instant5 = Instant.ofEpochMilli(3000L);
System.out.println(instant5); // 1970-01-01T00:00:03
Instant instant6 = instant5.minusSeconds(1); // 减少1秒
System.out.println(instant6); // 1970-01-01T00:00:02
增加时间系列的方法
Instant instant7 = Instant.ofEpochMilli(3000L);
System.out.println(instant7); // 1970-01-01T00:00:03
Instant instant8 = instant5.plusSeconds(1); // 增加1秒
System.out.println(instant8); // 1970-01-01T00:00:04
ZoneDateTime带时区时间
| 方法名 | 说明 |
|---|---|
| static ZonedDateTime now() | 获取当前时间的ZonedDateTime对象 |
| static ZonedDateTime ofXxxx() | 获取指定时间的ZonedDateTime对象 |
| ZonedDateTime withXxx(时间) | 修改时间系列的方法 |
| ZonedDateTime minusXxx(时间) | 减少时间系列的方法 |
| ZonedDateTime plusXxx(时间) | 增加时间系列的方法 |
获取当前时间的ZonedDateTime对象
// 1.获取当前时间对象(带时区)
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
获取指定时间的ZonedDateTime对象
// 2.获取指定的时间对象(带时区)
// 年月日分秒纳秒方法指定
ZonedDateTime time1 = ZonedDateTime.of(2023,10,1,11,11,11,0, ZoneId.of("Asia/Shanghai"));
System.out.println(time1);
还可以利用Instant + 时区的方式指定获取时间对象
Instant instant = Instant.ofEpochMilli(0L); // 先获取一个instant对象并指定毫秒值
ZoneId zondId = ZoneId.of("Asia/Shanghai"); // 获取一个zondId对象并指定地区
ZonedDateTime time2 = ZonedDateTime.ofInstant(instant,zondId);
System.out.println(time2);
修改时间系列的方法
// 3.修改时间系列的方法
ZonedDateTime time3 = time2.withYear(2000); // 修改年份
ZonedDateTime time4 = time2.withMonth(11); // 修改月份
ZonedDateTime time5 = time2.withDayOfYear(30); // 修改日期
System.out.println(time3);
System.out.println(time4);
System.out.println(time5);
减少时间系列的方法
// 4.减少时间系列的方法
ZonedDateTime time6 = time2.minusYears(1); // 减少年份
ZonedDateTime time7 = time2.minusMonths(1); // 减少月份
ZonedDateTime time8 = time2.minusDays(3); // 减少日期
System.out.println(time6);
System.out.println(time7);
System.out.println(time8);
增加时间系列的方法
// 5.增加时间系列的方法
ZonedDateTime time9 = time2.plusYears(1); // 增加年份
ZonedDateTime time10 = time2.plusMonths(1); // 增加月份
ZonedDateTime time11 = time2.plusDays(3); // 增加日期
System.out.println(time9);
System.out.println(time10);
System.out.println(time11);
SimpleDateFormat日期类
- DateTimeFormatter:用于时间的格式化和解析
DateTimeFormatter格式化和解析
| 方法名 | 说明 |
|---|---|
| static DateTimeFormatter ofPattern(格式) | 获取格式对象 |
| String format(时间对象) | 按照指定方式格式化 |
// 1.获取时间对象
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
// 2.解析/格式化
DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");
// 3.格式化
System.out.println(df1.format(time));
我们来运行看一下:
是不是和我们指定的格式一模一样呀!
Calendar日历类
- LocalDate:年、月、日
- LocalTime:时、分、秒
- LocalDateTime:年、月、日、时、分、秒
| 方法名 | 说明 |
|---|---|
| static xxX now() | 获取当前时间的对象 |
| static XXX of() | 获取指定时间的对象 |
| get开头的方法 | 获取日历中的年、月、日、时、分、秒等信息 |
| isBefore,isAfter | 比较两个 LocalDate |
| with开头的 | 修改时间系列的方法 |
| minus开头的 | 减少时间系列的方法 |
| plus开头的 | 增加时间系列的方法 |
下面我们来实操一下吧:
LocalDate
获取当前日历
//1.获取当前时间的日历对象(包含 年月日)
LocalDate nowDate = LocalDate.now();
System.out.println("今天的日期:" + nowDate);
获取指定的日历对象
//2.获取指定的时间的日历对象
LocalDate ldDate = LocalDate.of(2023,1,1);
System.out.println("指定日期:"+ldDate);
get方法获取日历中的每一个属性值
//3.get系列方法获取日历中的每一个属性值
//获取年
int year = ldDate.getYear();
System.out.println("year:" + year);
//获取月
int month = ldDate.getMonthValue();
System.out.println("month:"+ month);
//获取日
int day = ldDate.getDayOfMonth();
System.out.println("day:"+ day);
//获取一年的第几天
int dayofYear = ldDate.getDayOfYear();
System.out.println("dayofYear:"+ dayofYear);
//获取星期
DayOfWeek dayOfWeek= ldDate.getDayOfWeek();
System.out.println(dayOfWeek.getValue());
is开头的方法表示判断
//is开头的方法表示判断
System.out.println(ldDate.isBefore(ldDate));
System.out.println(ldDate.isAfter(ldDate));
with开头的方法表示修改,只能修改年月日
//with开头的方法表示修改,只能修改年月日
LocalDate withLocalDate = ldDate.withYear(2000);
System.out.println(withLocalDate);
minus开头的方法表示减少,只能减少年月日
//minus开头的方法表示减少,只能减少年月日
LocalDate minusLocalDate=ldDate.minusYears(1);
System.out.println(minusLocalDate);
plus开头的方法表示增加,只能增加年月日
//plus开头的方法表示增加,只能增加年月日
LocalDate plusLocalDate=ldDate.plusDays(1);
System.out.println(plusLocalDate);
LocalTime
获取当前时间
// 获取本地时间的日历对象。(包含 时秒)
LocalTime nowTime =LocalTime.now();
System.out.println("今天的时间:"+nowTime);
指定时间
// 指定时间
System.out.println(LocalTime.of(8,10)); // 时分
System.out.println(LocalTime.of(8,10,30)); // 时分秒
System.out.println(LocalTime.of(8,10,30,150)); // 时分秒纳秒
get方法获取时间的每一个属性值
int hour =nowTime.getHour();//时
System.out.println("hour:"+ hour);
int minute =nowTime.getMinute();//分
System.out.println("minute:"+minute);
int second =nowTime.getSecond();//秒
System.out.println("second:"+second);
int nano =nowTime.getNano();//纳秒
System.out.println("nano:"+nano);
is系列判断
LocalTime mTime = LocalTime.of(8,10,30,150);
//is系列的方法
System.out.println(nowTime.isBefore(mTime));
System.out.println(nowTime.isAfter(mTime));
with方法修改
//with系列的方法,只能修改时、分、秒
System.out.println(nowTime.withHour(10));
minus方法减少
//minus系列的方法,只能减少时、分、秒
System.out.println(nowTime.minusHours(10));
plus方法增加
//plus系列的方法,只能增加时、分、秒
System.out.println(nowTime.plusHours(10));
LocalDateTime
获取当日期和时间
// 当前时间的的日历对象(包含 年月日时分秒)
LocalDateTime nowDateTime =LocalDateTime.now();
System.out.println("今天是:"+ nowDateTime);//今天是:
get方法获取每一个属性
System.out.println(nowDateTime.getYear());//年
System.out.println(nowDateTime.getMonthValue());//月
System.out.println(nowDateTime.getDayOfMonth());//日
System.out.println(nowDateTime.getHour());//时
System.out.println(nowDateTime.getMinute());//分
System.out.println(nowDateTime.getSecond());//秒
System.out.println(nowDateTime.getNano());//纳秒
工具类
- Period:用于计算两个“日期”间隔(年、月、日)
- Duration:用于计算两个“时间”间隔(秒,纳秒)
- ChronoUnit:用于计算两个“日期”间隔
Period
获取当前日历对象
// 当前本地 年月日
LocalDate today =LocalDate.now();
System.out.println(today);
定义第二个日期时间对象
// 生日的 年月日
LocalDate birthDate=LocalDate.of(2000,1,1);
System.out.println(birthDate);
Period period = Period.between(birthDate,today);//第二个参数减第一个参数
通过get方法获取
System.out.println("相差的时间间隔对象:"+ period);
System.out.println(period.getYears()); // 年
System.out.println(period.getMonths()); // 月
System.out.println(period.getDays()); // 日
计算间隔的月份
System.out.println(period.toTotalMonths()); // 间隔月份
Duration
获取当前时间对象
//本地日期时间对象。
LocalDateTime today=LocalDateTime.now();
System.out.println(today);
定义第二个日期时间对象
//出生的日期时间对象
LocalDateTime birthDate = LocalDateTime.of(2000,1,1,0,00,00);
System.out.println(birthDate);
Duration duration = Duration.between(birthDate ,today);//第二个参数减第一个参数
通过to转换方法获取对应的数据
System.out.println(duration.toDays());//两个时间差的天数
System.out.println(duration.toHours());//两个时间差的小时数
System.out.println(duration.toMinutes());//两个时间差的分钟数
System.out.println(duration.toMillis());//两个时间差的毫秒数
System.out.println(duration.toNanos());//两个时间差的纳秒数
ChronoUnit
获取当前时间对象
//本地日期时间对象。
LocalDateTime today =LocalDateTime.now();
System.out.println(today);
定义第二个日期时间对象
//出生的日期时间对象
LocalDateTime birthDate = LocalDateTime.of(2000,1,1,0,00,00);
System.out.println(birthDate);
计算两个日期的间隔
System.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));
好啦,到这里JDK8的时间类就学习完啦,我们不用死记硬背哟,有需要的时候知道怎么找API文档就可以啦,有什么不懂的可以评论区互相探讨哟,我们下期不见不散!
==最后非常感谢您的阅读,也希望能得到您的反馈 ==