Java 8 中的时间
Java 8 引入了一个全新的时间库,用于解决之前版本中日期和时间处理的问题。新的库叫做 java.time,它基于 Joda-Time 开发,并被视为 Joda-Time 的后继者。本篇教程将介绍如何使用 java.time 库进行日期和时间操作,以及这些操作的相关示例。
目录
- 1. LocalDate
- 2. LocalTime
- 3. LocalDateTime
- 4. ZonedDateTime
- 5. Period
- 6. Duration
- 7. DateTimeFormatter
1. LocalDate
LocalDate
类表示不带时间的日期,如 2021-09-30
。它可以用来处理年、月、日等信息。
javaCopy code
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(2020, 2, 29);
System.out.println("Specific Date: " + specificDate);
}
}
2. LocalTime
LocalTime
类表示一天中的某个时间,如 22:15:30
。它可以用来处理小时、分钟、秒和纳秒等信息。
javaCopy code
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(14, 30);
System.out.println("Specific Time: " + specificTime);
}
}
3. LocalDateTime
LocalDateTime
类表示日期和时间的组合,如 2021-09-30T22:15:30
。
javaCopy code
import java.time.LocalDateTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
// 获取当前日期时间
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date Time: " + currentDateTime);
// 创建指定日期时间
LocalDateTime specificDateTime = LocalDateTime.of(2020, 2, 29, 14, 30);
System.out.println("Specific Date Time: " + specificDateTime);
}
}
4. ZonedDateTime
ZonedDateTime
类表示带时区的日期和时间。您可以通过提供一个 ZoneId
来创建一个 ZonedDateTime 实例。
javaCopy code
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 Zoned Date Time: " + currentZonedDateTime);
// 获取特定时区的当前日期时间
ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime zonedDateTimeInSpecificZone = ZonedDateTime.now(zoneId);
System.out.println("Current Zoned Date Time in Specific Zone: " + zonedDateTimeInSpecificZone);
csharpCopy code
// 创建指定带时区的日期时间
ZonedDateTime specificZonedDateTime = ZonedDateTime.of(2020, 2, 29, 14, 30, 0, 0, zoneId);
System.out.println("Specific Zoned Date Time: " + specificZonedDateTime);
}
}
5. Period
Period
类表示两个 LocalDate
之间的时长。它主要用于处理以年、月、日为单位的差值。
javaCopy code
import java.time.LocalDate;
import java.time.Period;
public class PeriodExample {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2010, 1, 1);
LocalDate endDate = LocalDate.of(2020, 12, 31);
Period period = Period.between(startDate, endDate);
System.out.println("Years: " + period.getYears() + ", Months: " + period.getMonths() + ", Days: " + period.getDays());
}
}
6. Duration
Duration
类表示两个时间点之间的持续时间。它主要用于处理以秒和纳秒为单位的差值。
javaCopy code
import java.time.Duration;
import java.time.LocalTime;
public class DurationExample {
public static void main(String[] args) {
LocalTime startTime = LocalTime.of(14, 0);
LocalTime endTime = LocalTime.of(16, 30);
Duration duration = Duration.between(startTime, endTime);
System.out.println("Hours: " + duration.toHours() + ", Minutes: " + duration.toMinutes());
}
}
7. DateTimeFormatter
DateTimeFormatter
类用于将日期、时间和带时区的日期时间转换为字符串,或从字符串转换回来。
javaCopy code
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDateTime dateTime = LocalDateTime.now();
// 格式化
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = date.format(dateFormatter);
String formattedDateTime = dateTime.format(dateTimeFormatter);
System.out.println("Formatted Date: " + formattedDate);
System.out.println("Formatted Date Time: " + formattedDateTime);
// 解析
LocalDate parsedDate = LocalDate.parse("2020-02-29", dateFormatter);
LocalDateTime parsedDateTime = LocalDateTime.parse("2020-02-29 14:30:00", dateTimeFormatter);
System.out.println("Parsed Date: " + parsedDate);
System.out.println("Parsed Date Time: " + parsedDateTime);
}
}
希望这篇教程能够帮助你了解 Java 8 中的时间库。现在你可以开始使用 java.time 库来处理日期和时间相关的任务。不断练习和尝试是学习的关键。
转载自:https://juejin.cn/post/7222075996064890917