19-Spring Boot 时间

Spring 时间

  1. 主要涉及到LocalDateTime和TimeStamp之间的转换
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
@Component
public class TimeUtil {
public static LocalDateTime getLocalDateTime(long timestamp){
LocalDateTime time = LocalDateTime.ofEpochSecond(timestamp/1000,0, ZoneOffset.ofHours(0));
return time;
}

public static LocalDateTime getDefaultLocalDateTime(){
LocalDateTime time = LocalDateTime.ofEpochSecond(946656000/1000,0,ZoneOffset.ofHours(0));
return time;
}

//毫秒为单位
public static long getTimeStamp(LocalDateTime localDateTime){
return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
}

public static String dateToString(LocalDateTime localDateTime){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateTime = localDateTime.format(formatter);
return dateTime;
}

public static String dateToAreaString(LocalDateTime localDateTime){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM月dd日 HH:mm");
String dateTime = localDateTime.format(formatter);
return dateTime;
}

public static String dateToVOString(LocalDateTime localDateTime){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
String dateTime = localDateTime.format(formatter);
return dateTime;
}

public static String preTime(){
return dateToVOString(LocalDateTime.now()) + " ";
}

public static LocalDateTime toDay(LocalDateTime input){
String time = dateToString(input);
String[] temp = time.split("-");
return LocalDateTime.of(Integer.valueOf(temp[0]),Integer.valueOf(temp[1]), Integer.valueOf(temp[2]),0,0);
}

/**
* 获取本周一
* @return
*/
public static LocalDateTime weekFirstDay(){
LocalDateTime localDateTime = LocalDateTime.now();
int days_of_week = localDateTime.getDayOfWeek().getValue();
localDateTime = localDateTime.minusDays(days_of_week - 1);
return toDay(localDateTime);
}
}

19-Spring Boot 时间
https://spricoder.github.io/2022/04/13/Spring-Boot/19-Spring-Boot-%E6%97%B6%E9%97%B4/
作者
SpriCoder
发布于
2022年4月13日
许可协议