在Java 8中引入的LocalDate表示一个格式为yyyy-MM-dd的日期,如2024-06-13。它不存储时间或时区。我们可以从LocalDate中获取许多其他的日期字段,如年日(day-of-year)、周日(day-of-week)、月日(month-of-year)等等。
1 初始化
LocalDate以年月日的格式输出,即yyyy-MM-dd。我们可以使用LocalDate的下列静态方法来初始化LocalDate。
1.1 now() 当前时间
给出LocalDate实例,该实例包含默认时区的系统时钟的当前日期。
java
public static void main(String[] args) {
//获取当前日期
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
}
其他重载方法:
- now(Clock clock): 提供LocalDate实例,该实例具有从指定时钟获得的当前日期。
- now(ZoneId zone): 给出当前日期的
LocalDate
实例,该日期来自指定时区的系统时钟。
1.2 of() 指定时间
java
public static void main(String[] args) {
//获取指定的日期 参数是年,月,日
LocalDate localDate = LocalDate.of(2024,1,1);
System.out.println(localDate);
//获取当前日期时间 参数是年,月,日,时,分,秒
LocalDateTime localDateTime = LocalDateTime.of(2024,1,1,1,1,1);
System.out.println(localDateTime);
}
1.3 ofYearDay() 指定时间
java
public static void main(String[] args) {
//获取指定的日期 参数是年 指定年中的第几天
LocalDate localDate = LocalDate.ofYearDay(2024,100);
System.out.println(localDate);
// LocalDateTime 不存在ofYearDay方法
// LocalDateTime localDateTime = LocalDateTime.ofYearDay(2024,100);
// System.out.println(localDateTime);
}
1.4 parse() 从日期字符串中获取对象
java
public static void main(String[] args) {
// 从日期字符串中获取LocalDate,注意需要添加时间格式,否则会报错
LocalDate localDate = LocalDate.parse("2024-10-01");
System.out.println(localDate);
// 从日期字符串中获取LocalDateTime,注意需要添加时间格式,否则会报错
LocalDateTime localDateTime = LocalDateTime.parse("2024-10-01T12:12:12");
System.out.println(localDateTime);
// 从日期字符串中获取LocalDateTime,指定日期字符串格式
localDateTime = LocalDateTime.parse("2024-10-01 12:12:12", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(localDateTime);
}
2 获取时间
2.1 基础获取时间
java
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println("年:" + localDate.getYear());
System.out.println("月:" + localDate.getMonthValue());
System.out.println("年日:" + localDate.getDayOfYear());
System.out.println("月日:" + localDate.getDayOfMonth());
System.out.println("星期(英文):" + localDate.getDayOfWeek());
System.out.println("星期(数字):" + localDate.getDayOfWeek().getValue());
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("时:" + localDateTime.getHour());
System.out.println("分:" + localDateTime.getMinute());
System.out.println("秒:" + localDateTime.getSecond());
}
2.2 TemporalAdjusters 时间调节器
java
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("=======================月调节============================");
LocalDateTime firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("获取月中的第一天时间:" + firstDayOfMonth);
LocalDateTime lastDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("获取月中的最后一天时间:" + lastDayOfMonth);
LocalDateTime firstDayOfNextMonth = now.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println("获取下个月的第一天时间:" + firstDayOfNextMonth);
System.out.println("=======================年调节============================");
LocalDateTime firstDayOfYear = now.with(TemporalAdjusters.firstDayOfYear());
System.out.println("获取年中的第一天时间:" + firstDayOfYear);
LocalDateTime lastDayOfYear = now.with(TemporalAdjusters.lastDayOfYear());
System.out.println("获取年中的最后一天时间:" + lastDayOfYear);
LocalDateTime firstDayOfNextYear = now.with(TemporalAdjusters.firstDayOfNextYear());
System.out.println("获取下一年的第一天时间:" + firstDayOfNextYear);
System.out.println("=======================星期调节============================");
LocalDateTime firstInMonth = now.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println("获取本月第一个星期一的时间:" + firstInMonth);
LocalDateTime lastInMonth = now.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY));
System.out.println("获取本月最后一个星期一的时间:" + lastInMonth);
LocalDateTime next = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println("获取本月下一个星期一的时间:" + next);
LocalDateTime previous = now.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println("获取本月上一个星期一的时间:" + previous);
LocalDateTime nextOrSame = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
System.out.println("获取本月下一个或当前星期一的时间:" + nextOrSame);
LocalDateTime previousOrSame = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
System.out.println("获取本月上一个或当前星期一的时间:" + previousOrSame);
}
3 时间运算
3.1 plus() 增加时间
java
public static void main(String[] args) {
// plus方法 增加时间如果超出,则会自动进位,例如:2020-12-31.plusMonths(1) = 2021-01-31
LocalDate localDate = LocalDate.now();
System.out.println("增加1年:" + localDate.plusYears(1));
System.out.println("增加1月:" + localDate.plusMonths(1));
System.out.println("增加1周:" + localDate.plusWeeks(1));
System.out.println("增加1天:" + localDate.plusDays(1));
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("增加1小时:" + localDateTime.plusHours(1));
System.out.println("增加1分钟:" + localDateTime.plusMinutes(1));
System.out.println("增加1秒:" + localDateTime.plusSeconds(1));
}
3.2 minus() 减少时间
java
public static void main(String[] args) {
// minus方法 减少时间如果超出,会自动借位, 如:2020-01-01 减少一天,会变成2019-12-31
LocalDate localDate = LocalDate.now();
System.out.println("减少1年:" + localDate.minusYears(1));
System.out.println("减少1月:" + localDate.minusMonths(1));
System.out.println("减少1周:" + localDate.minusWeeks(1));
System.out.println("减少1天:" + localDate.minusDays(1));
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("减少1小时:" + localDateTime.minusHours(1));
System.out.println("减少1分钟:" + localDateTime.minusMinutes(1));
System.out.println("减少1秒:" + localDateTime.minusSeconds(1));
}
4 比较时间
java
public static void main(String[] args) {
LocalDate now = LocalDate.now();
LocalDate localDate = LocalDate.of(2023, 1, 1);
// 比较方法 -1 小于 0 等于 1 大于
int i = now.compareTo(localDate);
System.out.println("当前时间与指定时间比较:" + i);
boolean after = now.isAfter(localDate);
System.out.println("当前时间是否在指定时间之后:" + after);
boolean before = now.isBefore(localDate);
System.out.println("当前时间是否在指定时间之前:" + before);
boolean equal = now.isEqual(localDate);
System.out.println("当前时间是否等于指定时间:" + equal);
}
5 字符串化
java
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("转字符串:" + now);
System.out.println("年月日转字符串:" + now.toLocalDate());
System.out.println("时分秒转字符串:" + now.toLocalTime());
System.out.println("格式化时间:" + now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}