1 package com.lashoupay.common.util;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.io.*;
8
9 /**
10 * 日期操作工具类
11 *
12 */
13 public class DateUtil {
14
15 public DateUtil() {
16 }
17
18 public static void printTime(String s) {
19 }
20
21 /**
22 * 获取年份
23 * @return
24 */
25 public static String getYear() {
26 String strYear = "";
27 Date currentDate = new Date();
28 SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
29 strYear = formatter.format(currentDate);
30 return strYear;
31 }
32
33 /**
34 * 获取月份
35 * @return
36 */
37 public static String getYearMonth() {
38 String strYearMonth = "";
39 Date currentDate = new Date();
40 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
41 strYearMonth = formatter.format(currentDate);
42 return strYearMonth;
43 }
44
45 public static String getLastMonthByFormat(String dateFormat)
46 {
47 String str = "";
48 SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
49
50 Calendar lastDate = Calendar.getInstance();
51 lastDate.set(Calendar.DATE,1);//设为当前月的1号
52 lastDate.add(Calendar.MONTH,-1);//减一个月,变为下月的1号
53 //lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天
54
55 str=sdf.format(lastDate.getTime());
56 return str;
57 }
58
59 /**
60 * 获取指定日期格式的日期
61 * @param format
62 * @return
63 */
64 public static String getDateByFormat(String format)
65 {
66 String date = "";
67 Date currentDate = new Date();
68 SimpleDateFormat formatter = new SimpleDateFormat(format);
69 date = formatter.format(currentDate);
70 return date;
71 }
72
73 /**
74 * 获取年-月-日
75 * @return
76 */
77 public static String getDate() {
78 String strCurrentDate = "";
79 Date currentDate = new Date();
80 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
81 strCurrentDate = formatter.format(currentDate);
82 return strCurrentDate;
83 }
84
85
86 public static String getTommorrow() {
87 String strCurrentDate = "";
88 // Date currentDate = new Date();
89 Calendar c = Calendar.getInstance();
90 c.add(Calendar.DAY_OF_YEAR, -3);
91 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
92 strCurrentDate = formatter.format(c.getTime());
93 return strCurrentDate;
94 }
95
96 /**
97 * 获取指定格式的日期
98 * @param dateFormat 日期格式
99 * @param intervalDay 与当天相隔的天数(0:当天 1:明天 -1:昨天)
100 * @return
101 */
102 public static String getSpecialDate(String dateFormat,int intervalDay)
103 {
104 String strCurrentDate = "";
105 // Date currentDate = new Date();
106 Calendar c = Calendar.getInstance();
107 c.add(Calendar.DAY_OF_YEAR, intervalDay);
108 SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
109 strCurrentDate = formatter.format(c.getTime());
110
111 return strCurrentDate;
112 }
113
114 /**
115 * 功能:获取间隔一定分钟数的日期
116 * 修改时间:Jul 27, 2011 10:46:53 AM
117 * @param dateFormat
118 * @param intervalMinute
119 * @return
120 */
121 public static String getSpecialDateByIntervalMinute(String dateFormat,int intervalMinute)
122 {
123 Date date = new Date();
124 long time = date.getTime()+intervalMinute* 60 * 1000;
125 Date preDate = new Date(time);
126
127 SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
128
129 return formatter.format(preDate);
130 }
131
132 public static void main(String[] args)
133 {
134 //Date date=new Date();
135 //System.out.println(DateUtil.gettDateStrFromSpecialDate(date, "yyyyMMdd"));
136 String str="2011-11-24 15:21:44";
137 System.out.println(DateUtil.stringToNumber(str));
138 }
139
140 public static String getTime() {
141 String strCurrentTime = "";
142 Date currentTime = new Date();
143 SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
144 strCurrentTime = formatter.format(currentTime);
145 return strCurrentTime;
146 }
147
148 public static String getCurrentHour()
149 {
150 String strCurrentTime = "";
151 Date currentTime = new Date();
152 SimpleDateFormat formatter = new SimpleDateFormat("HH");
153 strCurrentTime = formatter.format(currentTime);
154 return strCurrentTime;
155 }
156
157 public static String getHourFromSpecialDate(Date specialDate)
158 {
159 String strCurrentTime = "";
160 SimpleDateFormat formatter = new SimpleDateFormat("HH");
161 strCurrentTime = formatter.format(specialDate);
162 return strCurrentTime;
163 }
164
165
166
167 public static String getCurrentMinute()
168 {
169 String strCurrentTime = "";
170 Date currentTime = new Date();
171 SimpleDateFormat formatter = new SimpleDateFormat("mm");
172 strCurrentTime = formatter.format(currentTime);
173 return strCurrentTime;
174 }
175
176 public static String gettMinuteFromSpecialDate(Date specialDate)
177 {
178 String strCurrentTime = "";
179 SimpleDateFormat formatter = new SimpleDateFormat("mm");
180 strCurrentTime = formatter.format(specialDate);
181 return strCurrentTime;
182 }
183
184 public static String gettDateStrFromSpecialDate(Date specialDate,String dateFormat)
185 {
186 String strCurrentTime = "";
187 SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
188 strCurrentTime = formatter.format(specialDate);
189 return strCurrentTime;
190 }
191
192 public static String getNumberTime() {
193 String strCurrentTime = "";
194 Date currentTime = new Date();
195 SimpleDateFormat formatter = new SimpleDateFormat("HHmmss");
196 strCurrentTime = formatter.format(currentTime);
197 return strCurrentTime;
198 }
199
200 /**
201 * 数字时间格式化
202 * 234359->23:43:59
203 * @param time
204 * @return
205 */
206 public static String numberToStringTime(String time) {
207 String strTime = "";
208 if (time.length() == 6) {
209 String hh = time.substring(0, 2);
210 String mm = time.substring(2, 4);
211 String ss = time.substring(4, 6);
212 strTime = (new StringBuilder(String.valueOf(hh))).append(":")
213 .append(mm).append(":").append(ss).toString();
214 }
215 return strTime;
216 }
217
218 /**
219 * 数字日期格式化
220 * 20090806->2009-08-06
221 * @param date
222 * @return
223 */
224 public static String numberToStringDate(String date) {
225 String strTime = "";
226 // System.out.println("date.length:"+date.length());
227 if (date.length() == 8) {
228 String year = date.substring(0, 4);
229 String month = date.substring(4, 6);
230 String day = date.substring(6, 8);
231 strTime = (new StringBuilder(String.valueOf(year))).append("-")
232 .append(month).append("-").append(day).toString();
233 }
234 return strTime;
235 }
236
237 /**
238 * 获取年月日时分秒
239 * @return
240 */
241 public static String getDateTime() {
242 String strCurrentDateTime = "";
243 Date currentDateTime = new Date();
244 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
245 strCurrentDateTime = formatter.format(currentDateTime);
246 return strCurrentDateTime;
247 }
248
249 public static String getYMD() {
250 String strYMD = "";
251 Date currentDateTime = new Date();
252 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
253 strYMD = formatter.format(currentDateTime);
254 return strYMD;
255 }
256
257 public static String getYMDHMS() {
258 String strYMDHMS = "";
259 Date currentDateTime = new Date();
260 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
261 strYMDHMS = formatter.format(currentDateTime);
262 return strYMDHMS;
263 }
264
265 public static String getYMDHMSS() {
266 String strYMDHMSS = "";
267 Date currentDateTime = new Date();
268 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
269 strYMDHMSS = formatter.format(currentDateTime);
270 return strYMDHMSS;
271 }
272
273 public static String lengthConvesion(String strDateTime) {
274 String strYMDHMS = "";
275 if (strDateTime == null)
276 return "";
277 if (strDateTime.length() == 10)
278 strYMDHMS = " 23:59:59";
279 return (new StringBuilder(String.valueOf(strDateTime))).append(
280 strYMDHMS).toString();
281 }
282
283 public static Date praseStringToDate(String dateFormat,String dateStr)
284 {
285 SimpleDateFormat df =new SimpleDateFormat(dateFormat);
286 Date date=null;
287 try {
288 date = df.parse(dateStr);
289 } catch (ParseException e) {
290 // TODO Auto-generated catch block
291 e.printStackTrace();
292 }
293
294 return date;
295
296 }
297
298 public String getDateChange(String strCurrentDate, int iQuantity) {
299 String strTarget = "";
300 int iYear = Integer.parseInt(strCurrentDate.substring(0, 4));
301 int iMonth = Integer.parseInt(strCurrentDate.substring(5, 7));
302 int iDay = Integer.parseInt(strCurrentDate.substring(8, 10));
303 Calendar cal = Calendar.getInstance();
304 cal.set(1, iYear);
305 cal.set(2, iMonth - 1);
306 cal.set(5, iDay);
307 cal.add(5, iQuantity);
308 Date currentDate = cal.getTime();
309 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
310 strTarget = formatter.format(currentDate);
311 return strTarget;
312 }
313
314 public static String stringToNumber(String strDateTime) {
315 String strYMDHMS = "";
316 if (strDateTime == null)
317 return "";
318 strDateTime = strDateTime.trim();
319 if (strDateTime.length() == 8) {
320 strYMDHMS = (new StringBuilder(String.valueOf(strDateTime
321 .substring(0, 2)))).append(strDateTime.substring(3, 5))
322 .append(strDateTime.substring(6, 8)).toString();
323 }
324 if (strDateTime.length() == 10) {
325 strYMDHMS = (new StringBuilder(String.valueOf(strDateTime
326 .substring(0, 4)))).append(strDateTime.substring(5, 7))
327 .append(strDateTime.substring(8, 10)).toString();
328 strYMDHMS = (new StringBuilder(String.valueOf(strYMDHMS))).append(
329 "000000").toString();
330 return strYMDHMS;
331 }
332 if (strDateTime.length() == 18) {
333 int i = strDateTime.indexOf(" ");
334 int ii = strDateTime.indexOf(":");
335 String tempStr = strDateTime.substring(i + 1, ii);
336 tempStr = (new StringBuilder("0")).append(tempStr).toString();
337 strYMDHMS = (new StringBuilder(String.valueOf(strDateTime
338 .substring(0, 4)))).append(strDateTime.substring(5, 7))
339 .append(strDateTime.substring(8, 10)).append(tempStr)
340 .append(strDateTime.substring(13, 15)).append(
341 strDateTime.substring(16, 18)).toString();
342 return strYMDHMS;
343 }
344 if (strDateTime.length() == 19) {
345 int i = strDateTime.indexOf(" ");
346 int j = strDateTime.lastIndexOf(" ");
347 int l = strDateTime.length();
348 String temp = strDateTime;
349 if (i != j)
350 temp = (new StringBuilder(String.valueOf(strDateTime.substring(
351 0, j)))).append("0").append(
352 strDateTime.substring(j + 1, l)).toString();
353 strYMDHMS = (new StringBuilder(String.valueOf(temp.substring(0, 4))))
354 .append(temp.substring(5, 7)).append(temp.substring(8, 10))
355 .append(temp.substring(11, 13)).append(
356 temp.substring(14, 16)).append(
357 temp.substring(17, 19)).toString();
358 return strYMDHMS;
359 }
360 if (strDateTime.length() > 19) {
361 strDateTime = strDateTime.substring(0, 19);
362 int i = strDateTime.indexOf(" ");
363 int j = strDateTime.lastIndexOf(" ");
364 int l = strDateTime.length();
365 String temp = strDateTime;
366 if (i != j)
367 temp = (new StringBuilder(String.valueOf(strDateTime.substring(
368 0, j)))).append("0").append(
369 strDateTime.substring(j + 1, l)).toString();
370 strYMDHMS = (new StringBuilder(String.valueOf(temp.substring(0, 4))))
371 .append(temp.substring(5, 7)).append(temp.substring(8, 10))
372 .append(temp.substring(11, 13)).append(
373 temp.substring(14, 16)).append(
374 temp.substring(17, 19)).toString();
375 return strYMDHMS;
376 } else {
377 return "";
378 }
379 }
380
381 // 日期转化为字符串(后边不加0)
382 public static String stringToNumber_null(String strDateTime) {
383 String strYMDHMS = "";
384 if (strDateTime == null)
385 return "";
386 strDateTime = strDateTime.trim();
387 if (strDateTime.length() == 8) {
388 strYMDHMS = (new StringBuilder(String.valueOf(strDateTime
389 .substring(0, 2)))).append(strDateTime.substring(3, 5))
390 .append(strDateTime.substring(6, 8)).toString();
391 return strYMDHMS;
392 }
393 if (strDateTime.length() == 10) {
394 strYMDHMS = (new StringBuilder(String.valueOf(strDateTime
395 .substring(0, 4)))).append(strDateTime.substring(5, 7))
396 .append(strDateTime.substring(8, 10)).toString();
397 return strYMDHMS;
398 }
399 if (strDateTime.length() == 18) {
400 int i = strDateTime.indexOf(" ");
401 int ii = strDateTime.indexOf(":");
402 String tempStr = strDateTime.substring(i + 1, ii);
403 tempStr = (new StringBuilder("0")).append(tempStr).toString();
404 strYMDHMS = (new StringBuilder(String.valueOf(strDateTime
405 .substring(0, 4)))).append(strDateTime.substring(5, 7))
406 .append(strDateTime.substring(8, 10)).append(tempStr)
407 .append(strDateTime.substring(13, 15)).append(
408 strDateTime.substring(16, 18)).toString();
409 return strYMDHMS;
410 }
411 if (strDateTime.length() == 19) {
412 int i = strDateTime.indexOf(" ");
413 int j = strDateTime.lastIndexOf(" ");
414 int l = strDateTime.length();
415 String temp = strDateTime;
416 if (i != j)
417 temp = (new StringBuilder(String.valueOf(strDateTime.substring(
418 0, j)))).append("0").append(
419 strDateTime.substring(j + 1, l)).toString();
420 strYMDHMS = (new StringBuilder(String.valueOf(temp.substring(0, 4))))
421 .append(temp.substring(5, 7)).append(temp.substring(8, 10))
422 .append(temp.substring(11, 13)).append(
423 temp.substring(14, 16)).append(
424 temp.substring(17, 19)).toString();
425 return strYMDHMS;
426 }
427 if (strDateTime.length() > 19) {
428 strDateTime = strDateTime.substring(0, 19);
429 int i = strDateTime.indexOf(" ");
430 int j = strDateTime.lastIndexOf(" ");
431 int l = strDateTime.length();
432 String temp = strDateTime;
433 if (i != j)
434 temp = (new StringBuilder(String.valueOf(strDateTime.substring(
435 0, j)))).append("0").append(
436 strDateTime.substring(j + 1, l)).toString();
437 strYMDHMS = (new StringBuilder(String.valueOf(temp.substring(0, 4))))
438 .append(temp.substring(5, 7)).append(temp.substring(8, 10))
439 .append(temp.substring(11, 13)).append(
440 temp.substring(14, 16)).append(
441 temp.substring(17, 19)).toString();
442 return strYMDHMS;
443 } else {
444 return "";
445 }
446 }
447
448 public static String numberToString(String strYMDHMS) {
449 String strDateTime = "";
450 if (strYMDHMS == null || "".equals(strYMDHMS))
451 return "";
452 strYMDHMS = strYMDHMS.trim();
453 if (strYMDHMS.length() == 8) {
454 strDateTime = (new StringBuilder(String.valueOf(strYMDHMS
455 .substring(0, 4)))).append("-").append(
456 strYMDHMS.substring(4, 6)).append("-").append(
457 strYMDHMS.substring(6, 8)).toString();
458 return strDateTime;
459 }
460 if (strYMDHMS.length() == 14) {
461 if (strYMDHMS.endsWith("000000"))
462 strDateTime = (new StringBuilder(String.valueOf(strYMDHMS
463 .substring(0, 4)))).append("-").append(
464 strYMDHMS.substring(4, 6)).append("-").append(
465 strYMDHMS.substring(6, 8)).toString();
466 else
467 strDateTime = (new StringBuilder(String.valueOf(strYMDHMS
468 .substring(0, 4)))).append("-").append(
469 strYMDHMS.substring(4, 6)).append("-").append(
470 strYMDHMS.substring(6, 8)).append(" ").append(
471 strYMDHMS.substring(8, 10)).append(":").append(
472 strYMDHMS.substring(10, 12)).append(":").append(
473 strYMDHMS.substring(12, 14)).toString();
474 return strDateTime;
475 } else {
476 return strYMDHMS;
477 }
478 }
479
480 public static String startDatetoString(String strYMD) {
481 if (strYMD == null || "".equals(strYMD))
482 return "";
483 String strYMDHMS = "";
484 if (strYMD.length() == 10) {
485 strYMDHMS = (new StringBuilder(String.valueOf(strYMD
486 .substring(0, 4)))).append(strYMD.substring(5, 7)).append(
487 strYMD.substring(8, 10)).toString();
488 strYMDHMS = (new StringBuilder(String.valueOf(strYMDHMS))).append(
489 "000000").toString();
490 }
491 if (strYMD.length() == 19)
492 return stringToNumber(strYMD);
493 else
494 return strYMDHMS;
495 }
496
497 public static String endDatetoString(String strYMD) {
498 if (strYMD == null || "".equals(strYMD))
499 return "";
500 String strYMDHMS = "";
501 if (strYMD.length() == 10) {
502 strYMDHMS = (new StringBuilder(String.valueOf(strYMD
503 .substring(0, 4)))).append(strYMD.substring(5, 7)).append(
504 strYMD.substring(8, 10)).toString();
505 strYMDHMS = (new StringBuilder(String.valueOf(strYMDHMS))).append(
506 "235959").toString();
507 }
508 if (strYMD.length() == 19)
509 return stringToNumber(strYMD);
510 else
511 return strYMDHMS;
512 }
513
514 public static String getWeek() {
515 String strCurrentWeek = "";
516 Date currentWeek = new Date();
517 SimpleDateFormat formatter = new SimpleDateFormat("E");
518 strCurrentWeek = formatter.format(currentWeek);
519 return strCurrentWeek;
520 }
521
522 public static String getDateToWeek(String strDate) {
523 String strWeek = "";
524 int iYear = Integer.parseInt(strDate.substring(0, 4));
525 int iMonth = Integer.parseInt(strDate.substring(5, 7));
526 int iDay = Integer.parseInt(strDate.substring(8, 10));
527 Calendar cal = Calendar.getInstance();
528 cal.set(1, iYear);
529 cal.set(2, iMonth - 1);
530 cal.set(5, iDay);
531 Date currentDate = cal.getTime();
532 SimpleDateFormat formatter = new SimpleDateFormat("E");
533 strWeek = formatter.format(currentDate);
534 return strWeek;
535 }
536
537
538 /**
539 * 格式化日期字符串
540 * @param date
541 * @param format
542 * @return
543 */
544 public static String formatDateString(String date,String format)
545 {
546 //20090803
547 if(date.length()==8)
548 {
549 if(format.equals("yyyyMMdd"))
550 {
551 date=date;
552 }
553 if(format.equals("yyyy-MM-dd"))
554 {
555 date=DateUtil.numberToStringDate(date);
556 }
557 else if(format.equals("yyyyMMddHHmmss"))
558 {
559 date=date+"000000";
560 }
561 else if(format.equals("yyyy-MM-dd HH:mm:ss"))
562 {
563 date=DateUtil.numberToStringDate(date)+" 00:00:00";
564 }
565 }
566 //2009-08-03
567 else if(date.length()==10)
568 {
569 if(format.equals("yyyyMMdd"))
570 {
571 date=DateUtil.stringToNumber(date);
572 }
573 if(format.equals("yyyy-MM-dd"))
574 {
575 date=date;
576 }
577 else if(format.equals("yyyyMMddHHmmss"))
578 {
579 date=DateUtil.stringToNumber(date)+"000000";
580 }
581 else if(format.equals("yyyy-MM-dd HH:mm:ss"))
582 {
583 date=date+" 00:00:00";
584 }
585 }
586 else if(date.length()==14)
587 {
588 if(format.equals("yyyyMMdd"))
589 {
590 date=date.substring(0,8);
591 }
592 if(format.equals("yyyy-MM-dd"))
593 {
594 date=DateUtil.numberToStringDate(date.substring(0,8));
595 }
596 else if(format.equals("yyyyMMddHHmmss"))
597 {
598 date=date;
599 }
600 else if(format.equals("yyyy-MM-dd HH:mm:ss"))
601 {
602 date=DateUtil.numberToString(date);
603 }
604 }
605 else if(date.length()==19)
606 {
607 if(format.equals("yyyyMMdd"))
608 {
609 date=DateUtil.stringToNumber_null(date.substring(0,10));
610 }
611 if(format.equals("yyyy-MM-dd"))
612 {
613 date=date.substring(0,10);
614 }
615 else if(format.equals("yyyyMMddHHmmss"))
616 {
617 date=DateUtil.stringToNumber(date);
618 }
619 else if(format.equals("yyyy-MM-dd HH:mm:ss"))
620 {
621 date=date;
622 }
623 }
624 else if(date.length()>19)
625 {
626 date=DateUtil.formatDateString(date.substring(0,19),format);
627 }
628
629 return date;
630 }
631
632 }