1、写在前面
今天文章的内容是:
- 日期时间类
2、内容
2.1、Date类
(1) 介绍
java.util
包提供了Date
类来封装当前的日期时间,表示特定的时刻,精度为毫秒。
(2) 构造器
Date
类提供两个构造函数来实例化Date
对象。
Date( )
: 使用系统当前的日期和时间来初始化Date
对象,精度为毫秒。Date(long millisec)
: 有参构造器中的参数表示从 1970 年 11 月 11 日 起的毫秒数。
(3) 常用方法
getTime()
:- 返回自此
Date
对象表示的1970年11月11日00:00:0000:00:00 GMTGMT以来的毫秒数。 setTime()
:- 将此
Date
对象设置为表示格林尼治标准时间19701970年11月11日00:00:0000:00:00之后的time
毫秒的时间点。 after(Date date)
:- 判断当前对象是否在指定日期之后,结果返回
true
或false
。 before(Date date)
:- 判断当前对象是否在指定日期之前,结果返回
true
或false
。 equals(Object date)
:- 判断当前对象是否和指定日期相等,结果返回
true
或false
。 toString( )
:- 将
Date
对象转换为字符串。
(4) 举例
举个例子:
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);
}
}
// 运行结果:
// Thu Oct 06 11:30:30 CST 2022
2.2、SimpleDateFormat类
(1) 介绍
一般的,我们会使用SimpleDateFormat
类来格式化日期以及分析日期。也就是说,SimpleDateFormat
类可以将Date对象或时间毫秒值格式化成我们喜欢的时间形式,我们也可以将字符串的时间形式解析成日期对象。另外的,SimpleDateFormat
类允许我们使用自定义的日期时间格式来运行出结果。
小结,SimpleDateFormat
类的主要功能:
- 格式化:日期→文本
- 解析:文本→日期
(2) 构造器
SimpleDateFormat()
: 构造一个SimpleDateFormat
对象,使用默认格式。SimpleDateFormat(String pattern)
: 构造一个SimpleDateFormat
对象,使用指定格式。
(3) 常用方法
常用模式字符:
y
: 年M
: 月d
: 日H
: 时m
: 分s
: 秒a
: AM / PME
: 星期D
: 一年中的第一天
常用方法如下:
format(Date date)
: 将日期格式化成日期/时间字符串format(Object time)
: 将时间毫秒值格式化为日期/时间字符串parse(String source)
: 解析给定的时间字符串并生成日期对象
(4) 举例
举个例子:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
// 创建 SimpleDateFormat 对象 sdf
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
// 1\. 格式化日期对象
Date d = new Date();
String rs1 = sdf.format(d);
System.out.println(rs1);
// 2\. 格式化时间毫秒值
long time = System.currentTimeMillis();
String rs2 = sdf.format(time);
System.out.println(rs2);
}
}
// 运行结果:
// 2022年10月06日 11:40:30 星期四 下午
// 2022年10月06日 11:40:30 星期四 下午
2.3、扩展
(1) 介绍
从JDK8
开始,java.time
包提供了新的日期和时间API,严格区分了时刻、本地日期、本地时间以及对日期时间的运行会更加方便。
(2) 举例
LocalDate类
举个例子:
// LocalDate可获取与当天相关的信息
import java.time.LocalDate;
public class Test {
public static void main(String[] args) {
// 1\. 获取本地日期对象
LocalDate nowDate = LocalDate.now();
// 2\. 输出对象信息
System.out.println("今天是:" + nowDate);
// 3\. 分别获取对象 年 月 日 信息
System.out.println("年份: " + nowDate.getYear());
System.out.println("月份: " + nowDate.getMonthValue());
System.out.println("日期: " + nowDate.getDayOfMonth());
// 4\. 获取星期信息
System.out.println("星期:" + nowDate.getDayOfWeek());
// 5\. 获取月份信息
System.out.println("月份:" + nowDate.getMonth());
// 6\. 查看当年的第几天
System.out.println("今天是这一年的第"+ nowDate.getDayOfYear() +"天");
// 7\. 查看当周的第几天
System.out.println("这周已经过了" + nowDate.getDayOfWeek().getValue() + "天");
// 8\. 创建指定日期对象
LocalDate ld = LocalDate.of(2002, 11, 11);
System.out.println("我的生日:" + ld);
}
}
输出结果:
今天是:2022-10-06
年份: 2022
月份: 10
日期: 6
星期:THURSDAY
月份:OCTOBER
今天是这一年的第279天
这周已经过了4天
我的生日:2002-06-22
- 以下是一些增减日期的方法
方法名 | 说明 |
---|---|
plusDays() | 添加指定的天数 |
plusWeeks() | 添加指定周数 |
plusMonths() | 添加指定的月数 |
plusYears() | 添加指定的年数 |
minusDays() | 减去指定的天数 |
minusWeeks() | 减去指定的周数 |
minusMonths() | 减去指定的月数 |
minusYears() | 减去指定的年数 |
备注:以上方法都将返回LocalDate
对象的副本。
LocalTime类
import java.time.LocalTime;
public class Test {
public static void main(String[] args) {
// 1\. 获取本地时间对象
LocalTime nowTime = LocalTime.now();
// 2\. 输出当前具体时间
System.out.println("当前时间:" + nowTime);
// 3\. 输出 时 分 秒 纳秒
System.out.println("hour: " + nowTime.getHour());
System.out.println("minute: " + nowTime.getMinute());
System.out.println("second: " + nowTime.getSecond());
System.out.println("nano: " + nowTime.getNano());
// 4\. 输出指定时间
System.out.println(LocalTime.of(11, 11)); // 时分
System.out.println(LocalTime.of(11, 11, 11)); // 时分秒
System.out.println(LocalTime.of(11, 11, 11, 111)); // 时分秒纳秒
}
}
输出结果:
当前时间:11:30:28.368
hour: 11
minute: 30
second: 28
nano: 368000000
11:11
11:11:11
11:11:11.000000111
LocalDateTime类
import java.time.LocalDateTime;
public class Test {
public static void main(String[] args) {
// 1\. 获取日期时间对象
LocalDateTime now = LocalDateTime.now();
// 2\. 输出日期时间对象
System.out.println("今天是:"+now);
System.out.println("----------");
// 3\. 输出日期信息
System.out.println("年份: " + now.getYear());
System.out.println("月份: " + now.getMonthValue());
System.out.println("日期: " + now.getDayOfMonth());
System.out.println("星期:" + now.getDayOfWeek());
System.out.println("月份:" + now.getMonth());
System.out.println("----------");
// 4\. 输出时间信息
System.out.println("hour: " + now.getHour());
System.out.println("minute: " + now.getMinute());
System.out.println("second: " + now.getSecond());
System.out.println("nano: " + now.getNano());
}
}
输出结果:
今天是:2022-10-06T11:30:40.189
----------
年份: 2022
月份: 10
日期: 6
星期:THURSDAY
月份:OCTOBER
----------
hour: 11
minute: 30
second: 40
nano: 189000000
可以说,LocalDateTime
综合了LocalTime
和LocalDate
的相关方法,我们还可以将LocalDateTime
转换成日期对象或时间对象,如下所示:
public LocalDate toLocalDate()
: 将LocalDateTime
对象转换成一个LocalDate
对象public LocalTime toLocalTime()
: 将LocalDateTime
对象转换成一个LocalTime
对象
举个例子:
import java.time.LocalDateTime;
public class Test {
public static void main(String[] args) {
// 1\. 获取日期时间对象
LocalDateTime now = LocalDateTime.now();
// 2\. 转换成日期对象
System.out.println("当前日期:" + now.toLocalDate());
// 3\. 转换成时间对象
System.out.println("当前时间:" + now.toLocalTime());
}
}
输出结果:
当前日期:2022-10-06
当前时间:11:30:30.333
DateTimeFormatter类
DateTimeFormatter
类是一个新的日期时间格式化类。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
// 1\. 创建本地日期时间对象, 并输出对象内容
LocalDateTime now = LocalDateTime.now();
System.out.println("格式化前:"+now);
// 2\. 创建格式化器
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a");
// 3\. 开始格式化日期时间对象
System.out.println("格式化后:" + now.format(dtf)); // dtf.format(now)的效果也一样
}
}
输出结果:
格式化前:2022-10-06T11:30:30.866
格式化后:2022-10-06 11:30:30 星期四 下午
Period类
- 利用
Period
类,我们可以来计算日期间隔,一般用于LocalDate
对象之前的比较。
比如between
方法的使用:
public static Period between (LocalDate startDateInclusive, LocalDate endDateExclusive)
举个例子:
import java.time.LocalDate;
import java.time.Period;
public class Test {
public static void main(String[] args) {
// 1\. 创建当前日期对象
LocalDate now = LocalDate.now();
System.out.println("今天日期: " + now);
// 2\. 创建指定日期对象
LocalDate birthDate = LocalDate.of(2002, 6, 22);
System.out.println("我的生日: " + birthDate);
// 3\. 计算日期间隔
Period between = Period.between(birthDate, now);
System.out.printf("我已经生活了%d年%d月%d天", between.getYears(),between.getMonths(),between.getDays());
}
}
输出结果:
今天日期: 2022-10-06
我的生日: 2002-06-22
我已经生活了20年3月14天
Duration类
- 利用
Duration
类,我们可以更加方便地计算两个时间之间的间隔。
比如between
方法的使用,表示两个时间对象之间的持续时间:
public static Duration between (Temporal startInclusive, Temporal endExclusive)
举个例子:
import java.time.Duration;
import java.time.LocalDateTime;
public class Test {
public static void main(String[] args) {
// 1\. 创建当前日期时间对象
LocalDateTime now = LocalDateTime.now();
System.out.println("当前时刻: " + now);
// 2\. 创建指定日期时间对象
LocalDateTime birthDate = LocalDateTime.of(2002, 6, 22, 00, 00, 00);
System.out.println("我的生日: " + birthDate);
// 3\. 计算间隔时间
Duration between = Duration.between(birthDate, now);
System.out.printf("我已经生活了%d天\n", between.toDays());
}
}
输出结果:
当前时刻: 2022-10-06T11:30:30.268
我的生日: 2002-06-22T00:00
我已经生活了7411天