1 public class DateUtil {
2
3 /**
4 * 获取 时间和当前时间的时间差,用字符串表示出来
5 *
6 * @param date
7 * @return
8 */
9 public static String absTime(Date date) {
10 Long newTime = System.currentTimeMillis();
11 if (date == null) {
12 return "";
13 }
14 Long intervalTime = newTime - date.getTime();
15
16 if (intervalTime < 1000 * 60 * 1) {
17 return "刚刚";
18 } else if (intervalTime < (1000L * 60 * 60)) {
19 return (intervalTime / (1000L * 60)) + "分钟前";
20 } else if (intervalTime < (1000L * 60 * 60 * 24)) {
21 return (intervalTime / (1000L * 60 * 60)) + "小时前";
22 } else if (intervalTime < (1000L * 60 * 60 * 24 * 30)) {
23 return (intervalTime / (1000L * 60 * 60 * 24)) + "天前";
24 } else if (intervalTime < (1000L * 60 * 60 * 24 * 30 * 356)) {
25 return (intervalTime / (1000L * 60 * 60 * 24 * 30)) + "月前";
26 } else {
27 return (intervalTime / (1000L * 60 * 60 * 24 * 30 * 356)) + "年前";
28 }
29 }
30
31 /**
32 * 时间格式转换
33 *
34 * @param date
35 * @param format
36 * @return
37 */
38 public static String DateToString(Date date, String format) {
39 SimpleDateFormat sdf = new SimpleDateFormat(format);
40 return sdf.format(date);
41 }
42
43 public static String getNewDate() {
44 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
45 return sdf.format(new Date());
46 }
47
48 /**
49 * 时间格式转换
50 *
51 * @param time
52 * @param format
53 * @return
54 * @throws ParseException 字符换转换成int型数据时可能出现的异常
55 * @author xujiayu
56 * @time 2016/04/01
57 */
58 public static String timeFormatConversion(String time, String format) {
59 if (time == null || "".equals(time)) {
60 return "";
61 }
62 if (TextUtil.startCheck("^[0-9]+$", time)) {
63 SimpleDateFormat sdf = new SimpleDateFormat(format);
64 Date date = new Date(Long.parseLong(time));
65 String formatTime = sdf.format(date);
66 return formatTime;
67 } else {
68 SimpleDateFormat sdf = new SimpleDateFormat(format);
69 Date date = null;
70 try {
71 date = sdf.parse(time);
72 } catch (ParseException e) {
73 e.printStackTrace();
74 return "";
75 }
76 String formatTime = date.getTime() + "";
77 return formatTime;
78 }
79 }
80
81
82 /**
83 * 获取一定范围内的日期
84 *
85 * @param date
86 * @param beforeDays
87 * @return
88 */
89 public static List<String> getDatePeriod(Date date, int beforeDays) {
90 List<String> datePeriodList = new ArrayList<String>();
91 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
92 Calendar cal = Calendar.getInstance();
93 cal.setTime(date);
94 int inputDayOfYear = cal.get(Calendar.DAY_OF_YEAR);
95 for (int i = 0; i < beforeDays; i++) {
96 cal.set(Calendar.DAY_OF_YEAR, inputDayOfYear + i);
97 datePeriodList.add(dateFormat.format(cal.getTime()));
98 }
99 return datePeriodList;
100 }
101
102 /**
103 * 获取时间差 (毫秒)
104 *
105 * @param d1
106 * @param d2
107 * @return
108 */
109 public static long timeDifference(Date d1, Date d2) {
110
111 return d1.getTime() - d2.getTime();
112
113 }
114
115 /**
116 * 通过时间秒毫秒数判断两个时间的间隔
117 *
118 * @param date1
119 * @param date2
120 * @return
121 */
122 public static int differentDaysByMillisecond(Date date1, Date date2) {
123 int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
124 return days;
125 }
126
127 /**
128 * 获取格式化时间
129 *
130 * @param dateStr
131 * @param pattern
132 * @return
133 * @throws ParseException
134 */
135 public static Date getDate(String dateStr, String pattern) throws ParseException {
136 SimpleDateFormat format = new SimpleDateFormat(pattern);
137 return format.parse(dateStr);
138 }
139
140 /**
141 * 获取当前时间
142 *
143 * @return
144 * @throws ParseException
145 * @author huangzhengyan
146 */
147 public static String getNowDate(Date creatDate) {
148 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
149 return format.format(creatDate);
150
151 }
152
153 public static String getNowDate1(Date creatDate) {
154 SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
155 return format.format(creatDate);
156
157 }
158
159 public static String getNowDate2(Date creatDate) {
160 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
161 return format.format(creatDate);
162 }
163
164 public static String getNowDate3(Date creatDate) {
165 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
166 return format.format(creatDate);
167 }
168
169 public static String getNowDate4(Date creatDate) {
170 SimpleDateFormat format = new SimpleDateFormat("HHmmss");
171 return format.format(creatDate);
172 }
173 public static String getNowDate5(Date creatDate) {
174 SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
175 return format.format(creatDate);
176 }
177
178 public static Date getAppointDate(final Date date, int i) {
179 if (null == date) {
180 return null;
181 }
182 Calendar calendar = Calendar.getInstance();
183 calendar.setTime(date);
184 calendar.add(Calendar.DATE, i);
185 return calendar.getTime();
186 }
187
188
189 public static String getTodyCalculateDay(Integer mon, Integer day, String formatType) {
190 SimpleDateFormat format = new SimpleDateFormat(formatType);
191 Calendar cale = null;
192 cale = Calendar.getInstance();
193 cale.add(Calendar.MONTH, mon);
194 cale.set(Calendar.DAY_OF_MONTH, day);
195 return format.format(cale.getTime());
196 }
197
198 public static String getNowWeekBegin(Integer i, String time, String formatType) {
199 SimpleDateFormat simdf = new SimpleDateFormat(formatType);
200
201 Calendar cal = Calendar.getInstance();
202 try {
203 cal.setTime(simdf.parse(time));
204 if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
205 cal.add(Calendar.DAY_OF_MONTH, -1);
206 }
207 } catch (ParseException e) {
208 e.printStackTrace();
209 return null;
210 }
211
212 switch (i) {
213 case 1:
214 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
215 break;
216 case 2:
217 cal.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
218 break;
219 case 3:
220 cal.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
221 break;
222 case 4:
223 cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
224 break;
225 case 5:
226 cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
227 break;
228 case 6:
229 cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
230 break;
231 case 7:
232 cal.add(Calendar.WEEK_OF_YEAR, 1);
233 cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
234 break;
235 default:
236 return null;
237 }
238 return simdf.format(cal.getTime());
239 }
240
241 public static List<String> getNowWeekBeginAll(String time, String formatType) {
242 List<String> list = new ArrayList<String>();
243 String weekTime = null;
244 for (int i = 1; i < 8; i++) {
245 weekTime = getNowWeekBegin(i, time, formatType);
246 if (weekTime == null) {
247 return null;
248 }
249 list.add(weekTime);
250 }
251 return list;
252 }
253
254
255 public static boolean isWorkDay(Date date) {
256 Calendar cal = Calendar.getInstance();
257 cal.setTime(date);
258 int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
259 if (w < 0 || w == 0 || w == 6) {
260 return false;
261 } else {
262 return true;
263 }
264
265 }
266
267 public static String getTodyLaterDay(Integer mon, Integer day, String formatType) {
268 SimpleDateFormat format = new SimpleDateFormat(formatType);
269 Calendar cale = null;
270 cale = Calendar.getInstance();
271 cale.add(Calendar.MONTH, mon);
272 cale.add(Calendar.DAY_OF_MONTH, day);
273 return format.format(cale.getTime());
274 }
275
276 public static String getTodyLaterDayCompleteness(Integer mon, Integer day, String formatType) {
277 SimpleDateFormat format = new SimpleDateFormat(formatType);
278 Calendar cale = null;
279 cale = Calendar.getInstance();
280 cale.add(Calendar.MONTH, mon);
281 cale.add(Calendar.DAY_OF_MONTH, day);
282 cale.set(Calendar.HOUR_OF_DAY, 23);
283 cale.set(Calendar.MINUTE, 59);
284 cale.set(Calendar.SECOND, 59);
285 return format.format(cale.getTime());
286 }
287
288 public static void main(String[] args) {
289
290 Calendar calendar = Calendar.getInstance();
291 calendar.add(Calendar.DAY_OF_MONTH, -1);
292 SimpleDateFormat format = new SimpleDateFormat("yyy/MM/dd");
293 System.out.println(format.format(calendar.getTime()));
294 calendar.add(Calendar.DAY_OF_MONTH, -1);
295 System.out.println(format.format(calendar.getTime()));
296 System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
297 }
298
299 /**
300 * 返回指定日期
301 *
302 * @param date 标准日期
303 * @param i 偏移量 正负数 1 = 加一天日期, -1 = 一天日期
304 * @return 指定日期
305 */
306 public static Date getBeginAppointDate(final Date date, int i) {
307 if (null == date) {
308 return null;
309 }
310 Calendar calendar = Calendar.getInstance();
311 calendar.setTime(date);
312 calendar.add(Calendar.DATE, i);
313 calendar.set(Calendar.HOUR_OF_DAY, 0);
314 calendar.set(Calendar.MINUTE, 0);
315 calendar.set(Calendar.SECOND, 0);
316 return calendar.getTime();
317 }
318
319 public static Date getEndAppointDate(final Date date, int i) {
320 if (null == date) {
321 return null;
322 }
323 Calendar calendar = Calendar.getInstance();
324 calendar.setTime(date);
325 calendar.add(Calendar.DATE, i);
326 calendar.set(Calendar.HOUR_OF_DAY, 23);
327 calendar.set(Calendar.MINUTE, 59);
328 calendar.set(Calendar.SECOND, 59);
329 return calendar.getTime();
330 }
331
332 public static Date getAppointDateMinute(final Date date, int i) {
333 if (null == date) {
334 return null;
335 }
336 Calendar calendar = Calendar.getInstance();
337 calendar.setTime(date);
338 calendar.add(Calendar.MINUTE, i);
339 return calendar.getTime();
340 }
341 }