DateUtil.java
4.28 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
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
package com.yoho.rfid.util;
import org.slf4j.helpers.MessageFormatter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;
/**
* Created by sailing on 2017/10/16.
*/
public class DateUtil {
// 基础(通用)的FORMAT类型,
public static final String NORMAL_DAY_FORMAT = "yyyy-MM-dd";
public static final String DAY_FORMAT = "yyyyMMdd";
// 完整格式的时间
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 操作一个时间里的分钟/小时/天数
* @param date
* @param operator
* @param timeLong
* @param timeUnit
* @return
*/
public static Date calDateTime(Date date,Operator operator,long timeLong, TimeUnit timeUnit){
if (Objects.isNull(date)){
return date;
}
if(Objects.isNull(operator)){
return date;
}
if (timeLong == 0L){//as no any operator
return date;
}
if(Objects.isNull(timeUnit)){
return date;
}
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant,zoneId);
switch (operator){
case plus:
switch(timeUnit){
case minute:
localDateTime = localDateTime.plusMinutes(timeLong);
break;
case hour:
localDateTime = localDateTime.plusHours(timeLong);
break;
case day:
localDateTime = localDateTime.plusDays(timeLong);
break;
}
break;
case minus:
switch(timeUnit){
case minute:
localDateTime = localDateTime.minusMinutes(timeLong);
break;
case hour:
localDateTime = localDateTime.minusHours(timeLong);
break;
case day:
localDateTime = localDateTime.minusDays(timeLong);
break;
}
break;
}
return Date.from(localDateTime.atZone(zoneId).toInstant());
}
/**
* 根据时间获取到秒值
* @param date
* @return
*/
public static int getDateTimeSecond(Date date){
long longTime = date.getTime();
return (int) (longTime / 1000);
}
public static int getPart(Date date, TimeUnit timeUnit){
int result = 0;
if (Objects.isNull(date)){
return result;
}
if(Objects.isNull(timeUnit)){
return result;
}
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant,zoneId);
switch(timeUnit){
case minute:
result = localDateTime.getMinute();
break;
case hour:
result = localDateTime.getHour();
break;
case day:
result = localDateTime.getDayOfMonth();
break;
}
return result;
}
public enum Operator{
plus,minus;
}
public enum TimeUnit{
minute,hour,day
}
public static void main(String[] args) {
try{
int currentHour = DateUtil.getPart(new Date(), TimeUnit.hour);
String specailDatetime = "2018-10-10 00:00:00";
DateFormat format = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT);
Date parseDate = format.parse(specailDatetime);
int parseDateHour = DateUtil.getPart(parseDate, TimeUnit.hour);
System.out.println(MessageFormatter
.arrayFormat("currentHour {}, parseDateHour {}", new Object[]{currentHour, parseDateHour})
.getMessage());
}catch (Exception e){
e.printStackTrace();
}
}
}