Java8新增的日期时间类
LocalDate、LocalTime、LocalDateTime 类的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。
类上都是有final修饰的
LocalDate:代表本地日期(年、月、日、星期)
package com.itheima.d10_jdk8_time;import java.time.LocalDate;public class Demo1_LocalDate {public static void main(String[] args) {// 0、获取本地日期对象(不可变对象)LocalDate ld = LocalDate.now();System.out.println(ld);// 1、获取日期对象中的信息int year = ld.getYear(); // 年int month = ld.getMonthValue(); // 月(1-12)int month1 = ld.getMonth().getValue();int day = ld.getDayOfMonth(); // 日int dayOfYear = ld.getDayOfYear(); // 一年中的第几天int dayOfWeek = ld.getDayOfWeek().getValue(); // 星期几System.out.println(year);System.out.println(month);System.out.println(day);System.out.println(dayOfYear);System.out.println(dayOfWeek);// 2、直接修改某个信息: withYear、withMonth、withDayOfMonth、withDayOfYear// JDK8新增的日期时间对象都是不可变的对象,每次修改都返回了一个新对象,原来的对象并没有发生改变LocalDate ld2 = ld.withYear(2099);System.out.println(ld2); // 2099-12-16System.out.println(ld); // 2022-12-16LocalDate ld3 = ld.withMonth(10);System.out.println(ld3); // 2022-10-16// 3、把某个信息加多少: plusYears、plusMonths、plusDays、plusWeeksLocalDate ld4 = ld.plusDays(5);System.out.println(ld4); // 2022-12-21// 4、把某个信息减多少:minusYears、minusMonths、minusDays、minusWeeksLocalDate ld5 = ld.minusYears(5);System.out.println(ld5); // 2017-12-16// 5、获取指定日期的LocalDate对象: public static LocalDate of(int year, int month, int dayOfMonth)LocalDate ld1 = LocalDate.of(2099, 11, 11);LocalDate ld6 = LocalDate.of(2099, 11, 20);// 6、判断2个日期对象,是否相等,在前还是在后: equals isBefore isAfterSystem.out.println(ld1.equals(ld6)); // falseSystem.out.println(ld1.isBefore(ld6)); // trueSystem.out.println(ld1.isAfter(ld6)); // false}
}
LocalTime:代表本地时间(时、分、秒、纳秒)
package com.itheima.d10_jdk8_time;import java.time.LocalTime;public class Demo2_LocalTime {public static void main(String[] args) {// 0、获取本地时间对象LocalTime lt = LocalTime.now(); // 时 分 秒 纳秒 不可变的System.out.println(lt);// 1、获取时间中的信息int hour = lt.getHour(); //时int minute = lt.getMinute(); //分int second = lt.getSecond(); //秒int nano = lt.getNano(); //纳秒// 2、修改时间:withHour、withMinute、withSecond、withNanoLocalTime lt3 = lt.withHour(10);LocalTime lt4 = lt.withMinute(10);LocalTime lt5 = lt.withSecond(10);LocalTime lt6 = lt.withNano(10);// 3、加多少:plusHours、plusMinutes、plusSeconds、plusNanosLocalTime lt7 = lt.plusHours(10);LocalTime lt8 = lt.plusMinutes(10);LocalTime lt9 = lt.plusSeconds(10);LocalTime lt10 = lt.plusNanos(10);// 4、减多少:minusHours、minusMinutes、minusSeconds、minusNanosLocalTime lt11 = lt.minusHours(10);LocalTime lt12 = lt.minusMinutes(10);LocalTime lt13 = lt.minusSeconds(10);LocalTime lt14 = lt.minusNanos(10);// 5、获取指定时间的LocalTime对象:// public static LocalTime of(int hour, int minute, int second)LocalTime lt15 = LocalTime.of(12, 12, 12);LocalTime lt16 = LocalTime.of(12, 12, 12);// 6、判断2个时间对象,是否相等,在前还是在后: equals isBefore isAfterSystem.out.println(lt15.equals(lt16)); // trueSystem.out.println(lt15.isAfter(lt)); // falseSystem.out.println(lt15.isBefore(lt)); // true}
}
LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)
package com.itheima.d10_jdk8_time;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;public class Demo3_LocalDateTime {public static void main(String[] args) {// 0、获取本地日期和时间对象。LocalDateTime ldt = LocalDateTime.now(); // 年 月 日 时 分 秒 纳秒System.out.println(ldt);// 1、可以获取日期和时间的全部信息int year = ldt.getYear(); // 年int month = ldt.getMonthValue(); // 月int day = ldt.getDayOfMonth(); // 日int dayOfYear = ldt.getDayOfYear(); // 一年中的第几天int dayOfWeek = ldt.getDayOfWeek().getValue(); // 获取是周几int hour = ldt.getHour(); //时int minute = ldt.getMinute(); //分int second = ldt.getSecond(); //秒int nano = ldt.getNano(); //纳秒// 2、修改时间信息:// withYear withMonth withDayOfMonth withDayOfYear withHour// withMinute withSecond withNanoLocalDateTime ldt2 = ldt.withYear(2029);LocalDateTime ldt3 = ldt.withMinute(59);// 3、加多少:// plusYears plusMonths plusDays plusWeeks plusHours plusMinutes plusSeconds plusNanosLocalDateTime ldt4 = ldt.plusYears(2);LocalDateTime ldt5 = ldt.plusMinutes(3);// 4、减多少:// minusDays minusYears minusMonths minusWeeks minusHours minusMinutes minusSeconds minusNanosLocalDateTime ldt6 = ldt.minusYears(2);LocalDateTime ldt7 = ldt.minusMinutes(3);// 5、获取指定日期和时间的LocalDateTime对象:// public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour,// int minute, int second, int nanoOfSecond)LocalDateTime ldt8 = LocalDateTime.of(2029, 12, 12, 12, 12, 12, 1222);LocalDateTime ldt9 = LocalDateTime.of(2029, 12, 12, 12, 12, 12, 1222);// 6、 判断2个日期、时间对象,是否相等,在前还是在后: equals、isBefore、isAfterSystem.out.println(ldt9.equals(ldt8));System.out.println(ldt9.isAfter(ldt));System.out.println(ldt9.isBefore(ldt));// 7、可以把LocalDateTime转换成LocalDate和LocalTime// public LocalDate toLocalDate()// public LocalTime toLocalTime()// public static LocalDateTime of(LocalDate date, LocalTime time)LocalDate ld = ldt.toLocalDate();LocalTime lt = ldt.toLocalTime();LocalDateTime ldt10 = LocalDateTime.of(ld, lt);}
}
Instant时间戳
Instant时间线上的某个时刻/时间戳
作用:可以用来记录代码的执行时间,或用于记录用户操作某个事件的时间点。
传统的Date类,只能精确到毫秒,并且是可变对象;
新增的Instant类,可以精确到纳秒,并且是不可变对象,推荐用Instant代替Date。
public class Demo5_Instant {public static void main(String[] args) {// 1、创建Instant的对象,获取此刻时间信息Instant now = Instant.now();System.out.println(now);// 2、获取总秒数long epochSecond = now.getEpochSecond();System.out.println(epochSecond);// 3、不够1秒的纳秒数int nano = now.getNano();System.out.println(nano);// 4、增加、减少系列的方法Instant plusSeconds = now.plusSeconds(10);System.out.println(plusSeconds);Instant minusSeconds = now.minusSeconds(10);System.out.println(minusSeconds);// 5、判断系列的方法System.out.println(plusSeconds.equals(minusSeconds));System.out.println(plusSeconds.isAfter(minusSeconds));System.out.println(plusSeconds.isBefore(minusSeconds));// Instant对象的作用:做代码的性能分析,或者记录用户的操作时间点Instant begin = Instant.now();Instant end = Instant.now();s}
}
Tip:
1秒=1000毫秒;
1毫秒=1000微秒;
1微秒=1000纳秒;
1秒 = 1000 000 000纳秒
Duration 和 Period
Period(一段时期)
可以用于计算两个 LocalDate对象 相差的年数、月数、天数。
package com.itheima.d10_jdk8_time;import java.time.LocalDate;
import java.time.Period;public class Demo7_Period {public static void main(String[] args) {// 目标:掌握Period的作用:计算机两个日期相差的年数,月数、天数。LocalDate start = LocalDate.of(2029, 8, 10);LocalDate end = LocalDate.of(2029, 12, 15);// 1、创建Period对象,封装两个日期对象。Period between = Period.between(start, end);// 2、通过period对象获取两个日期对象相差的信息。System.out.println(between.getDays());System.out.println(between.getMonths());System.out.println(between.getYears());}
}
Duration(持续时间)
可以用于计算两个时间对象相差的天数、小时数、分数、秒数、纳秒数;支持LocalTime、LocalDateTime、Instant等时间。
package com.itheima.d10_jdk8_time;import java.time.Duration;
import java.time.LocalDateTime;public class Demo8_Duration {public static void main(String[] args) {LocalDateTime start = LocalDateTime.of(2025, 11, 11, 11, 10, 10);LocalDateTime end = LocalDateTime.of(2025, 11, 11, 12, 11, 11);// 1、得到Duration对象Duration between = Duration.between(start, end);// 2、获取两个时间对象间隔的信息System.out.println(between.toDays());System.out.println(between.toHours());System.out.println(between.toMinutes());System.out.println(between.toSeconds());System.out.println(between.toMillis());System.out.println(between.toNanos());}
}
解析与格式化
DateTimeFormatter:格式化器,用于时间的格式化、解析,并且是线程安全的
| 方法名 | 说明 |
|---|---|
public static DateTimeFormatter ofPattern(时间格式) | 获取格式化器对象 |
public String format(时间对象) | 格式化时间 |
LocalDateTime提供的格式化、解析时间的方法
package com.itheima.d10_jdk8_time;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;public class Demo6_DateTimeFormatter {public static void main(String[] args) {// 目标:掌握JDK 8新增的DateTimeFormatter格式化器的用法。LocalDateTime ldt = LocalDateTime.now();// 1、创建一个日期时间格式化器对象出来。DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");// 2、对时间进行格式化// 正向格式化String rs1 = dtf.format(ldt);System.out.println(rs1); //2022年12月16日 19:43:04// 反向格式化String rs2 = ldt.format(dtf);System.out.println(rs2);//2022年12月16日 19:43:04// 3、解析时间:解析时间一般使用LocalDateTime提供的解析方法来解析。String dateStr = "2029年12月12日 12:12:11";LocalDateTime parse = LocalDateTime.parse(dateStr, dtf);System.out.println(parse);}
}
时区的处理
Java8 中加入了对时区的支持,带时区的时间为分别为:ZonedDate、ZonedTime、ZonedDateTime
其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式,对于的Java类为ZoneId
例如 :Asia/Shanghai 等
ZoneId 时区的常见方法
package com.itheima.d10_jdk8_time;import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Set;public class Demo4_ZoneId_ZonedDateTime {public static void main(String[] args) {// 目标:了解时区和带时区的时间。// 1、ZoneId的常见方法:// public static ZoneId systemDefault(): 获取系统默认的时区ZoneId z = ZoneId.systemDefault();System.out.println(z.getId());// Asia/ShanghaiSystem.out.println(z);System.out.println("==========================================");// public static Set<String> getAvailableZoneIds(): 获取Java支持的全部时区IdSet<String> ids = ZoneId.getAvailableZoneIds();ids.forEach(System.out::println);// public static ZoneId of(String zoneId) : 把某个时区id封装成ZoneId对象。ZoneId of = ZoneId.of("America/New_York");System.out.println(of);//America/New_York// 2、ZonedDateTime:带时区的时间。// public static ZonedDateTime now(ZoneId zone): 获取某个时区的ZonedDateTime对象。ZonedDateTime now = ZonedDateTime.now(of);System.out.println(now); //2022-12-16T05:09:34.036378100-05:00[America/New_York]// 获取世界标准时间ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC());System.out.println(now1); //2022-12-16T10:10:57.850892200Z// public static ZonedDateTime now():获取系统默认时区的ZonedDateTime对象ZonedDateTime now2 = ZonedDateTime.now();System.out.println(now2);// 获取日期时间中的相关信息System.out.println(now2.getHour());System.out.println(now2.getDayOfWeek());// 修改日期时间中的相关信息ZonedDateTime zdt = now2.withYear(2088);System.out.println(zdt.getYear()); //2088}
}
参考:https://blog.csdn.net/weixin_48675653/article/details/128348609