LocalDateUtils.java
7.25 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
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
package com.ruoyi.common.utils;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
/**
* @Author: ly
* 时间工具类
*/
public class LocalDateUtils {
private boolean isNull=false;
public static enum TimeUnit{
Year,
Month,
Week,
Day,
Hour,
Minute,
Second
}
public LocalDateTime localDate= LocalDateTime.now();
public static LocalDateUtils of(){return new LocalDateUtils();}
public static void main(String[] args) {
LocalDateUtils localDateUtils = LocalDateUtils.of(new Date());
System.out.println(localDateUtils.time_addOrSub(-3, TimeUnit.Year).getFormatStr("yyyy"));
}
public static LocalDateUtils of(Object object){
LocalDateUtils localDateUtils = new LocalDateUtils();
if (object instanceof Date){
Date date=(Date)object;
localDateUtils.localDate=date.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
return localDateUtils;
}else if(object instanceof LocalDateTime){
localDateUtils.localDate=(LocalDateTime)object;
}else if (object instanceof String){
String dateStr = object.toString();
if (!dateStr.contains("-")&dateStr.matches("\\d+")){
localDateUtils.localDate=Instant.ofEpochMilli(Long.parseLong(dateStr)).atZone(ZoneId.systemDefault()).toLocalDateTime();
}else{
dateStr=(!dateStr.contains(" ")&&dateStr.contains("-"))?dateStr+" 00:00:00":dateStr;
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
localDateUtils.localDate=LocalDateTime.parse(dateStr, df);
}
} else if (object instanceof Long) {
localDateUtils.localDate=Instant.ofEpochMilli((Long)object).atZone(ZoneId.systemDefault()).toLocalDateTime();
}else{
localDateUtils.isNull=true;
}
return localDateUtils;
}
public String getYMD(){
if (isNull)return null;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format = dateTimeFormatter.format(localDate);
return format;
}
public String getYMD_HMS(){
if (isNull)return null;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(localDate);
return format;
}
/**
* 获取Date对象
* @return
*/
public Date getDate(){
if (isNull)return null;
Date from = Date.from(localDate.atZone(ZoneOffset.systemDefault()).toInstant());
return from;
}
public String getFormatStr(String formatDateStr){
if (isNull)return null;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatDateStr);
String format = dateTimeFormatter.format(localDate);
return format;
}
/**
* 日期加减
* @param num
* @param timeUnit
* @return
*/
public LocalDateUtils time_addOrSub(long num,TimeUnit timeUnit){
if (isNull)return null;
if (num>0){
if (TimeUnit.Year==timeUnit){localDate=localDate.plusYears(num);}else
if (TimeUnit.Month==timeUnit){localDate=localDate.plusMonths(num);}else
if (TimeUnit.Week==timeUnit){localDate=localDate.plusWeeks(num);}else
if (TimeUnit.Day==timeUnit){localDate=localDate.plusDays(num);}else
if (TimeUnit.Hour==timeUnit){localDate=localDate.plusHours(num);}else
if (TimeUnit.Minute==timeUnit){localDate=localDate.plusMinutes(num);}else
if (TimeUnit.Second==timeUnit){localDate=localDate.plusSeconds(num);}
}else{
num=Math.abs(num);
if (TimeUnit.Year==timeUnit){localDate=localDate.minusYears(num);}else
if (TimeUnit.Month==timeUnit){localDate=localDate.minusMonths(num);}else
if (TimeUnit.Week==timeUnit){localDate=localDate.minusWeeks(num);}else
if (TimeUnit.Day==timeUnit){localDate=localDate.minusDays(num);}else
if (TimeUnit.Hour==timeUnit){localDate=localDate.minusHours(num);}else
if (TimeUnit.Minute==timeUnit){localDate=localDate.minusMinutes(num);}else
if (TimeUnit.Second==timeUnit){localDate=localDate.minusSeconds(num);}
}
return this;
}
/**
* 获取毫秒数 时间戳
* @return
*/
public Long getMilli(){
if (isNull)return null;
return localDate.toInstant(ZoneOffset.of("+8")).toEpochMilli();
}
//指定具体时间
public LocalDateUtils withTime(int num ,TimeUnit timeUnit){
if (isNull)return null;
if (TimeUnit.Year==timeUnit)localDate=localDate.withYear(num);
if (TimeUnit.Month==timeUnit)localDate=localDate.withMonth(num);
if (TimeUnit.Week==timeUnit)throw new RuntimeException("不支持指定星期");
if (TimeUnit.Day==timeUnit)localDate=localDate.withDayOfMonth(num);
if (TimeUnit.Hour==timeUnit)localDate=localDate.withHour(num);
if (TimeUnit.Minute==timeUnit)localDate=localDate.withMinute(num);
if (TimeUnit.Second==timeUnit)localDate=localDate.withSecond(num);
return this;
}
public static Boolean equal(Date date1,Date date2){
LocalDateTime stL=date1.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
LocalDateTime edL=date2.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
return stL.equals(edL);
}
/**
* 获取时间单独位
* @param timeUnit
* @return
*/
public Integer dateInfo(TimeUnit timeUnit){
if (isNull)return null;
//年
int year = localDate.getYear();
//直接获取
int monthValue = localDate.getMonthValue();
//日
int dayOfMonth1 = localDate.getDayOfMonth();
//时
int hour = localDate.getHour();
//分
int minute = localDate.getMinute();
//秒
int second = localDate.getSecond();
if (TimeUnit.Year==timeUnit)return year;
if (TimeUnit.Month==timeUnit)return monthValue;
if (TimeUnit.Day==timeUnit)return dayOfMonth1;
if (TimeUnit.Hour==timeUnit)return hour;
if (TimeUnit.Minute==timeUnit)return minute;
if (TimeUnit.Second==timeUnit)return second;
return 0;
}
/**
* 两日期间隔多少天
* @param start
* @param end
* @return
*/
public static Long betweenDays(Date start,Date end){
LocalDateTime stL=start.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
LocalDateTime edL=end.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
Duration between = Duration.between(stL, edL);
return between.toDays();
}
public static Long betweenDays(LocalDateTime start,LocalDateTime end){
long l = ChronoUnit.DAYS.between(start, end);
return l;
}
/**
* LocalDate 转 Date
* @param localDate
* @return
*/
public static Date convertLocalDateToDate(LocalDateTime localDate) {
ZoneId zoneId = ZoneId.systemDefault();
return Date.from(localDate.atZone(zoneId).toInstant());
}
}