时间类
JDK8之前
java.util.Date
SimpleDateFormat
parse
format
abstartct Calendar(不介绍)
JDK8及之后
时区问题
直接加8小时(TimeUnit.HOURS.toMillis(8))
Instant默认时区UTC,比中国时间少8H
LocalDateTime会取本地时区,和中国本地时间一致(√ )
java.time.Instant
java.time.LocalDateTime
java.time.LocalDate
java.time.LocalTime
java.time.format.DateTimeFormatter
Date转Instant、Date转LocalDateTime
Insatnt转Date、LocalDateTime转Date
一:引入
1> java.util.Date包下的许多方法已经被废弃,不推荐使用
2> JDK8新增的时间类(java.time.xxx)约束更强,一般使用LocalDateTime
Date类想要获取当前系统时间的毫秒数(millisecond)需要new出该对象使用getTime()获取,在Instant和LocalDateTime类中可以使用静态方法now获取该对象使用toEpochMillis获取;
Instant和LocalDateTime有时区差异
Instant时区默认UTC
LocalDateTime时区默认本地时区
二:Date
package xyz.xx;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
时间类Date测试
*/
public class DateTest {
public static void main(String[] args) throws ParseException {
// 使用Date获取当前系统时间
System.out.println(new Date()); // Sat Oct 10 15:16:44 CST 2020
System.out.println(new Date().getTime()); // 1602314205069
// 创建Date对象
// 传入年月日时间构造Date已被废弃
Date date1 = new Date(1602307501568L);
// 解析与格式化(全部由SimpleDateFormat负责)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = sdf.parse("2020-10-10");
String date2String = sdf.format(date2);
System.out.println(date2String); // 2020-10-10
}
}
三:Instant(一般不使用)
package xyz.xx;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
/**
结果的Z表示ZoneOffset.UTC(可以进入ZoneID类的318行开始查看:JDK1.8)
*/
public class InstantTest {
public static void main(String[] args) {
// 获取当前系统时间,返回Instant对象
Instant now = Instant.now();
System.out.println(now); // 2020-10-10T05:41:31.998Z
// 时区问题
// 1. 直接加(TimeUnit在concurrent包下,线程安全类)
// now = now.plusMillis(TimeUnit.HOURS.toMillis(8));
// System.out.println(now);
// 如何获取毫秒
System.out.println(now.toEpochMilli()); // 1602314128461
// 创建Instant对象
// Instant类将构造函数私有化,对外主要提供三个方法用于创建该对象
// public static Instant ofEpochSecond(long epochSecond)
// public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment)
// public static Instant ofEpochMilli(long epochMilli)
// Date转换为Instant
Date date1 = new Date();
Instant instant1 = Instant.ofEpochMilli(date1.getTime());
System.out.println(instant1); // 2020-10-10T07:15:28.711Z
// 解析与格式化
// 结尾必须要有Z
String date2String = "2020-10-10T05:41:31.998Z";
Instant date2 = Instant.parse(date2String);
System.out.println(date2); // 2020-10-10T05:41:31.998Z
// 格式化可以转换为字符串进行提取
String[] ts = date2String.substring(0, date2String.length() - 1).split("T");
System.out.println(Arrays.toString(ts)); // [2020-10-10, 05:41:31.998]
//一般使用LocalDateTime会更加便捷
}
}
四:LocalDateTime(常用)
package xyz.xx;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
/**
和Instant同样是final并没有对外开放构造函数,但是对外提供了of方法构造当前对象
*/
public class LocalDateTimeTest {
public static void main(String[] args) {
// 获取当前时间
// 与Instant结果相比,没有以Z结束
// 这意味着Instant是可以和LocalDateTime进行转换的
LocalDateTime now = LocalDateTime.now();
System.out.println(now); // 2020-10-10T15:12:54.330
// 如何获取毫秒
long millis = now.toInstant(ZoneOffset.UTC).toEpochMilli();
System.out.println(millis); // 1602342774330
// LocalDateTime与Instant进行转换
// 1. toInstant直接转换
// 2. 通过toString的字符串转换
// 2020-10-10T15:05:17.155Z
System.out.println(now.toInstant(ZoneOffset.UTC)); // 2020-10-10T15:12:54.330Z
Instant now1 = Instant.now();
String now1New = now1.toString().substring(0,now1.toString().length()-1);
LocalDateTime localDateTime1 = LocalDateTime.parse(now1New);
System.out.println(localDateTime1); // 2020-10-10T07:12:54.409
// LocalDateTime创建对象的方式多种多样
// milliSecond 毫秒(-3)
// microSecond 微秒(-6)
// nanoOfSecond 纳秒(-9)
// 2020-10-10T03:58
// 2020-10-10T03:58:20
// 2020-10-10T03:58:20.000000999
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 10, 10, 3, 58);
LocalDateTime localDateTime3 = LocalDateTime.of(2020, 10, 10, 3, 58,20);
LocalDateTime localDateTime4 = LocalDateTime.of(2020, 10, 10, 3, 58,20,999);
System.out.println(localDateTime2);
System.out.println(localDateTime3);
System.out.println(localDateTime4);
// 解析与格式化
// 解析交给LocalDateTime自己做
// 格式化由DateTimeFormatter完成
String date6String = "2020-10-10T03:58:20.123567";
LocalDateTime localDateTime6 = LocalDateTime.parse(date6String);
System.out.println(localDateTime6); // 2020-10-10T03:58:20.123567
String date7String = "2020-10-10 12:12:12";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime7 = LocalDateTime.parse(date7String, dtf);
System.out.println(localDateTime7); // 2020-10-10T12:12:12
String date7StringNew = dtf.format(localDateTime7);
System.out.println(date7StringNew); // 2020-10-10 12:12:12
}
}
五:Date转Instant、Date转LocalDateTime
Date -> Insatnt :自动-8H,因为Insatnt默认时区UTC;
Instant -> Date :自动+8H,因为Date默认本地时区;
/**
Date -> Instant(时区默认会有变换,注意手动增加)
Date -> [ Instant -> [ZonedDateTime] ] -> LocalDateTime
*/
private static void Old2New(){
Date date1 = new Date();
System.out.println(date1); // Sat Oct 10 18:01:21 CST 2020
// -8H,因为Instant默认UTC时区,Date默认本地时区
// Instant instant1 = date1.toInstant(); // 2020-10-10T09:56:22.043Z
// 手动+8H
Instant instant1 = date1.toInstant().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(instant1); // 2020-10-10T17:57:26.844Z
LocalDateTime localDateTime1 = instant1.atZone(ZoneId.of("UTC")).toLocalDateTime();
System.out.println(localDateTime1); // 2020-10-10T18:00:49.032
// Date转LocalDateTime
// 通过Insatnt中转时不需要+8H (起点终点时区保持一致)
LocalDateTime localDateTime2 = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println(localDateTime2); // 2020-10-10T18:00:49.032
}
六:Insatnt转Date、LocalDateTime转Date
Date -> Insatnt :自动-8H,因为Insatnt默认时区UTC;
Instant -> Date :自动+8H,因为Date默认本地时区;
/**
*/
private static void New2Old(){
LocalDateTime localDateTime1 = LocalDateTime.now();
System.out.println(localDateTime1); // 2020-10-10T18:05:17.157
// ZoneOffset参数表示LocalDateTime在转Instant时候,自己相对UTC标准偏移量
// 细节进入ZoneOffset源码即可查看[202]
Instant instant1 = localDateTime1.toInstant(ZoneOffset.of("+8"));
System.out.println(instant1); // 2020-10-10T10:05:17.157Z
// 会在Instant的基础上+8H,需要在LocalDateTime转Instant时将Instant值手动-8H
Date date1 = Date.from(instant1);
System.out.println(date1); // Sat Oct 10 18:05:17 CST 2020