Java Date 类
Java 中的 Date
类位于 java.util
包中,它表示特定的瞬间,精确到毫秒。
Date 类主要用于表示日期和时间信息,是 Java 中处理日期和时间的基础类之一。
需要注意的是,Java 8 之后引入了新的日期时间 API (java.time
包),它比 Date
类更加强大和易用。但在很多遗留代码中,你仍然会看到 Date
类的使用。
Date 类的基本使用
创建 Date 对象
Date 类有多个构造函数,最常用的有两种方式:
实例
// 创建一个表示当前时间的 Date 对象
Date currentDate = new Date();
// 创建一个指定时间的 Date 对象 (从1970年1月1日00:00:00 GMT开始的毫秒数)
Date specificDate = new Date(1640995200000L); // 2022年1月1日00:00:00 GMT
Date currentDate = new Date();
// 创建一个指定时间的 Date 对象 (从1970年1月1日00:00:00 GMT开始的毫秒数)
Date specificDate = new Date(1640995200000L); // 2022年1月1日00:00:00 GMT
常用方法
Date 类提供了几个常用的方法:
实例
Date date = new Date();
// 获取自1970年1月1日00:00:00 GMT以来的毫秒数
long timeInMillis = date.getTime();
// 设置时间 (从1970年1月1日00:00:00 GMT开始的毫秒数)
date.setTime(1640995200000L);
// 比较两个日期
Date anotherDate = new Date();
int result = date.compareTo(anotherDate); // 小于返回-1,等于返回0,大于返回1
// 检查日期是否在指定日期之后
boolean isAfter = date.after(anotherDate);
// 检查日期是否在指定日期之前
boolean isBefore = date.before(anotherDate);
// 获取自1970年1月1日00:00:00 GMT以来的毫秒数
long timeInMillis = date.getTime();
// 设置时间 (从1970年1月1日00:00:00 GMT开始的毫秒数)
date.setTime(1640995200000L);
// 比较两个日期
Date anotherDate = new Date();
int result = date.compareTo(anotherDate); // 小于返回-1,等于返回0,大于返回1
// 检查日期是否在指定日期之后
boolean isAfter = date.after(anotherDate);
// 检查日期是否在指定日期之前
boolean isBefore = date.before(anotherDate);
Date 类的格式化
Date 类本身不提供格式化的功能,但我们可以使用 SimpleDateFormat
类来格式化和解析日期。
日期格式化示例
实例
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date date = new Date();
// 创建格式化对象
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 E HH时mm分ss秒");
// 格式化日期
String formattedDate1 = sdf1.format(date);
String formattedDate2 = sdf2.format(date);
System.out.println("格式1: " + formattedDate1);
System.out.println("格式2: " + formattedDate2);
}
}
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date date = new Date();
// 创建格式化对象
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 E HH时mm分ss秒");
// 格式化日期
String formattedDate1 = sdf1.format(date);
String formattedDate2 = sdf2.format(date);
System.out.println("格式1: " + formattedDate1);
System.out.println("格式2: " + formattedDate2);
}
}
常用格式模式
字母 | 含义 | 示例 |
---|---|---|
y | 年 | yyyy → 2023 |
M | 月 | MM → 01, MMM → Jan |
d | 日 | dd → 05 |
H | 小时(24小时制) | HH → 13 |
h | 小时(12小时制) | hh → 01 |
m | 分钟 | mm → 30 |
s | 秒 | ss → 45 |
S | 毫秒 | SSS → 789 |
E | 星期 | E → 星期一 |
a | 上午/下午标记 | a → 下午 |
Date 类的局限性
虽然 Date 类在很多情况下仍然可用,但它有一些明显的局限性:
- 不是线程安全的:Date 对象是可变的,这在多线程环境中可能会出现问题。
- 设计不佳:月份从0开始(0表示1月),年份从1900年开始计算,这容易导致混淆。
- 时区处理复杂:Date 类本身不包含时区信息,时区处理需要额外的类。
- 功能有限:缺乏许多常用的日期操作功能,如加减天数、周数等。
替代方案:Java 8 日期时间 API
Java 8 引入了新的日期时间 API (java.time
包),它解决了 Date 类的许多问题:
实例
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class NewDateTimeExample {
public static void main(String[] args) {
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// 格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println("当前时间: " + formattedDateTime);
}
}
import java.time.format.DateTimeFormatter;
public class NewDateTimeExample {
public static void main(String[] args) {
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// 格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println("当前时间: " + formattedDateTime);
}
}
新的日期时间 API 提供了更多功能,如:
LocalDate
- 只包含日期LocalTime
- 只包含时间LocalDateTime
- 包含日期和时间ZonedDateTime
- 包含日期、时间和时区- 丰富的日期操作方法
总结
Date
类是 Java 中表示日期和时间的基本类,但有一些局限性。- 使用
SimpleDateFormat
可以格式化和解析日期字符串。 - 在 Java 8 及以上版本,推荐使用新的
java.time
API 替代Date
类。 - 在维护旧代码时,了解
Date
类仍然很重要。
如果你正在开发新项目,建议优先考虑使用 Java 8 的新日期时间 API,它更加直观、强大且线程安全。
以下是 java.util.Date 类的常用方法:
构造方法
方法 | 描述 | 备注 |
---|---|---|
Date() |
创建一个表示当前时间的Date对象 | 已过时,推荐使用new Date() 或Calendar.getInstance() |
Date(long date) |
根据指定的毫秒数创建Date对象 | 毫秒数是从1970年1月1日00:00:00 GMT开始计算的 |
常用方法
方法 | 描述 | 备注 |
---|---|---|
boolean after(Date when) |
测试此日期是否在指定日期之后 | |
boolean before(Date when) |
测试此日期是否在指定日期之前 | |
Object clone() |
返回此对象的副本 | |
int compareTo(Date anotherDate) |
比较两个日期的顺序 | |
boolean equals(Object obj) |
比较两个日期的相等性 | |
long getTime() |
返回自1970年1月1日以来的毫秒数 | |
void setTime(long time) |
用毫秒数设置此Date对象 | |
String toString() |
将Date对象转换为字符串表示形式 | 格式如:"Tue Jun 22 13:07:00 CST 2021" |
已过时方法(不推荐使用)
方法 | 描述 | 替代方案 |
---|---|---|
int getDate() |
获取月份中的某天(1-31) | 使用Calendar.get(Calendar.DATE) |
int getDay() |
获取星期几(0-6) | 使用Calendar.get(Calendar.DAY_OF_WEEK) |
int getHours() |
获取小时(0-23) | 使用Calendar.get(Calendar.HOUR_OF_DAY) |
int getMinutes() |
获取分钟(0-59) | 使用Calendar.get(Calendar.MINUTE) |
int getMonth() |
获取月份(0-11) | 使用Calendar.get(Calendar.MONTH) |
int getSeconds() |
获取秒数(0-59) | 使用Calendar.get(Calendar.SECOND) |
int getYear() |
获取年份(从1900开始) | 使用Calendar.get(Calendar.YEAR) |
void setDate(int date) |
设置月份中的某天 | 使用Calendar.set(Calendar.DATE, date) |
void setHours(int hours) |
设置小时 | 使用Calendar.set(Calendar.HOUR_OF_DAY, hours) |
void setMinutes(int minutes) |
设置分钟 | 使用Calendar.set(Calendar.MINUTE, minutes) |
void setMonth(int month) |
设置月份 | 使用Calendar.set(Calendar.MONTH, month) |
void setSeconds(int seconds) |
设置秒数 | 使用Calendar.set(Calendar.SECOND, seconds) |
void setYear(int year) |
设置年份 | 使用Calendar.set(Calendar.YEAR, year) |
点我分享笔记