java中常用的日期方法

一、常用方法

在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
相关推荐
后端AI实验室8 小时前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai
程序员清风10 小时前
小红书二面:Spring Boot的单例模式是如何实现的?
java·后端·面试
belhomme10 小时前
(面试题)Redis实现 IP 维度滑动窗口限流实践
java·面试
Be_Better10 小时前
学会与虚拟机对话---ASM
java
开源之眼12 小时前
《github star 加星 Taimili.com 艾米莉 》为什么Java里面,Service 层不直接返回 Result 对象?
java·后端·github
Maori31613 小时前
放弃 SDKMAN!在 Garuda Linux + Fish 环境下的优雅 Java 管理指南
java
用户9083246027314 小时前
Spring AI 1.1.2 + Neo4j:用知识图谱增强 RAG 检索(上篇:图谱构建)
java·spring boot
小王和八蛋14 小时前
DecimalFormat 与 BigDecimal
java·后端
beata14 小时前
Java基础-16:Java内置锁的四种状态及其转换机制详解-从无锁到重量级锁的进化与优化指南
java·后端
IT探险家14 小时前
你的第一个 Java 程序就翻车?HelloWorld 的 8 个隐藏陷阱
java