-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateUtils.java
239 lines (210 loc) · 5.82 KB
/
DateUtils.java
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
package com.stkj.pperty.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DateUtils {
// public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd
// HH:mm:ss");
//
// public static final DateFormat df_apm = new SimpleDateFormat("yyyy-MM-dd
// ahh:mm:ss");
public static final String df_Str = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY_MM_DD_Str = "yyyy-MM-dd";
public static final String df_apm_Str = "yyyy-MM-dd ahh:mm:ss";
public static Date toDate(String dateStr) {
try {
return new SimpleDateFormat(df_Str).parse(dateStr);
} catch (ParseException e) {
return null;
}
}
public static Date toYMDDate(String dateStr) {
try {
return new SimpleDateFormat(YYYY_MM_DD_Str).parse(dateStr);
} catch (ParseException e) {
return null;
}
}
public static String toStr(Date date) {
try {
return new SimpleDateFormat(df_Str).format(date);
} catch (Exception e) {
e.printStackTrace();
return date == null ? "" : date.toString();
}
}
public static String toStr(Object date) {
try {
return new SimpleDateFormat(df_Str).format(date);
} catch (Exception e) {
e.printStackTrace();
return date == null ? "" : date.toString();
}
}
/**
* 增加时间 (小时)
*
* @param startDate
* @param hour
* @return
*/
public static Date addHour(Date startDate, int hour) {
String format = new SimpleDateFormat(df_Str).format(startDate);
Date date = null;
try {
date = new SimpleDateFormat(df_Str).parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.add(Calendar.HOUR_OF_DAY, hour);
return ca.getTime();
}
/**
* 转化成上午、下午
*
* @param date
* @return
*/
public static String toAPM(Date date) {
return new SimpleDateFormat(df_apm_Str).format(date);
}
public static String toAPM(String date) {
try {
return new SimpleDateFormat(df_apm_Str).format(new SimpleDateFormat(df_Str).parse(date));
} catch (ParseException e) {
return date;
}
}
public static String format(Date date, String format) {
if (date == null || "".equals(format) || format == null) {
return null;
}
return new SimpleDateFormat(format).format(date);
}
public static Date parse(String date, String format) {
if (date == null || "".equals(format) || format == null) {
return null;
}
try {
return new SimpleDateFormat(format).parse(date);
} catch (ParseException e) {
// e.printStackTrace();
}
return null;
}
public static long dateToTimestamp(String dateStr) {
Date d = toDate(dateStr);
if (null != d) {
return d.getTime();
}
return 0;
}
public static long getTimestamp(){
return new Date().getTime();
}
public static String unixTimestampToDateStr(long timestamp) {
return timestampToDateStr(timestamp, true);
}
public static String timestampToDateStr(long timestamp, boolean isUnix) {
if (timestamp == 0)
return null;
if (isUnix)
return new SimpleDateFormat(df_Str).format(timestamp * 1000);
return new SimpleDateFormat(df_Str).format(timestamp);
}
public static long getUnixNowTimestamp(){
return (getNowTimestamp()/1000);
}
public static long getNowTimestamp(){
return System.currentTimeMillis();
}
public static String getNowToStrYYYY_MM_DD() {
return new SimpleDateFormat(YYYY_MM_DD_Str).format(new Date());
}
// day = -1 表示上一天
public static long lastDays(int day) {
Calendar c = setCalendar();
c.add(Calendar.DATE, day);
Date d = c.getTime();
return d.getTime();
}
// month = -1 表示过去一个月
public static long lastMonth(int month) {
Calendar c = setCalendar();
c.add(Calendar.MONTH, month);
Date m = c.getTime();
return m.getTime();
}
public static long lastYear(int year) {
// 过去一年
Calendar c = setCalendar();
c.add(Calendar.YEAR, year);
Date y = c.getTime();
return y.getTime();
}
private static Calendar setCalendar() {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
return c;
}
//一天开始 0点0时0分
public static long getStartTime() {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime().getTime();
}
//一天结束 23点59时59分
public static long getnowEndTime() {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime().getTime();
}
public static String getstringtime(Date aDate, String separator) {
try {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(separator);
returnValue = df.format(aDate);
}
return returnValue;
} catch (RuntimeException e) {
return null;
}
}
/**
* 当前时间是否是今天的?到?之间,例:"19:20:00","20:43:00"(不能夸天)
* @param stime
* @param etime
* @return
*/
public static boolean isRunTime(String stime,String etime) {
String nowString = getstringtime(new Date(System.currentTimeMillis()), "HH:mm:ss");
String startTime = stime;
String overTime = etime;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);
Date dtStart = null;
Date dtOver = null;
Date now = null;
try {
dtStart = sdf.parse(startTime);
dtOver = sdf.parse(overTime);
now = sdf.parse(nowString);
} catch (Exception e) {
e.printStackTrace();
}
boolean isNullIssueTime = (now.getTime() >= dtStart.getTime())
&& (now.getTime() <= dtOver.getTime());
return isNullIssueTime;
}
}