【java】jdk8及以后的时间类总结

目录

1. LocalDate

2. LocalTime

4. ZonedDateTime

5. Duration

6. Period

7. DateTimeFormatter


1. LocalDate

说明:表示不带时区的日期(年、月、日),不可变且线程安全。

import java.time.LocalDate;

public class LocalDateExample {
    public static void main(String[] args) {
        // 获取当前系统日期
        LocalDate today = LocalDate.now(); // 结果:2023-03-15
        
        // 手动构造指定日期
        LocalDate specificDate = LocalDate.of(2023, 3, 15); // 结果:2023-03-15
        
        // 从字符串解析日期(ISO格式)
        LocalDate parsedDate = LocalDate.parse("2023-03-15"); // 结果:2023-03-15
        
        // 日期加减操作(返回新对象)
        LocalDate tomorrow = today.plusDays(1); // 结果:2023-03-16
        LocalDate previousMonth = today.minusMonths(1); // 结果:2023-02-15
        
        // 获取日期组件
        int year = today.getYear(); // 结果:2023
        int month = today.getMonthValue(); // 结果:3
        int day = today.getDayOfMonth(); // 结果:15
    }
}

2. LocalTime

说明:表示不带时区的时间(时、分、秒、纳秒),不可变且线程安全。

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        // 获取当前系统时间
        LocalTime now = LocalTime.now(); // 结果:14:30:45.123(示例值)
        
        // 手动构造指定时间
        LocalTime specificTime = LocalTime.of(14, 30, 45); // 结果:14:30:45
        
        // 从字符串解析时间(ISO格式)
        LocalTime parsedTime = LocalTime.parse("14:30:45"); // 结果:14:30:45
        
        // 时间加减操作
        LocalTime oneHourLater = now.plusHours(1); // 结果:15:30:45.123
        LocalTime thirtyMinutesEarlier = now.minusMinutes(30); // 结果:14:00:45.123
        
        // 获取时间组件
        int hour = now.getHour(); // 结果:14
        int minute = now.getMinute(); // 结果:30
        int second = now.getSecond(); // 结果:45
    }
}

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        // 获取当前系统日期时间
        LocalDateTime now = LocalDateTime.now(); // 结果:2023-03-15T14:30:45.123
        
        // 手动构造指定日期时间
        LocalDateTime specificDateTime = LocalDateTime.of(2023, 3, 15, 14, 30, 45); // 结果:2023-03-15T14:30:45
        
        // 从字符串解析日期时间(ISO格式)
        LocalDateTime parsedDateTime = LocalDateTime.parse("2023-03-15T14:30:45"); // 结果:2023-03-15T14:30:45
        
        // 拆分为日期和时间部分
        LocalDate date = now.toLocalDate(); // 结果:2023-03-15
        LocalTime time = now.toLocalTime(); // 结果:14:30:45.123
    }
}

4. ZonedDateTime

说明:表示带时区的完整日期时间,不可变且线程安全。

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedDateTimeExample {
    public static void main(String[] args) {
        // 获取当前系统时区的日期时间
        ZonedDateTime now = ZonedDateTime.now(); // 结果:2023-03-15T14:30:45+08:00[Asia/Shanghai]
        
        // 获取指定时区的当前时间
        ZonedDateTime nowInNewYork = ZonedDateTime.now(ZoneId.of("America/New_York")); // 结果:2023-03-14T23:30:45-05:00[America/New_York]
        
        // 构造指定时区的日期时间
        ZonedDateTime specificZonedDateTime = ZonedDateTime.of(2023, 3, 15, 14, 30, 45, 0, ZoneId.of("Europe/London")); // 结果:2023-03-15T14:30:45Z[Europe/London]
        
        // 时区转换(保持同一时刻)
        ZonedDateTime timeInTokyo = specificZonedDateTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo")); // 结果:2023-03-15T23:30:45+09:00[Asia/Tokyo]
    }
}

5. Duration

说明:表示两个时间点之间的时长(基于秒和纳秒),不可变且线程安全。

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

public class DurationExample {
    public static void main(String[] args) {
        // 定义两个时间点
        LocalTime start = LocalTime.of(10, 0); // 10:00
        LocalTime end = LocalTime.of(14, 30); // 14:30
        
        // 计算时长
        Duration duration = Duration.between(start, end); // 结果:PT4H30M
        
        // 获取不同单位的时长
        long hours = duration.toHours(); // 结果:4
        long minutes = duration.toMinutes(); // 结果:270
        long seconds = duration.getSeconds(); // 结果:16200
        
        // 时长加减操作
        Duration threeHours = Duration.ofHours(3);
        Duration sevenHours = duration.plus(threeHours); // 结果:PT7H30M
        
        Duration oneHour = Duration.ofHours(1);
        Duration threeAndAHalfHours = duration.minus(oneHour); // 结果:PT3H30M
    }
}

6. 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(2023, 3, 15);
        
        // 计算日期间隔
        Period period = Period.between(startDate, endDate); // 结果:P3Y2M14D
        
        // 获取间隔组件
        int years = period.getYears(); // 结果:3
        int months = period.getMonths(); // 结果:2
        int days = period.getDays(); // 结果:14
        
        // 间隔加减操作
        Period oneYear = Period.ofYears(1);
        Period fourYears = period.plus(oneYear); // 结果:P4Y2M14D
        
        Period sixMonths = Period.ofMonths(6);
        Period twoYearsEightMonths = period.minus(sixMonths); // 结果:P2Y8M14D
    }
}

7. DateTimeFormatter

说明:用于日期时间对象的格式化和解析,线程安全且不可变。

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

public class DateTimeFormatterExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        
        // 使用预定义ISO格式
        DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        String formattedIso = now.format(isoFormatter); // 结果:2023-03-15T14:30:45.123
        
        // 使用自定义格式模式
        DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedCustom = now.format(customFormatter); // 结果:2023-03-15 14:30:45
        
        // 解析自定义格式的字符串
        String dateTimeStr = "2023-03-15 14:30:45";
        LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeStr, customFormatter); // 结果:2023-03-15T14:30:45
    }
}

你可能感兴趣的:(Java,java,开发语言,时间类)