Java8 中常用的时间处理类Date、Calendar、 java.time详解

📚 Java 时间处理函数详解


🔖 1. 概述

Java 中有多种 时间处理类函数 来处理日期和时间。这些类在不同的版本中不断演化,从早期的 DateCalendar,到 Java 8 引入的 java.time API,这些新 API 提供了更强大的时间处理能力,包括 日期、时间、时区、格式化、解析、时间间隔 等功能。


🔗 2. Java 中常用的时间处理类

类/接口描述
LocalDatejava.time表示日期,不包含时间
LocalTimejava.time表示时间,不包含日期
LocalDateTimejava.time表示日期和时间,不包含时区信息
ZonedDateTimejava.time包含时区的日期时间
Instantjava.time表示一个时间戳
Durationjava.time表示两个时间之间的间隔
Periodjava.time表示两个日期之间的间隔
DateTimeFormatterjava.time.format日期时间格式化和解析
ZoneIdjava.time表示时区

📋 3. LocalDateLocalTime 的使用

3.1 LocalDate 的使用

LocalDate 表示 日期(例如:2025-01-06),不包含时间和时区信息。

🔧 使用案例
import java.time.LocalDate;

public class LocalDateExample {
    public static void main(String[] args) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current Date: " + currentDate);

        // 创建一个指定日期
        LocalDate specificDate = LocalDate.of(2025, 1, 6);
        System.out.println("Specific Date: " + specificDate);

        // 获取日期的年、月、日
        int year = currentDate.getYear();
        int month = currentDate.getMonthValue();
        int day = currentDate.getDayOfMonth();
        System.out.println("Year: " + year + ", Month: " + month + ", Day: " + day);

        // 判断是否是闰年
        boolean isLeapYear = currentDate.isLeapYear();
        System.out.println("Is Leap Year: " + isLeapYear);
    }
}

输出

Current Date: 2025-01-06
Specific Date: 2025-01-06
Year: 2025, Month: 1, Day: 6
Is Leap Year: false

3.2 LocalTime 的使用

LocalTime 表示 时间(例如:10:15:30),不包含日期和时区信息。

🔧 使用案例
import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        // 获取当前时间
        LocalTime currentTime = LocalTime.now();
        System.out.println("Current Time: " + currentTime);

        // 创建一个指定时间
        LocalTime specificTime = LocalTime.of(10, 15, 30);
        System.out.println("Specific Time: " + specificTime);

        // 获取时间的小时、分钟、秒
        int hour = currentTime.getHour();
        int minute = currentTime.getMinute();
        int second = currentTime.getSecond();
        System.out.println("Hour: " + hour + ", Minute: " + minute + ", Second: " + second);
    }
}

输出

Current Time: 10:20:45.123
Specific Time: 10:15:30
Hour: 10, Minute: 20, Second: 45

📆 4. LocalDateTime 的使用

LocalDateTime 表示 日期和时间,不包含时区信息。

🔧 使用案例
import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        // 获取当前日期时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current DateTime: " + currentDateTime);

        // 创建一个指定日期时间
        LocalDateTime specificDateTime = LocalDateTime.of(2025, 1, 6, 10, 15, 30);
        System.out.println("Specific DateTime: " + specificDateTime);

        // 加减日期时间
        LocalDateTime nextDay = currentDateTime.plusDays(1);
        System.out.println("Next Day: " + nextDay);

        LocalDateTime previousHour = currentDateTime.minusHours(1);
        System.out.println("Previous Hour: " + previousHour);
    }
}

输出

Current DateTime: 2025-01-06T10:20:45.123
Specific DateTime: 2025-01-06T10:15:30
Next Day: 2025-01-07T10:20:45.123
Previous Hour: 2025-01-06T09:20:45.123

🕒 5. ZonedDateTime 的使用

ZonedDateTime 表示 包含时区的日期时间

🔧 使用案例
import java.time.ZonedDateTime;
import java.time.ZoneId;

public class ZonedDateTimeExample {
    public static void main(String[] args) {
        // 获取当前时区的日期时间
        ZonedDateTime currentZonedDateTime = ZonedDateTime.now();
        System.out.println("Current ZonedDateTime: " + currentZonedDateTime);

        // 创建指定时区的日期时间
        ZonedDateTime specificZonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println("Specific ZonedDateTime: " + specificZonedDateTime);
    }
}

输出

Current ZonedDateTime: 2025-01-06T10:20:45.123+01:00[Europe/Paris]
Specific ZonedDateTime: 2025-01-06T04:20:45.123-05:00[America/New_York]

🕰️ 6. Instant 的使用

Instant 表示一个 时间戳,通常用于记录事件的发生时间。

🔧 使用案例
import java.time.Instant;

public class InstantExample {
    public static void main(String[] args) {
        // 获取当前时间戳
        Instant currentInstant = Instant.now();
        System.out.println("Current Instant: " + currentInstant);

        // 将 Instant 转换为毫秒
        long epochMilli = currentInstant.toEpochMilli();
        System.out.println("Epoch Milliseconds: " + epochMilli);
    }
}

🧮 7. 时间间隔的计算

Duration(时间间隔)

import java.time.Duration;
import java.time.LocalTime;

public class DurationExample {
    public static void main(String[] args) {
        LocalTime startTime = LocalTime.of(10, 0);
        LocalTime endTime = LocalTime.of(12, 30);

        Duration duration = Duration.between(startTime, endTime);
        System.out.println("Duration: " + duration.toHours() + " hours");
    }
}

Period(日期间隔)

import java.time.LocalDate;
import java.time.Period;

public class PeriodExample {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2020, 1, 1);
        LocalDate endDate = LocalDate.of(2025, 1, 1);

        Period period = Period.between(startDate, endDate);
        System.out.println("Period: " + period.getYears() + " years");
    }
}

📅 8. 日期时间格式化

使用 DateTimeFormatter

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();

        // 自定义格式化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = dateTime.format(formatter);
        System.out.println("Formatted DateTime: " + formattedDateTime);
    }
}

🎯 9. 总结

类/接口描述
LocalDate表示日期
LocalTime表示时间
LocalDateTime表示日期和时间
ZonedDateTime表示带时区的日期时间
Instant表示时间戳
Duration表示两个时间点之间的时间间隔
Period表示两个日期之间的日期间隔
DateTimeFormatter日期时间格式化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十方来财

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值