java 代码块

  1. 时间代码块

本篇主要为了记录 平时使用到到代码块

时间代码块

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

public class TimeUtils {

public static DateTimeFormatter generalformat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static DateTimeFormatter generalformat2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm");
public static DateTimeFormatter generalformat3 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static DateTimeFormatter generalformat4 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");


/**
* 时间字符串转ZonedDateTime
* @param time
* @return
*/
public static ZonedDateTime timeToZonedDate(String time){
ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
LocalDateTime localDateTime = LocalDateTime.parse(time, generalformat);
return localDateTime.atZone(shanghaiZoneId);
}

/**
* 解析类似2020-10-16T21:07:57+08:00时间字符串
* @param time
* @return
*/
public static String formatTime(String time) {
String str = "";
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date date = sd.parse(time);
str = sdf.format(date);
} catch (Exception e) {
System.out.println(e.getMessage());
return str;
}
return str;
}

/**
* yyyy-MM-dd HH:mm:ss格式
* @param zonedDateTime
* @return
*/
public static String timeToString(ZonedDateTime zonedDateTime){
return zonedDateTime.format(generalformat);
}

/**
* xxxx年xx月xx日 xx:xx格式
* @param zonedDateTime
* @return
*/
public static String timeToString2(ZonedDateTime zonedDateTime){
return zonedDateTime.format(generalformat2);
}

/**
* yyyy-MM-dd 格式
* @param zonedDateTime
* @return
*/
public static String timeToString3(ZonedDateTime zonedDateTime) {
return zonedDateTime.format(generalformat3);
}


/**
* xxxx年xx月xx日 xx:xx格式
* @param zonedDateTime
* @return
*/
public static String timeToString4(ZonedDateTime zonedDateTime){
return zonedDateTime.format(generalformat4);
}

/**
* 时间字符串转Time类型
* @param timeStr
* @return
*/
public static Time stringToTime(String timeStr) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = dateFormat.parse(timeStr);
return new Time(date.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* zoneDateTime 转换为指定格式的时间
* @param zonedDateTime
* @param pattern
* @return
*/
public static ZonedDateTime ZoneToZoneForPattern(ZonedDateTime zonedDateTime,String pattern){
DateTimeFormatter generalformat = DateTimeFormatter.ofPattern(pattern);
String timeStr = zonedDateTime.format(generalformat);
return parseZonedDate(timeStr);
}
/**
* zoneTime 转换为时间戳
* @param zonedDateTime
* @param pattern
* @return
*/
public static long zoneTimeToTime(ZonedDateTime zonedDateTime,String pattern) {
try {
DateTimeFormatter generalformat = DateTimeFormatter.ofPattern(pattern);
String timeStr = zonedDateTime.format(generalformat);
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date date = dateFormat.parse(timeStr);
return date.getTime();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

/**
* 时间字符串 转time
* @param dateTimeStr
* @return
*/
public static ZonedDateTime parseZonedDate(String dateTimeStr){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
if (dateTimeStr.length() == 10) {
dateTimeStr += " 00:00:00";
}else if (dateTimeStr.length() == 19) {
dateTimeStr += "";
}
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeStr, formatter.withZone(ZoneId.systemDefault()));

return zonedDateTime;
}

/**
* 计算间隔天数
* @param startTime 开始时间
* @param endTime 结束时间
* @return 间隔天数
*/
public static long caculateDays(String startTime, String endTime) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate;
Date endDate;
long days = 0;
try {
startDate = format.parse(startTime);
endDate = format.parse(endTime);
//判断是否为同一天
if (DateUtils.isSameDay(startDate, endDate)) {
days = 1;
} else {
days = (endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24) + 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return days;
}

/**
* 根据开始时间和结束时间计算几个星期几
* @param beginDate 开始时间
* @param endDate 结束时间
* @param week 星期几
* @return
*/
public static List<String> getWeekDateList(String beginDate, String endDate, String week){
//获取俩个日期之间的日期
List<String> list = findDates(beginDate, endDate);
List<String> weekDateList = new ArrayList<>();
if (list.size() > 0) {
for (String date : list) {
//判断当前是星期几
boolean isDay = weekdayOrNot(date,week);
if (isDay) {
weekDateList.add(date);
}
}
}
return weekDateList;
}

public static boolean weekdayOrNot(String date, String week) {
switch (week) {
case "1":
week = "星期一";
break;
case "2":
week = "星期二";
break;
case "3":
week = "星期三";
break;
case "4":
week = "星期四";
break;
case "5":
week = "星期五";
break;
case "6":
week = "星期六";
break;
case "7":
week = "星期日";
break;
}
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date currentDate = sdf.parse(date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
String currSun = simpleDateFormat.format(currentDate);
//判断当前是星期几
if (currSun.equals(week)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

public static List<String> findDates(String beginDate, String endDate) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<String> dateList = new ArrayList<>();
try {
Calendar calBegin = Calendar.getInstance();
calBegin.setTime(format.parse(beginDate));
Calendar calEnd = Calendar.getInstance();
calEnd.setTime(format.parse(endDate));
while (format.parse(endDate).after(calBegin.getTime())) {
calBegin.add(Calendar.DAY_OF_MONTH, 1);
dateList.add(format.format(calBegin.getTime()));
}
dateList.add(beginDate);
} catch (Exception e) {
e.printStackTrace();
}
return dateList;
}

/**
* 时间比较
* @param date1
* @param date2
* @return
*/
public static int compareDate(Date date1, Date date2) {
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.setTime(date1);
calendar2.setTime(date2);
return calendar1.compareTo(calendar2);
}

/**
* 获取某天00:00:00
* @param time
* @return
*/
public static String getStartOfDay(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
}

/**
* 获取某天23:59:59
* @param time
* @return
*/
public static String getEndOfDay(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
}

//
// /**
// * 获取延迟某天00:00:00
// * @param time
// * @return
// */
//
// public static String getDelayTheDay(Date time ,int delay){
// Calendar calendar = Calendar.getInstance();
// calendar.setTime(time);
// calendar.add(Calendar.DAY_OF_MONTH, 1);
// calendar.set(Calendar.HOUR_OF_DAY, 0);
// calendar.set(Calendar.MINUTE, 0);
// calendar.set(Calendar.SECOND, 0);
// calendar.set(Calendar.MILLISECOND, 0);
// return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
// }
/**
* 获取延迟某天00:00:00
* @param time
* @return
*/

public static ZonedDateTime getDelayTheDay(ZonedDateTime now ,int delay){
final ZonedDateTime todayZero = now.truncatedTo(ChronoUnit.DAYS); //今天的0点
final ZonedDateTime tomorrowZero = todayZero.plusDays(delay); //delay的0点
return tomorrowZero;
}

}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 337950548@qq.com

💰

×

Help us with donation