时间戳定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。因此,不管你处在地球上的哪个地方,任意时间点的时间戳都是相同的。这点有利于线上和客户端分布式应用统一追踪时间信息。但是不同的时区,当前时间戳对应的当前时间是不同的。
一张图说清楚转换规则:
/**
* @author liuzhulin
* @email 1392673190@qq.com
* @desc 时间对象、时间戳、时间字符串互相转换
* @create 2021/12/26
*/
public class TimeTest {
public static void main(String[] args) {
// 日期对象转字符串
dateObjToStr(LocalDateTime.now());
// 字符串转日期对象,秒
String str = "2021-12-26 15:05:20";
strToDateObj1(str);
// 字符串转日期对象,毫秒
String strMilli = "2021-12-26 15:05:20.123";
strToDateObj2(strMilli);
// 时间对象转时间戳
LocalDateTime localDateTime = LocalDateTime.of(2021, 12, 26, 13, 12, 11, 234 * 1000000);
dateObjToTimestamp(localDateTime);
// 时间戳转转时间对象,秒
int second = 1640495531;
timestampToDateObj(second);
// 时间戳转时间对象,毫秒
String milliSecond = "1640495531234";
long s = Integer.parseInt(milliSecond.substring(0, 10));
int ms = Integer.parseInt(milliSecond.substring(10));
timestampToDateObj(s, ms);
// 日期字符串转时间戳,秒
strToTimestamp(str);
// 日期字符串转时间戳,毫秒
strToMilliTimestamp(strMilli);
// 时间戳转日期字符串
timestampToStr(second);
timestampToStr(second, ms);
}
private static void timestampToStr(long second, int ms) {
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, ms * 1000000, ZoneOffset.of("+8"));
String str = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
System.out.println(str);
}
private static void timestampToStr(long second) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(second), ZoneId.systemDefault());
String str = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(str);
}
private static void strToMilliTimestamp(String milliSecond) {
LocalDateTime localDateTime2 = LocalDateTime.parse(milliSecond, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
long ms = localDateTime2.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(ms);
}
private static void strToTimestamp(String str) {
LocalDateTime localDateTime1 = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
long second = localDateTime1.toEpochSecond(ZoneOffset.of("+8"));
System.out.println(second);
}
private static void timestampToDateObj(long second, int milliSecond) {
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, milliSecond * 1000000, ZoneOffset.of("+8"));
System.out.println(localDateTime);
}
private static void timestampToDateObj(long second) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(second), ZoneId.systemDefault());
System.out.println(localDateTime);
}
private static void dateObjToTimestamp(LocalDateTime localDateTime) {
// 精确到秒
long second = localDateTime.toEpochSecond(ZoneOffset.of("+8"));
System.out.println(second);
// 精确到毫秒
long milliSecond = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(milliSecond);
}
private static void dateObjToStr(LocalDateTime dateTime) {
// 秒
String dateStr1 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(dateStr1);
// 毫秒
String dateStr2 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
System.out.println(dateStr2);
}
private static void strToDateObj2(String str) {
LocalDateTime localDateTime = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
System.out.println(localDateTime);
}
private static void strToDateObj1(String str) {
LocalDateTime localDateTime = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(localDateTime);
}
}