Java8 日期和时间API示例

1. LocalDate

LocalDate 表示一个具体的日期,不包含时间信息,也不包含时区信息。

import java.time.LocalDate;  
  
public class LocalDateExample {  
    public static void main(String[] args) {  
        // 获取当前日期  
        LocalDate today = LocalDate.now();  
        System.out.println("当前日期: " + today);  
  
        // 创建特定日期  
        LocalDate specificDate = LocalDate.of(2023, 10, 1);  
        System.out.println("特定日期: " + specificDate);  
  
        // 日期运算  
        LocalDate nextDay = today.plusDays(1);  
        System.out.println("明天是: " + nextDay);  
  
        // 判断日期是否相等  
        if (today.equals(specificDate.minusYears(100))) {  
            System.out.println("今天的日期和100年前的某个特定日期相等(实际上不会相等)");  
        } else {  
            System.out.println("今天的日期和100年前的某个特定日期不相等");  
        }  
    }  
}

2. LocalTime

LocalTime 表示一个具体的时间,不包含日期信息,也不包含时区信息。

import java.time.LocalTime;  
  
public class LocalTimeExample {  
    public static void main(String[] args) {  
        // 获取当前时间  
        LocalTime now = LocalTime.now();  
        System.out.println("当前时间: " + now);  
  
        // 创建特定时间  
        LocalTime specificTime = LocalTime.of(14, 30, 45);  
        System.out.println("特定时间: " + specificTime);  
  
        // 时间运算  
        LocalTime later = specificTime.plusHours(2);  
        System.out.println("两小时后是: " + later);  
  
        // 格式化时间  
        System.out.println("格式化时间: " + specificTime.format(DateTimeFormatter.ofPattern("HH:mm:ss")));  
    }  
}

注意:上面的 DateTimeFormatter 需要从 java.time.format 包中导入。

3. LocalDateTime

LocalDateTime 表示一个具体的日期和时间,不包含时区信息。

import java.time.LocalDateTime;  
  
public class LocalDateTimeExample {  
    public static void main(String[] args) {  
        // 获取当前日期和时间  
        LocalDateTime now = LocalDateTime.now();  
        System.out.println("当前日期和时间: " + now);  
  
        // 创建特定日期和时间  
        LocalDateTime specificDateTime = LocalDateTime.of(2023, 10, 1, 14, 30, 45);  
        System.out.println("特定日期和时间: " + specificDateTime);  
  
        // 日期时间运算  
        LocalDateTime later = specificDateTime.plusMinutes(30);  
        System.out.println("30分钟后是: " + later);  
    }  
}

4. ZonedDateTime

ZonedDateTime 表示一个具体的日期、时间和时区。

import java.time.ZoneId;  
import java.time.ZonedDateTime;  
  
public class ZonedDateTimeExample {  
    public static void main(String[] args) {  
        // 获取当前日期、时间和时区  
        ZonedDateTime now = ZonedDateTime.now();  
        System.out.println("当前日期、时间和时区: " + now);  
  
        // 使用特定时区和时间创建ZonedDateTime  
        ZonedDateTime specificZoneDateTime = ZonedDateTime.of(2023, 10, 1, 14, 30, 45, 0, ZoneId.of("America/New_York"));  
        System.out.println("特定日期、时间和时区: " + specificZoneDateTime);  
  
        // 转换时区  
        ZonedDateTime converted = specificZoneDateTime.withZoneSameInstant(ZoneId.of("Europe/London"));  
        System.out.println("转换时区后的日期和时间: " + converted);  
    }  
}

5. Instant

Instant 表示时间线上的一个瞬时点,通常是以UTC时区表示的。它主要用于表示时间戳,即从1970-01-01T00:00:00Z(UTC)开始的毫秒数(或纳秒数)。

import java.time.Instant;  
  
public class InstantExample {  
    public static void main(String[] args) {  
        // 获取当前时间戳  
        Instant now = Instant.now();  
        System.out.println("当前时间戳: " + now);  
  
        // 使用特定时间戳创建Instant  
        Instant specificInstant = Instant.ofEpochMilli(1633046400000L); // 2021-10-01T00:00:00Z  
        System.out.println("特定时间戳的Instant: " + specificInstant);  
  
        // 转换为其他时区的时间  
        Instant convertedInstant = specificInstant.atOffset(ZoneOffset.ofHours(8)).toInstant(); // 假设转换为UTC+8时区,但注意这里实际上没有改变Instant的值,只是展示了如何结合使用  
        System.out.println("(注意:这里转换只是示例,Instant本身不存储时区信息)");  
    }  
}

注意:Instant本身不存储时区信息,所以convertedInstant实际上和specificInstant表示的是同一个时间点,只是示例了如何与时区偏移量结合使用。

6.Duration

Duration 表示两个时间点之间的时间量,以秒和纳秒为单位。它用于表示时间间隔,比如持续时间或延迟。

import java.time.Duration;  
  
public class DurationExample {  
    public static void main(String[] args) {  
        // 创建一个Duration实例,表示1小时2分钟3秒  
        Duration duration = Duration.ofHours(1).plusMinutes(2).plusSeconds(3);  
  
        // 输出Duration  
        System.out.println("Duration: " + duration);  
  
        // 将Duration转换为秒和纳秒  
        long seconds = duration.getSeconds();  
        int nano = duration.getNano();  
        System.out.println("Seconds: " + seconds + ", Nanoseconds: " + nano);  
    }  
}

7.Period

Period 表示两个日期之间的时间量,以年、月和日为单位。它通常用于日历日期的计算,比如两个日期之间相差多少年、多少月、多少天。

import java.time.LocalDate;  
import java.time.Period;  
  
public class PeriodExample {  
    public static void main(String[] args) {  
        // 创建两个LocalDate实例  
        LocalDate startDate = LocalDate.of(2020, 1, 15);  
        LocalDate endDate = LocalDate.of(2023, 4, 1);  
  
        // 计算两个日期之间的Period  
        Period period = Period.between(startDate, endDate);  
  
        // 输出Period  
        System.out.println("Period: " + period.getYears() + " years, " +  
                           period.getMonths() + " months, " +  
                           period.getDays() + " days");  
    }  
}

8.DateTimeFormatter

DateTimeFormatter 用于格式化和解析日期时间对象。它提供了丰富的格式模式,允许你以自定义的方式展示日期和时间,也可以将字符串解析为日期时间对象。

import java.time.LocalDate;  
import java.time.format.DateTimeFormatter;  
  
public class DateTimeFormatterExample {  
    public static void main(String[] args) {  
        // 创建一个DateTimeFormatter实例,用于自定义日期格式  
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");  
  
        // 使用formatter格式化LocalDate  
        LocalDate date = LocalDate.now();  
        String formattedDate = date.format(formatter);  
        System.out.println("Formatted date: " + formattedDate);  
  
        // 使用formatter解析字符串为LocalDate  
        String dateString = "2023-10-01";  
        LocalDate parsedDate = LocalDate.parse(dateString, formatter);  
        System.out.println("Parsed date: " + parsedDate);  
    }  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值