有时候服务器对于时间日期,只会返回给你一个字符串,而你根据业务需求要把该字符串转换成各种需要的格式。下面是本人总结的一个工具类,话说Java 8的时间日期真的很好用,我们服务器返回的是标准时间,即ZonedDateTime格式,其他格式的类似。
复制代码
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Created by Apple on 17/4/21.
* 时间日期工具类,注意这里的时间字符串都是标准时间,即ZonedDateTime样式
*/
public class DateUtils {
/**
* 根据给定的时间返回对应格式的字符串
* 1、小于一分钟---"刚刚"
* 2、大于一分钟而在本日内的,返回格式为"HH:mm"
* 3、今年之内而不再本日内,格式为"MM-dd"
* 4、否则,格式为"yyyy-MM-dd"
* @param strDate
* @return
*/
public static String getFormatDate(String strDate) {
DateTimeFormatter formatter;
ZonedDateTime zonedDateTime = ZonedDateTime.parse(strDate);
if (zonedDateTime.toLocalDate().equals(LocalDate.now())) {
if (getOverMinutes(strDate) <= 1) {
return "刚刚";
} else {
formatter = DateTimeFormatter.ofPattern("HH:mm", Locale.CHINA);
return zonedDateTime.format(formatter);
}
} else {
if (zonedDateTime.toLocalDate().getYear() == LocalDate.now().getYear()) {
formatter = DateTimeFormatter.ofPattern("MM-dd", Locale.CHINA);
} else {
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINA);
}
return zonedDateTime.format(formatter);
}
}
/**
* 将给定的秒数转换为一段持续时间,如70s,可为01:10
* @param s
* @return
*/
public static String sec2Duration(int s) {
int sec;
if (s < 0) {
return "00:00";
}
int min = s / 60;
if (min < 60) {
sec = s % 60;
return unitFormat(min) + ":" + unitFormat(sec);
} else {
int hour = min / 60;
min = min % 60;
sec = s - hour * 3600 - min * 60;
return unitFormat(hour) + ":" + unitFormat(min) + ":" + unitFormat(sec);
}
}
public static String unitFormat(int i) {
return i >= 0 && i < 10 ? "0" + i : i + "";
}
/**
* 给定开始时间,返回还剩多少天多少时多少分的字符串数组
* @param startTime
* @return
*/
public static String[] getLeftStr(String startTime) {
Instant start = ZonedDateTime.parse(startTime).toInstant();
long minutes = Duration.between(Instant.now(), start).toMinutes();
if (minutes < 60 && minutes > 0) {
return new String[]{"0", "0", minutes + ""};
} else if (minutes <= 0) {
return null;
}
long hours = minutes / 60;
if (hours < 60) {
minutes = minutes % 60;
return new String[]{"0", hours + "", minutes + ""};
} else {
long days = hours / 24;
hours = hours % 24;
minutes = minutes - days * 24 * 60 - hours * 60;
return new String[]{days + "", hours + "", minutes + ""};
}
}
/**
* 根据创建时间和总共时间得到当前剩余时间
* @param createStr
* @param totalMills
* @return
*/
public static long getLeftMills(String createStr, long totalMills) {
long leftMills = totalMills - getOverMills(createStr);
return leftMills >= 0 ? leftMills : 0;
}
public static long getLeftMinutes(String createStr, long totalMinutes) {
long leftMinutes = totalMinutes - getOverMinutes(createStr);
return leftMinutes >= 0 ? leftMinutes : 0;
}
public static long getLeftHours(String createStr, long totalHours) {
long leftHours = totalHours - getOverHours(createStr);
return leftHours >= 0 ? leftHours : 0;
}
/**
* 根据跟定的时间得到到目前为止过了多少秒
* @param time
* @return
*/
public static long getOverMills(String time){
return getOverTime(time).toMillis();
}
public static long getOverMinutes(String time){
return getOverTime(time).toMinutes();
}
public static long getOverHours(String time){
return getOverTime(time).toHours();
}
private static Duration getOverTime(String time) {
Instant timeInstant = ZonedDateTime.parse(time).toInstant();
Instant now = Instant.now();
return Duration.between(timeInstant, now);
}
/**
* 将UNIX时间戳(秒级)格式化成ZoneDateTime的格式
*/
public static String format2ZoneDateTimeFromSecond(long timestamp) {
return ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.of("Asia/Shanghai")).toString();
}
}
最后
以上就是刻苦唇彩最近收集整理的关于从ZonedDateTime开启日期时间的管理的全部内容,更多相关从ZonedDateTime开启日期时间内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复