|
@@ -1,7 +1,17 @@
|
|
|
package com.fdkankan.common.util;
|
|
|
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.ZonedDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
import java.util.GregorianCalendar;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.TimeZone;
|
|
|
+import lombok.SneakyThrows;
|
|
|
|
|
|
public class DateExtUtil {
|
|
|
|
|
@@ -30,4 +40,74 @@ public class DateExtUtil {
|
|
|
return gc.getTime();
|
|
|
}
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
+ public static void main(String[] args) {
|
|
|
+// DateExtUtil.testInstant();
|
|
|
+ DateExtUtil.testTransferBetweenZones();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //同一时刻,不同的时区的时间展现形式不一样,但是时刻一样,也就是时间戳是一样的
|
|
|
+
|
|
|
+ public static void testOldDate(){
|
|
|
+ Date currDate = new Date();
|
|
|
+ //国际统一时间字符串格式 Wed Apr 27 16:17:30 CST 2022 CST (china standar time)
|
|
|
+ System.out.println(currDate.toString());
|
|
|
+ //本地时间字符串格式 2022-4-27 16:17:30
|
|
|
+ System.out.println(currDate.toLocaleString());
|
|
|
+ //国际统一GMT(格林威治标准时间)字符串格式 27 Apr 2022 08:17:30 GMT
|
|
|
+ System.out.println(currDate.toGMTString());
|
|
|
+
|
|
|
+ String[] availableIDs = TimeZone.getAvailableIDs();
|
|
|
+ for (String availableID : availableIDs) {
|
|
|
+ System.out.println(availableID);
|
|
|
+ }
|
|
|
+
|
|
|
+ Date bjDate = new Date();
|
|
|
+ System.out.println("时间戳:" + bjDate.getTime());
|
|
|
+ //得到纽约的时区
|
|
|
+ TimeZone newYorkTimeZone = TimeZone.getTimeZone("America/New_York");
|
|
|
+ DateFormat newYorkDateFormat = new SimpleDateFormat(dateStyle);
|
|
|
+ newYorkDateFormat.setTimeZone(newYorkTimeZone);
|
|
|
+ //纽约时间
|
|
|
+ System.out.println("纽约时间:" + newYorkDateFormat.format(bjDate));
|
|
|
+ //背景时间
|
|
|
+ System.out.println("北京时间:" + new SimpleDateFormat(dateStyle).format(bjDate));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void testInstant(){
|
|
|
+ Instant instant = Instant.ofEpochMilli(new Date().getTime());
|
|
|
+ Date from = Date.from(instant);
|
|
|
+ System.out.println(instant.toEpochMilli());
|
|
|
+ System.out.println(from.getTime());
|
|
|
+ System.out.println(instant);
|
|
|
+ System.out.println(from);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void testTransferBetweenZones(){
|
|
|
+ //获取所有时区的id
|
|
|
+ Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
|
|
|
+
|
|
|
+ //模拟数据库查询出来的时间
|
|
|
+ Instant fromdb = Instant.now();
|
|
|
+
|
|
|
+ ZonedDateTime sydney = ZonedDateTime.ofInstant(fromdb, ZoneId.of("Australia/Sydney"));
|
|
|
+ ZonedDateTime beijing = ZonedDateTime.ofInstant(fromdb, ZoneId.systemDefault());
|
|
|
+ System.out.println(sydney.format(DateTimeFormatter.ofPattern("dd-MM-yy HH:mm:ss")));
|
|
|
+ System.out.println(beijing.format(DateTimeFormatter.ofPattern(dateStyle)));
|
|
|
+ for (String availableZoneId : availableZoneIds) {
|
|
|
+ System.out.println(availableZoneId);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|