Java 8 在 java.time 包下引入了一个全新的日期和时间 API,旨在克服旧的 java.util.Date 和 java.util.Calendar 类的局限性。
为什么我们需要全新的日期 API?
- 非线程安全: 旧的 java.util.Date 类不是线程安全的,而新的日期时间 API 是不可变的,并且没有 setter 方法(设值方法)。
- 操作匮乏: 旧 API 中的日期操作非常少,但新 API 为我们提供了丰富的日期处理方法。
- 设计混乱: 旧 API 在日期、时间和时区的处理上混杂不清。
java.time 包中的核心类
- Local API(本地 API): LocalDate, LocalTime, LocalDateTime(不需要时区时使用)。
- Zoned API(时区 API): ZonedDateTime, ZoneId(在处理时区时使用)。
- Period 和 Duration: 分别表示基于日期的时间量(如年、月、日)和基于时间的时间量(如秒、纳秒)。
- ChronoUnit 枚举: 使用类型安全的单位(如 DAYS, WEEKS, YEARS)替换旧有的整型常量。
- TemporalAdjusters: 用于常见日期操作的实用工具(例如“本月第一天”、“下一个周六”)。
#### 示例 1:LocalDate, LocalTime, LocalDateTime
Java
import java.time.*;
import java.time.format.DateTimeFormatter;
public class Date {
public static void LocalDateTimeApi()
{
// the current date
LocalDate date = LocalDate.now();
System.out.println("the current date is "+ date);
// the current time
LocalTime time = LocalTime.now();
System.out.println("the current time is "+ time);
// will give us the current time and date
LocalDateTime current = LocalDateTime.now();
System.out.println("current date and time : "+ current);
// to print in a particular format
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formatedDateTime = current.format(format);
System.out.println("in formatted manner "+ formatedDateTime);
// printing months days and seconds
Month month = current.getMonth();
int day = current.getDayOfMonth();
int seconds = current.getSecond();
System.out.println("Month : "+month+" day : "+day+" seconds : "+seconds);
// printing some specified date
LocalDate date2 = LocalDate.of(1950,1,26);
System.out.println("the republic day :"+date2);
// printing date with current time.
LocalDateTime specificDate = current.withDayOfMonth(24).withYear(2016);
System.out.println("specific date with "+ "current time : "+specificDate);
}
// Driver code
public static void main(String[] args)
{
LocalDateTimeApi();
}
}
Output
the current date is 2021-09-23
the current time is 20:52:39.954238
current date and time : 2021-09-23T20:52:39.956909
in formatted manner 23-09-2021 20:52:39
Month : SEPTEMBER day : 23 seconds : 39
the republic day :1950-01-26
specific date with current time : 2016-09-24T20:52:39.956909
#### 示例 2:Zoned Date-Time API(带时区的日期时间 API)
Java
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Zone {
// Function to get Zoned Date and Time
public static void ZonedTimeAndDate()
{
LocalDateTime date = LocalDateTime.now();
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedCurrentDate = date.format(format1);
System.out.println("formatted current Date and"+ " Time : "+ formattedCurrentDate);
// to get the current zone
ZonedDateTime currentZone = ZonedDateTime.now();
System.out.println("the current zone is "+
ZoneId tokyo = ZoneId.of("Asia/Tokyo");
ZonedDateTime tokyoZone = currentZone.withZoneSameInstant(tokyo);
System.out.println("tokyo time zone is " + tokyoZone);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formatedDateTime = tokyoZone.format(format);
System.out.println("formatted tokyo time zone "+ formatedDateTime);
}
// Driver code
public static void main(String[] args)
{
ZonedTimeAndDate();
}
}
Output
formatted current Date and Time : 09-04-2018 06:21:13
the current zone is Etc/UTC
tokyo time zone is 2018-04-09T15:21:13.220+09:00[Asia/Tokyo]
formatted tokyo time zone 09-04-2018 15:21:13
#### 示例 3:Period 和 Duration
Java
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.Period;
public class Geekforgeeks {
public static void checkingPeriod()
{
LocalDate date1 = LocalDate.now();
LocalDate date2 = LocalDate.of(2014, Month.DECEMBER, 12);
Period gap = Period.between(date2, date1);
System.out.println("gap between dates " + "is a period of " + gap);
}
// Function to check duration
public static void checkingDuration()
{
LocalTime
“