Java 8中时间API初探之一("Java 8 时间API入门教程(一):基础探析")
原创
一、引言
Java 8 引入了一套全新的时间日期API,这套API被命名为 Java 8 Date and Time API,也称为 JSR-310。它旨在解决旧版 Date 和 Calendar API 中的诸多问题,如线程不平安、设计不一致、日期操作不直观等。本文将带领大家了解 Java 8 时间API 的基本概念和使用方法。
二、Java 8 时间API的优势
Java 8 时间API具有以下优势:
- 不可变数据结构,保证线程平安;
- 清楚、直观的API设计;
- 易于使用,降低日期时间处理的谬误;
- 赞成ISO标准和Java旧版API。
三、Java 8 时间API的核心类
Java 8 时间API关键包括以下几个核心类:
java.time
:时间API的根包;java.time.LocalDate
:描述没有时区的日期;java.time.LocalTime
:描述没有时区的时间;java.time.LocalDateTime
:描述没有时区的日期和时间;java.time.ZonedDateTime
:描述带时区的日期和时间;java.time.ZoneId
:描述时区;java.time.format.DateTimeFormatter
:日期时间格式化类;java.time.temporal.ChronoUnit
:时间单位枚举类。
四、LocalDate、LocalTime和LocalDateTime
下面我们来具体了解一下这三个类的基本用法。
4.1 LocalDate
LocalDate
类描述没有时区的日期,如年、月、日。下面是创建和操作 LocalDate
的示例:
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
// 创建 LocalDate 实例
LocalDate today = LocalDate.now();
LocalDate specificDate = LocalDate.of(2021, 10, 1);
// 输出当前日期
System.out.println("Today is: " + today);
// 输出指定日期
System.out.println("Specific date is: " + specificDate);
// 日期加减
LocalDate nextDay = today.plusDays(1);
LocalDate previousDay = today.minusDays(1);
System.out.println("Next day is: " + nextDay);
System.out.println("Previous day is: " + previousDay);
}
}
4.2 LocalTime
LocalTime
类描述没有时区的时间,如小时、分钟、秒。下面是创建和操作 LocalTime
的示例:
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// 创建 LocalTime 实例
LocalTime now = LocalTime.now();
LocalTime specificTime = LocalTime.of(14, 30, 0);
// 输出当前时间
System.out.println("Now is: " + now);
// 输出指定时间
System.out.println("Specific time is: " + specificTime);
// 时间加减
LocalTime laterTime = now.plusHours(2);
LocalTime earlierTime = now.minusMinutes(30);
System.out.println("Later time is: " + laterTime);
System.out.println("Earlier time is: " + earlierTime);
}
}
4.3 LocalDateTime
LocalDateTime
类描述没有时区的日期和时间。下面是创建和操作 LocalDateTime
的示例:
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
// 创建 LocalDateTime 实例
LocalDateTime now = LocalDateTime.now();
LocalDateTime specificDateTime = LocalDateTime.of(2021, 10, 1, 14, 30, 0);
// 输出当前日期时间
System.out.println("Now is: " + now);
// 输出指定日期时间
System.out.println("Specific date time is: " + specificDateTime);
// 日期时间加减
LocalDateTime laterDateTime = now.plusDays(1).plusHours(2);
LocalDateTime earlierDateTime = now.minusMonths(1).minusMinutes(30);
System.out.println("Later date time is: " + laterDateTime);
System.out.println("Earlier date time is: " + earlierDateTime);
}
}
五、ZonedDateTime和ZoneId
在实际应用中,我们时常需要处理带时区的日期和时间。Java 8 时间API 提供了 ZonedDateTime
类和 ZoneId
类来赞成这一需求。
5.1 ZoneId
ZoneId
类描述时区。我们可以通过它来获取不同时区的当前时间。下面是使用 ZoneId
的示例:
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
// 获取指定时区的当前时间
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println("Current time in New York: " + zonedDateTime);
// 获取所有时区
ZoneId.getAvailableZoneIds().forEach(System.out::println);
}
}
5.2 ZonedDateTime
ZonedDateTime
类描述带时区的日期和时间。下面是创建和操作 ZonedDateTime
的示例:
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
// 创建 ZonedDateTime 实例
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZonedDateTime specificDateTime = ZonedDateTime.of(LocalDateTime.of(2021, 10, 1, 14, 30, 0), ZoneId.of("America/New_York"));
// 输出当前时区日期时间
System.out.println("Current time with zone is: " + zonedDateTime);
// 输出指定时区日期时间
System.out.println("Specific time with zone is: " + specificDateTime);
// 时区转换
ZonedDateTime convertedDateTime = specificDateTime.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));
System.out.println("Converted time with zone is: " + convertedDateTime);
}
}
六、DateTimeFormatter
DateTimeFormatter
类用于格式化日期时间。我们可以自定义日期时间的格式,也可以使用内置的格式。下面是使用 DateTimeFormatter
的示例:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 创建 LocalDateTime 实例
LocalDateTime now = LocalDateTime.now();
// 使用自定义格式
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(customFormatter);
System.out.println("Formatted date time: " + formattedDateTime);
// 使用内置格式
DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String isoFormattedDateTime = now.format(isoFormatter);
System.out.println("ISO formatted date time: " + isoFormattedDateTime);
}
}
七、总结
本文介绍了 Java 8 时间API 的基本概念和使用方法。通过使用这套API,我们可以更加方便、平安地处理日期时间相关的操作。在接下来的文章中,我们将继续深入学习 Java 8 时间API 的高级特性和应用。