常用日期和时间API
# 常用日期和时间 API
前言
Java 中的日期和时间 API 提供了多种类和方法,常见的有 Date
、DateFormat
、SimpleDateFormat
、LocalDateTime
、Duration
、Period
等。
# 1. Date 类
Date
类是 Java 早期的日期时间类,主要用于表示时间点。尽管在 JDK 1.8 之后,LocalDateTime
逐渐成为主流,但 Date
类仍然在处理时间戳和与旧系统兼容时广泛使用。
构造器与方法
构造方法和方法 | 作用说明 |
---|---|
public Date() | 创建表示当前时间的 Date 对象 |
public Date(long time) | 通过指定的毫秒值创建 Date 对象 |
public long getTime() | 返回从 1970 年 1 月 1 日 00:00:00 GMT 开始的毫秒数 |
public void setTime(long time) | 设置时间,以毫秒为单位 |
import java.util.Date;
public class DateExample {
public static void main(String[] args) {
// 创建当前时间的Date对象
Date now = new Date();
System.out.println("当前时间:" + now);
// 获取当前时间的毫秒值
long currentTimeMillis = now.getTime();
System.out.println("当前时间的毫秒值:" + currentTimeMillis);
// 将时间向后推121秒
Date futureDate = new Date(currentTimeMillis + 121 * 1000);
System.out.println("121秒后的时间:" + futureDate);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
实际场景:Date
类适用于记录日志时间、生成时间戳、测量程序运行时间等。
# 2. DateFormat 类与 SimpleDateFormat 类
DateFormat
是一个抽象类,提供了日期和时间的格式化与解析功能。SimpleDateFormat
是其常用的子类,允许用户定义自定义日期时间格式。
常用方法与格式化模式
方法 | 作用说明 |
---|---|
public String format(Date date) | 将 Date 对象格式化为字符串 |
public Date parse(String source) | 将字符串解析为 Date 对象 |
格式化模式 | "yyyy-MM-dd HH:mm:ss" 、"EEE, MMM d, ''yy" 、"h:mm a" |
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) throws Exception {
// 创建当前日期对象
Date now = new Date();
// 使用自定义格式创建SimpleDateFormat对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化日期对象
String formattedDate = sdf.format(now);
System.out.println("格式化后的日期:" + formattedDate);
// 解析字符串为日期对象
String dateStr = "2024-08-24 15:30:00";
Date parsedDate = sdf.parse(dateStr);
System.out.println("解析后的日期:" + parsedDate);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
实际场景:SimpleDateFormat
常用于将日期格式化为自定义格式(如在 UI 界面显示),或者将用户输入的日期字符串解析为 Date
对象。
# 3. LocalDateTime 类 (JDK 1.8+)
LocalDateTime
是 JDK 1.8 新增的日期时间类,具有更好的线程安全性,常用于替代 Date
类。它与 LocalDate
和 LocalTime
一起构成了新的日期时间 API。
构造方法与常用 API
方法名称 | 作用说明 |
---|---|
public static LocalDateTime now() | 获取当前系统日期时间 |
public static LocalDateTime of(年, 月, 日, 时, 分, 秒) | 创建指定年月日和时分秒的 LocalDateTime 对象 |
public String format(DateTimeFormatter formatter) | 将 LocalDateTime 对象格式化为字符串 |
public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) | 将字符串解析为 LocalDateTime 对象 |
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample {
public static void main(String[] args) {
// 获取当前系统日期时间
LocalDateTime now = LocalDateTime.now();
System.out.println("当前日期时间:" + now);
// 指定日期时间创建LocalDateTime对象
LocalDateTime specificDateTime = LocalDateTime.of(2024, 8, 24, 15, 30, 0);
System.out.println("指定日期时间:" + specificDateTime);
// 格式化LocalDateTime对象
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println("格式化后的日期时间:" + formattedDateTime);
// 解析日期字符串为LocalDateTime对象
String dateTimeStr = "2024-08-24 15:30:00";
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeStr, formatter);
System.out.println("解析后的日期时间:" + parsedDateTime);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
实际场景:LocalDateTime
适用于日期时间计算、定时任务管理、日志记录和各种时间相关的业务逻辑处理中。
# 4. Duration 与 Period 类 (JDK 1.8+)
Duration
处理时间间隔(如秒、纳秒),Period
处理日期间隔(如年、月、日)。
常用方法
方法 | 作用说明 |
---|---|
public static Duration between(Temporal start, Temporal end) | 计算两个时间点之间的间隔 |
public static Period between(LocalDate startDate, LocalDate endDate) | 计算两个日期之间的间隔 |
public int getYears() | 获取间隔的年数 |
public long toHours() | 获取间隔的小时数(Duration ) |
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
public class DurationPeriodExample {
public static void main(String[] args) {
// 计算日期间隔
LocalDate startDate = LocalDate.of(2020, 1, 1);
LocalDate endDate = LocalDate.of(2024, 8, 24);
Period period = Period.between(startDate, endDate);
System.out.println("年数:" + period.getYears());
System.out.println("月数:" + period.getMonths());
System.out.println("天数:" + period.getDays());
// 计算时间间隔
LocalDateTime startTime = LocalDateTime.of(2024, 8, 24, 8, 0, 0);
LocalDateTime endTime = LocalDateTime.of(2024, 8, 24, 15, 30, 0);
Duration duration = Duration.between(startTime, endTime);
System.out.println("小时数:" + duration.toHours());
System.out.println("分钟数:" + duration.toMinutes());
System.out.println("秒数:" + duration.getSeconds());
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
实际场景:Duration
和 Period
在计算任务间隔、分析数据变化周期、管理计时器等场景中非常有用。
# 5. DecimalFormat 类
DecimalFormat
是用于格式化数字的类,尤其在处理货币、百分比、科学计数法等场景时非常有用。
常用格式与方法
格式化模式 | 作用说明 |
---|---|
"0" | 保留整数部分 |
"0.00" | 保留两位小数 |
"#,###" | 以千位分隔符格式化 |
"#.##%" | 以百分比方式格式化 |
"#.#####E0" | 以科学计数法格式化 |
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
double pi = 3.1415927;
// 取一位整数
System.out.println(new DecimalFormat("0").format(pi)); // 3
// 取两位小数
System.out.println(new DecimalFormat("0.00").format(pi)); // 3.14
// 科学计数法显示并取五位小数
long speedOfLight = 299792458;
System.out.println(new DecimalFormat("#.#####E0").format(speedOfLight)); // 2.99792E8
// 每三位以逗号进行分隔
System.out.println(new DecimalFormat(",###").format(speedOfLight)); // 299,792,458
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
实际场景:DecimalFormat
在金融应用、数据展示、科学计算等场景中应用广泛,尤其适合需要将数字以特定格式输出时。
结论
Java 提供了多种日期和时间处理 API,可以根据具体需求选择合适的工具类。无论是传统的 Date
类,还是现代的 LocalDateTime
、Duration
等新特性,都能很好地满足日常开发中的日期时间需求。通过理解和掌握这些 API,开发者可以更加灵活、准确地处理复杂的日期时间场景。