TimestampUtil.java
2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.topdraw.util;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class TimestampUtil {
public static Timestamp now(){
return new Timestamp(System.currentTimeMillis());
}
public static Timestamp now(LocalDateTime localDateTime) {
long epochSecond = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
return new Timestamp(epochSecond);
}
public static long localDateTime2long(LocalDateTime localDateTime){
long epochSecond = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
return epochSecond;
}
public static Timestamp localDateTime2Timestamp(LocalDateTime localDateTime){
long epochSecond = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
return long2Timestamp(epochSecond);
}
public static Timestamp long2Timestamp(long timestamp){
Timestamp timestamp1 = Timestamp.from(Instant.ofEpochMilli(timestamp));
return timestamp1;
}
public static long timestamp2long(Timestamp timestamp){
long l = timestamp.toInstant().toEpochMilli();
return l;
}
public static LocalDateTime long2LocalDateTime(Long expireTime) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(expireTime),ZoneOffset.of("+8"));
}
public static void main(String[] args) {
long a = 1636616464000L;
long b = 1637046948588L;
long c = 1637047122176L;
// long l = 16342727230L;
// Timestamp now = now(LocalDateTime.now());
long l = localDateTime2long(LocalDateTime.now());
System.out.println(l);
// System.out.println(now.toString());
Timestamp timestamp1 = long2Timestamp(a);
// long l = localDateTime2Timestamp(of);
// long l = 16342727230L;
// Timestamp timestamp = long2Timestamp(l);
System.out.println(timestamp1.toString());
long l1 = localDateTime2long(LocalDateTime.now());
System.out.println(l1);
}
}