简介
JDK8以前都是通过java.util.Date与 SimpleDateFormatter格式化时间获得时间,但是这些都不是线程安全的,JDK8带给我们新的获取日期的方式。
实战测试
java.time类简介
Date和time做下区分,Date(日期)的单位是年月日。time(时间)的单位是时分秒
类 | 描述 |
---|---|
Instant | 时间戳(瞬时时间,带时区) |
LocalDate | 日期(比如:2018-09-24,不带时区) |
LocalTime | 时间(比如:10:32:10,不带时区) |
LocalDateTime | 日期时间(比如:2018-09-24 10:32:10,不带时区) |
Duration | 两个时间的差,精确到秒或纳秒 |
Peroid | 两个日期的差(精确到日) |
DateTimeFormatter | 日期时间格式化类 |
ZoneId | 时区 |
ZoneOffset | 时区偏移量(比如:+8:00) |
ZonedDateTime | 带时区的日期时间 |
ChronoUnit | 日期枚举类(在时间加减操作可用到) |
MonthDay | 月日 |
YearMonth | 年月 |
测试
测试代码如下:
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* NAME_ASCENDING 按照名字排序
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DateTest {
@Test
public void test01Jdk7DateNow() {
System.out.println("-------------------test01Jdk7DateNow------------------------");
Date dateNow = new Date();
System.out.println("Jdk7获取时间: " + dateNow);
}
@Test
public void test02Jdk8DateNow() {
System.out.println("\r\n-------------------test02Jdk8DateNow------------------------");
LocalDate dateNow = LocalDate.now();
System.out.println("Jdk8获取时间: " + dateNow);
}
@Test
public void test03Jdk7SimpleDateFormat() {
System.out.println("\r\n-------------------test03Jdk7SimpleDateFormat------------------------");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateNow = new Date();
String strNow = sdf.format(dateNow);
System.out.println("SimpleDateFormat格式化后: " + strNow);
}
/**
* DateTimeFormatter 格式化时间
* 使用"yyyy-MM-dd HH:mm:ss"格式化会报以下异常:
* java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
* 原因:
* 因为LocalDate中是不包含时分秒的,想要格式化时分秒需要用LocalDateTime这个类,里边包含的有时分秒
*/
@Test
public void test04Jdk8DateTimeFormatter() {
System.out.println("\r\n-------------------test04Jdk8DateTimeFormatter------------------------");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate dateNow = LocalDate.now();
String strNow = dateNow.format(dtf);
System.out.println("DateTimeFormatter格式化后: " + strNow);
}
/**
* DateTimeFormatter 格式化时间
* 因为LocalDate中是不包含时分秒的,想要格式化时分秒需要用LocalDateTime这个类,里边包含的有时分秒
*/
@Test
public void test05Jdk8LocalDateTime() {
System.out.println("\r\n-------------------test05Jdk8LocalDateTime------------------------");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateNow = LocalDateTime.now();
String strNow = dateNow.format(dtf);
System.out.println("DateTimeFormatter格式化后: " + strNow);
}
/**
* 时间和字符串之间的转换
*/
@Test
public void test06Jdk8String2LocalDate() {
System.out.println("\r\n-------------------test06Jdk8String2LocalDate------------------------");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateNow = LocalDateTime.now();
System.out.println("LocalDateTime(原始): " + dateNow);
String strNow = dateNow.format(dtf);
System.out.println("LocalDateTime转String后(格式化后): " + strNow);
// 将字符串转LocalDateTime
LocalDateTime str2LocalDateTime = LocalDateTime.parse(strNow, dtf);
System.out.println("String转LocalDateTime(格式化后): " + str2LocalDateTime);
}
/**
* Date和LocalDateTime之间的转换
*/
@Test
public void test07Jdk8Date2LocalDateTime() {
System.out.println("\r\n-------------------test07Jdk8Date2LocalDateTime------------------------");
// date转LocalDateTime
Date date = new Date();
System.out.println("date转LocalDateTime: " + date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
// LocalDateTime转date
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("LocalDateTime转date: " + Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
}
/**
* Timestamp和LocalDateTime之间的转换
*/
@Test
public void test08Jdk8Timestamp2LocalDateTime() {
System.out.println("\r\n-------------------test08Jdk8Timestamp2LocalDateTime------------------------");
// 或者使用 new Date().getTime()
long timestamp = System.currentTimeMillis();
System.out.println("Timestam转换之前:" + timestamp);
Instant instant1 = Instant.ofEpochMilli(timestamp);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());
System.out.println("Timestamp转LocalDateTime:" + localDateTime);
long timestamp1 = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
System.out.println("LocalDateTime转Timestamp:" + timestamp1);
}
}
测试结果如下:
-------------------test01Jdk7DateNow------------------------
Jdk7获取时间: Tue May 10 16:48:50 CST 2022
-------------------test02Jdk8DateNow------------------------
Jdk8获取时间: 2022-05-10
-------------------test03Jdk7SimpleDateFormat------------------------
SimpleDateFormat格式化后: 2022-05-10 16:48:50
-------------------test04Jdk8DateTimeFormatter------------------------
DateTimeFormatter格式化后: 2022-05-10
-------------------test05Jdk8LocalDateTime------------------------
DateTimeFormatter格式化后: 2022-05-10 16:48:50
-------------------test06Jdk8String2LocalDate------------------------
LocalDateTime(原始): 2022-05-10T16:48:50.214
LocalDateTime转String后(格式化后): 2022-05-10 16:48:50
String转LocalDateTime(格式化后): 2022-05-10T16:48:50
-------------------test07Jdk8Date2LocalDateTime------------------------
date转LocalDateTime: 2022-05-10T16:48:50.218
LocalDateTime转date: Tue May 10 16:48:50 CST 2022
-------------------test08Jdk8Timestamp2LocalDateTime------------------------
Timestam转换之前:1652172530220
Timestamp转LocalDateTime:2022-05-10T16:48:50.220
LocalDateTime转Timestamp:1652172530220
其他常用API
其他常用API,遇到了再研究吧