一、常用方法
在Java中,日期和时间的处理主要依赖于 java.time 包中的类。以下是详细的日期类及其常用方法的列举:
1. LocalDate(表示日期)
now(): 获取当前日期
of(int year, int month, int day): 创建指定日期
getYear(): 获取年份
getMonthValue(): 获取月份(1-12)
getDayOfMonth(): 获取日期(1-31)
plusDays(long days): 增加天数
minusMonths(long months): 减少月数
isAfter(LocalDate other): 判断是否在指定日期之后
isBefore(LocalDate other): 判断是否在指定日期之前
isLeapYear(): 判断是否为闰年
2. LocalTime(表示时间)
now(): 获取当前时间
of(int hour, int minute, int second): 创建指定时间
getHour(): 获取小时
getMinute(): 获取分钟
getSecond(): 获取秒
plusHours(long hours): 增加小时数
minusMinutes(long minutes): 减少分钟数
3. LocalDateTime(表示日期和时间)
now(): 获取当前日期和时间
of(int year, int month, int day, int hour, int minute): 创建指定日期和时间
atTime(LocalTime time): 将日期与时间组合
plusDays(long days): 增加天数
plusHours(long hours): 增加小时数
4. ZonedDateTime(带时区的日期时间)
now(): 获取当前带时区的时间
of(LocalDateTime dateTime, ZoneId zone): 创建指定时区的日期时间
withZoneSameInstant(ZoneId zone): 转换到另一个时区
5. Duration(时间间隔)
between(Temporal start, Temporal end): 计算两个时间点之间的时间间隔
getSeconds(): 获取总秒数
6. Period(日期间隔)
between(LocalDate startDate, LocalDate endDate): 计算两个日期之间的间隔
getDays(): 获取天数部分
7. DateTimeFormatter(格式化和解析日期时间)
ofPattern(String pattern): 定义日期时间格式
format(TemporalAccessor temporal): 格式化日期时间
parse(CharSequence text): 解析字符串为日期时间
二、demo
java
package com.qiu;
import org.junit.jupiter.api.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
* 描述:
*
* @author: qxd
* @date: 2026/2/12 21:03
* @version: 1.0.0
*/
public class DateTimeTest {
@Test
public void test1() {
// 1. LocalDate 示例
LocalDate today = LocalDate.now();
System.out.println("当前日期: " + today);
LocalDate specificDate = LocalDate.of(2026, 2, 12);
System.out.println("指定日期: " + specificDate);
System.out.println("年份: " + today.getYear());
System.out.println("月份: " + today.getMonthValue());
System.out.println("日期: " + today.getDayOfMonth());
LocalDate nextWeek = today.plusDays(7);
System.out.println("一周后日期: " + nextWeek);
LocalDate lastMonth = today.minusMonths(1);
System.out.println("上个月日期: " + lastMonth);
System.out.println("是否在指定日期之后: " + today.isAfter(specificDate));
System.out.println("是否为闰年: " + today.isLeapYear());
// 2. LocalTime 示例
LocalTime nowTime = LocalTime.now();
System.out.println("当前时间: " + nowTime);
LocalTime specificTime = LocalTime.of(20, 59, 0);
System.out.println("指定时间: " + specificTime);
System.out.println("小时: " + nowTime.getHour());
System.out.println("分钟: " + nowTime.getMinute());
System.out.println("秒: " + nowTime.getSecond());
LocalTime laterTime = nowTime.plusHours(2);
System.out.println("两小时后时间: " + laterTime);
LocalTime earlierTime = nowTime.minusMinutes(30);
System.out.println("半小时前时间: " + earlierTime);
// 3. LocalDateTime 示例
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println("当前日期时间: " + nowDateTime);
LocalDateTime specificDateTime = LocalDateTime.of(2026, 2, 12, 20, 59);
System.out.println("指定日期时间: " + specificDateTime);
LocalDateTime futureDateTime = nowDateTime.plusDays(1).plusHours(3);
System.out.println("一天三小时后日期时间: " + futureDateTime);
// 4. ZonedDateTime 示例
ZonedDateTime nowZoned = ZonedDateTime.now();
System.out.println("当前带时区时间: " + nowZoned);
ZonedDateTime specificZoned = ZonedDateTime.of(specificDateTime, ZoneId.of("Asia/Shanghai"));
System.out.println("指定时区时间: " + specificZoned);
ZonedDateTime utcTime = specificZoned.withZoneSameInstant(ZoneId.of("UTC"));
System.out.println("UTC 时间: " + utcTime);
// 5. Duration 示例
Duration duration = Duration.between(nowTime, specificTime);
System.out.println("时间间隔秒数: " + duration.getSeconds());
// 6. Period 示例
Period period = Period.between(today, specificDate);
System.out.println("日期间隔天数: " + period.getDays());
// 7. DateTimeFormatter 示例
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = nowDateTime.format(formatter);
System.out.println("格式化日期时间: " + formattedDateTime);
LocalDateTime parsedDateTime = LocalDateTime.parse("2026-02-12 20:59:00", formatter);
System.out.println("解析日期时间: " + parsedDateTime);
}
}
三、输出结果
java
当前日期: 2026-02-12
指定日期: 2026-02-12
年份: 2026
月份: 2
日期: 12
一周后日期: 2026-02-19
上个月日期: 2026-01-12
是否在指定日期之后: false
是否为闰年: false
当前时间: 21:04:50.344337200
指定时间: 20:59
小时: 21
分钟: 4
秒: 50
两小时后时间: 23:04:50.344337200
半小时前时间: 20:34:50.344337200
当前日期时间: 2026-02-12T21:04:50.346203300
指定日期时间: 2026-02-12T20:59
一天三小时后日期时间: 2026-02-14T00:04:50.346203300
当前带时区时间: 2026-02-12T21:04:50.347203900+08:00[Asia/Shanghai]
指定时区时间: 2026-02-12T20:59+08:00[Asia/Shanghai]
UTC 时间: 2026-02-12T12:59Z[UTC]
时间间隔秒数: -351
日期间隔天数: 0
格式化日期时间: 2026-02-12 21:04:50
解析日期时间: 2026-02-12T20:59