Java中日期时间相关类
一、java.lang.System类
System类提供的public static long currentTimeMillis()
用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。此方法适于计算时间差。
计算世界时间的主要标准有:
- UTC(Coordinated Universal Time)
- GMT(Greenwich Mean Time)
- CST(Central Standard Time)
二、java.util.Date类
- 两个构造方法:
- Date():创建一个对应当前时间的Date对象
- Date(long date)创建指定毫秒数的Date对象
- 常用方法:
方法 | 介绍 |
---|---|
getTime() | 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。 |
toString() | 把此 Date 对象转换为以下形式的 String: dow mon ddhh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun, Mon, Tue,Wed, Thu, Fri, Sat),zzz是时间标准 |
public class Test01 {
public static void main(String[] args) {
//构造器一,Date()创建一个对应当前时间的Date对象
//获取系统当前时间
Date nowTime1 = new Date();
System.out.println(nowTime1);//Wed Jan 13 12:56:36 CST 2021
long time = nowTime.getTime();
System.out.println(time);//1610515896056
//构造器二,创建指定毫秒数的Date对象
Date nowTime2 = new Date(1610515896056L);
System.out.println(nowTime2);//Wed Jan 13 13:31:36 CST 2021
}
}
三、java.sql.Date类
java.sql.Date是java.util.Date的子类
- 方法:
Date(long date) 使用给定的毫秒时间价值构建 Date对象(构造方法)
String toString() 格式的日期格式为YYYY-MM-DD - java.sql.Date类和java.util.Date类的相互转换:
public class Test02 {
public static void main(String[] args) {
//创建java.sql.Date对象,其父类为java.util.Date
//利用毫秒数
java.sql.Date date1 = new java.sql.Date(1610515896056L);
System.out.println(date1);//2021-01-13
//如何将java.util.Date对象转换为java.sql.Date对象
//方法一:对于多态创建的
Date date2 = new java.sql.Date(1610515896056L);
//因为date2指向子类创建的对象,所以直接向下转型
java.sql.Date date3= (java.sql.Date)date2;
System.out.println(date2);//2021-01-13
//方法二:利用毫秒数进行转换
Date date4 = new Date();
//将date4转换成毫秒数再初始化java.sql.Date对象
java.sql.Date date5 = new java.sql.Date(date4.getTime());
System.out.println(date5);//2021-01-13
}
}
四、java.text.SimpleDateFormat类
- 格式化Date类型为字符串类型的日期
方法 | 作用 |
---|---|
SimpleDateFormat() | 默认的模式和语言环境创建对象 |
public SimpleDateFormat(String pattern) | 该构造方法可以用参数pattern指定的格式创建一个对象,该对象调用 |
public String format(Date date) | 方法格式化时间对象date |
- 解析字符串为Date类型
方法 | 作用 |
---|---|
public Date parse(String source) | 从给定字符串的开始解析文本,以生成一个日期 |
SimpleDateFormat有参构造方法中个字符所代表的的含义
public class Test03 {
public static void main(String[] args) throws ParseException {
// SimpleDateFormat对日期Date类的格式化和解析
// 1.两个操作:
// 1.1 格式化:日期 --->字符串
// 1.2 解析:格式化的逆过程,字符串 ---> 日期
//方法一:使用默认的构造方法
Date date = new Date();
System.out.println(date);//Wed Jan 13 21:13:37 CST 2021
SimpleDateFormat sdf1 = new SimpleDateFormat();
//格式化:日期 --->字符串
String format = sdf1.format(date);
System.out.println(format);//21-1-13 下午9:12
//解析:格式化的逆过程,字符串 ---> 日期
String str = "21-1-13 下午9:12";
Date date1 = sdf1.parse(str);
System.out.println(date1);//Wed Jan 13 21:12:00 CST 2021
//方法二:按照指定的方式格式化和解析:调用带参的构造方法
//格式化:日期 --->字符串
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format2 = sdf2.format(date);
System.out.println(format2);//2021-01-13 09:21:30
//解析:格式化的逆过程,字符串 ---> 日期
String str2 = "2021-01-13 09:21:30";
Date date2 = sdf2.parse(str2);
System.out.println(date2);//Wed Jan 13 09:21:30 CST 2021
}
}
五、JDK8 中新日期时间(java.time.包下)
(一)LocalDate、LocalTime、LocalDateTime 类
- LocalDate代表IOS格式(yyyy-MM-dd)的日期,可以存储 生日、纪念日等日期。
- LocalTime表示一个时间,而不是日期。
- LocalDateTime是用来表示日期和时间的
常用方法:
方法 | 描述 |
---|---|
now() / * now(ZoneId zone) | 静态方法,根据当前时间创建对象/指定时区的对象 |
of() | 静态方法,根据指定日期/时间创建对象 |
getDayOfMonth()/getDayOfYear() | 获得月份天数(1-31) /获得年份天数(1-366) |
getDayOfWeek() | 获得星期几(返回一个 DayOfWeek 枚举值) |
getMonth() | 获得月份, 返回一个 Month 枚举值 |
getMonthValue() / getYear() | 获得月份(1-12) /获得年份 |
getHour()/getMinute()/getSecond() | 获得当前对象对应的小时、分钟、秒 |
withDayOfMonth()/withDayOfYear()/withMonth()/withYear() | 将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象 |
plusDays(), plusWeeks(),plusMonths(), plusYears(),plusHours() | 向当前对象添加几天、几周、几个月、几年、几小时 |
minusMonths() / minusWeeks()/minusDays()/minusYears()/minusHours() | 从当前对象减去几月、几周、几天、几年、几小时 |
代码测试:
public class Test06 {
// LocalDate、LocalTime、LocalDateTime 类
public static void main(String[] args) {
//1.实例化---------
//方法一:now()获取当前的日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);//2021-01-13
System.out.println(localTime);//22:41:59.879
System.out.println(localDateTime);//2021-01-13T22:41:59.879
//方法二:of():设置指定的年、月、日、时、分、秒
LocalDateTime localDateTime1 = LocalDateTime.of(2021,1,1,22,18,22);
System.out.println(localDateTime1);//2021-01-01T22:18:22
//2.getXxx():获取相关的属性
//获取月份天数
System.out.println(localDateTime.getDayOfMonth());//13
//获取星期几
System.out.println(localDateTime.getDayOfWeek());//WEDNESDAY
//获取月份(枚举值)
System.out.println(localDateTime.getMonth());//JANUARY
//获取月份(1-12)
System.out.println(localDateTime.getMonthValue());//1
//获取当前对象对应的分钟
System.out.println(localDateTime.getMinute());//41
//3.withXxx():设置相关的属性,并返回新的对象
//将对象的月份改为22并返回新的对象
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);//2021-01-13
System.out.println(localDate1);//2021-01-22
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDateTime);//2021-01-13T22:41:59.879
System.out.println(localDateTime2);//2021-01-13T04:41:59.879
//4.plusXxx():对相关属性进行加操作,并返回新的对象
LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
System.out.println(localDateTime);//2021-01-13T22:41:59.879
System.out.println(localDateTime3);//2021-04-13T22:41:59.879
//5.minusXxx():对相关属性进行减操作,并返回新的对象
LocalDateTime localDateTime4 = localDateTime.minusDays(6);
System.out.println(localDateTime);//2021-01-13T22:41:59.879
System.out.println(localDateTime4);//2021-01-07T22:41:59.879
}
}
(三)import java.time.Instant类
类似于 java.util.Date类
- 方法:
方法 | 描述 |
---|---|
now() | 静态方法,返回默认UTC时区的Instant类的对象 |
ofEpochMilli(long epochMilli) | 静态方法,返回在1970-01-01 00:00:00基础上加上指定毫秒数之后的Instant类的对象 |
atOffset(ZoneOffset offset) | 结合即时的偏移来创建一个 OffsetDateTime |
toEpochMilli() | 返回1970-01-01 00:00:00到当前时间的毫秒数,即为时间戳 |
public class Test07 {
public static void main(String[] args) {
/*
Instant的使用
类似于 java.util.Date类
*/
//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);//2021-01-13T14:59:45.594Z
//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2021-01-13T22:59:45.594+08:00
//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime()
long milli = instant.toEpochMilli();
System.out.println(milli);//1610549985594
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1610549985594L);
System.out.println(instant1);//2021-01-13T14:59:45.594Z
}
}
(四)java.time.format.DateTimeFormatter 类
- 三种格式化方式:
- 预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
- 本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)
- 自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
- 方法:
方法 | 描述 |
---|---|
ofPattern(String pattern) | 静 态 方 法 , 返 回 一 个 指 定 字 符 串 格 式 的DateTimeFormatter |
format(TemporalAccessor t) | 格式化一个日期、时间,返回字符串 |
parse(CharSequence text) | 将指定格式的字符序列解析为一个日期、时间 |
代码测试:
public class Test08 {
public static void main(String[] args) {
//DateTimeFormatter的三种格式化方式
// 方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
// 格式化:日期-->字符串
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter.format(localDateTime);
System.out.println(localDateTime);//2021-01-13T23:06:19.827
System.out.println(str1);//2021-01-13T23:06:19.827
//解析:字符串 -->日期
TemporalAccessor parse = formatter.parse("2021-01-13T23:06:19.827");
System.out.println(parse);
// 方式二:
// 本地化相关的格式。如:ofLocalizedDateTime()
// FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//2021年1月13日 下午11时06分43秒
// 本地化相关的格式。如:ofLocalizedDate()
// FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
//格式化
String str3 = formatter2.format(LocalDate.now());
System.out.println(str3);//2021年1月13日 星期三
// 重点: 方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);//2021-01-13 11:07:19
//解析
TemporalAccessor accessor = formatter3.parse("2021-01-13 11:06:19");
System.out.println(accessor);
}
}