Browse Source

时间戳获取

tags/正式版本
雍文秀 2 years ago
parent
commit
85984cf075

+ 1
- 1
scommons/src/main/java/com/xhkjedu/utils/N_Utils.java View File

@@ -107,7 +107,7 @@ public class N_Utils {
107 107
      * @date 2019年3月6日 下午10:13:50
108 108
      */
109 109
     public static int getSecondTimestamp(){
110
-        String timestamp = String.valueOf(System.currentTimeMillis()/1000);
110
+        String timestamp = String.valueOf(SystemClock.millisClock().now()/1000);
111 111
         return Integer.valueOf(timestamp);
112 112
     }
113 113
 

+ 41
- 0
scommons/src/main/java/com/xhkjedu/utils/SystemClock.java View File

@@ -0,0 +1,41 @@
1
+package com.xhkjedu.utils;
2
+
3
+import java.util.concurrent.Executors;
4
+import java.util.concurrent.ScheduledExecutorService;
5
+import java.util.concurrent.TimeUnit;
6
+import java.util.concurrent.atomic.AtomicLong;
7
+
8
+/**
9
+ * @author ywx
10
+ * @classname SystemClock
11
+ * @description todo
12
+ * @date 2022/11/9 20:31
13
+ **/
14
+public class SystemClock {
15
+    private static final SystemClock MILLIS_CLOCK = new SystemClock(1);
16
+    private final long precision;
17
+    private final AtomicLong now;
18
+
19
+    private SystemClock(long precision) {
20
+        this.precision = precision;
21
+        now = new AtomicLong(System.currentTimeMillis());
22
+        scheduleClockUpdating();
23
+    }
24
+
25
+    public static SystemClock millisClock() {
26
+        return MILLIS_CLOCK;
27
+    }
28
+
29
+    private void scheduleClockUpdating() {
30
+        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
31
+            Thread thread = new Thread(runnable, "system.clock");
32
+            thread.setDaemon(true);
33
+            return thread;
34
+        });
35
+        scheduler.scheduleAtFixedRate(() -> now.set(System.currentTimeMillis()), precision, precision, TimeUnit.MILLISECONDS);
36
+    }
37
+
38
+    public long now() {
39
+        return now.get();
40
+    }
41
+}

Loading…
Cancel
Save