文章目录
- 日期类
- 第一代日期类
- 第二代日期类
- 第三代日期类
- LocalDateTime方法
- LocalDateTime格式化
- 日期与时间戳的转换
- Date转换为时间戳
- 时间戳转换为Date
日期类
在Java中,有三代日期类:
java.util.Date
、java.util.Calendar
和java.time
包下的日期类。这三代日期类在不同的Java版本中引入和改进,每一代都提供了更强大和更易于使用的日期和时间处理功能。
第一代日期类
第一代日期类:java.util.Date
java.util.Date
是Java最早引入的日期类,它表示从1970年1月1日开始的毫秒数。尽管它提供了基本的日期和时间操作方法,但它在设计上存在一些问题。其中一个主要问题是它的可变性,即一旦创建了一个Date
对象,就无法更改其值。此外,它的大部分方法已被标记为过时,不推荐使用。
第二代日期类
第二代日期类:java.util.Calendar
java.util.Calendar
是Java 1.1版本引入的日期类,用于弥补Date
类的不足。Calendar
类提供了更多的日期和时间操作方法,如获取特定日期的年、月、日、时、分、秒等。它还提供了一些用于日期和时间计算的方法,如添加或减去特定的时间单位。但是,Calendar
类的设计也存在一些问题。它的月份从0开始,即0表示一月,11表示十二月,这在使用时容易引起混淆。此外,Calendar
类也不是线程安全的。
第三代日期类
第三代日期类:java.time
包下的日期类
java.time
包是在Java 8中引入的,它提供了一组全新的日期和时间类,用于取代旧的Date
和Calendar
类。这些新的日期类是不可变的,线程安全的,并且提供了更加清晰和易于使用的API。主要的日期类包括:
LocalDate
:表示日期,如年、月、日。LocalTime
:表示时间,如时、分、秒。LocalDateTime
:表示日期和时间,如年、月、日、时、分、秒。ZonedDateTime
:表示带有时区的日期和时间。Instant
:表示时间戳,精确到纳秒级别。Duration
:表示时间的持续时间。Period
:表示日期的持续时间。
示例代码:
public class Test1 {public static void main(String[] args) {// 创建 LocalDate 对象表示当前日期(年月日)LocalDate currentDate = LocalDate.now();System.out.println("当前日期: " + currentDate);// 创建 LocalTime 对象表示当前时间(时分秒)LocalTime currentTime = LocalTime.now();System.out.println("当前时间: " + currentTime);// 创建 LocalDateTime 对象表示当前日期和时间(年月日时分秒)LocalDateTime currentDateTime = LocalDateTime.now();System.out.println("当前日期和时间: " + currentDateTime);// 创建带有时区的 ZonedDateTime 对象ZonedDateTime zonedDateTime = ZonedDateTime.now();System.out.println("当前日期和时间(带时区): " + zonedDateTime);// 创建 Instant 对象表示当前时间戳Instant instant = Instant.now();System.out.println("当前时间戳: " + instant);// 转换为以毫秒为单位的时间戳long epochMilli = instant.toEpochMilli(); System.out.println("毫秒时间戳: " + epochMilli);// 计算两个时间之间的持续时间LocalDateTime startDateTime = LocalDateTime.of(2024, 1, 1, 0, 0, 0);LocalDateTime endDateTime = LocalDateTime.of(2024, 1, 1, 12, 0, 0);Duration duration = Duration.between(startDateTime, endDateTime);System.out.println("时间间隔: " + duration);// 计算两个日期之间的持续时间LocalDate startDate = LocalDate.of(2024, 1, 1);LocalDate endDate = LocalDate.of(2024, 12, 31);Period period = Period.between(startDate, endDate);System.out.println("日期间隔: " + period);}
}
输出:
当前日期: 2024-03-07
当前时间: 09:19:30.221607600
当前日期和时间: 2024-03-07T09:19:30.221607600
当前日期和时间(带时区): 2024-03-07T09:19:30.222605800+08:00[Asia/Shanghai]
当前时间戳: 2024-03-07T01:19:30.222605800Z
毫秒时间戳: 1709775409194
时间间隔: PT12H
日期间隔: P11M30D
LocalDateTime方法
LocalDateTime currentDateTime = LocalDateTime.now();System.out.println("年=" + currentDateTime.getYear());
System.out.println("月=" + currentDateTime.getMonth());
System.out.println("月=" + currentDateTime.getMonthValue());
System.out.println("日=" + currentDateTime.getDayOfMonth());
System.out.println("时=" + currentDateTime.getHour());
System.out.println("分=" + currentDateTime.getMinute());
System.out.println("秒=" + currentDateTime.getSecond());
输出:
年=2024
月=MARCH
月=3
日=7
时=9
分=22
秒=14
LocalDateTime格式化
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 格式化LocalDateTime
System.out.println("格式化后:"+currentDateTime.format(formatter));
输出:
格式化后:2024-03-07 09:23:22
日期与时间戳的转换
Date转换为时间戳
LocalDateTime dateTime = LocalDateTime.now();
long timestamp = dateTime.toEpochSecond(ZoneOffset.UTC);
System.out.println("LocalDateTime转化为时间戳:"+timestamp);
输出:
Date转化为时间戳:2024-03-07T01:37:43.387Z
时间戳转换为Date
Instant now = Instant.now();
Date date = Date.from(now);
System.out.println("时间戳转换为Date:"+now);
输出:
时间戳转换为Date:2024-03-07T01:39:23.786778200Z