1 java 日期转换2 packagecom.hoperun.self.supermarket.common;3
4 importjava.text.ParseException;5 importjava.text.SimpleDateFormat;6 importjava.util.Calendar;7 importjava.util.Date;8 importjava.util.GregorianCalendar;9
10 importjava.sql.Timestamp;11 importjava.util.ArrayList;12 importjava.util.List;13
14 public classDateFormatUtil {15
16 /**
17 * 获取当前的时间 yyyy-MM-dd HH:mm:ss18 *@return
19 *@throwsParseException20 */
21 public staticDate getNow(){22 Date date = newDate();23 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");24 String createdate =sdf.format(date);25 Date date2 = null;26 try{27 date2 =sdf.parse(createdate);28 } catch(ParseException e) {29 e.printStackTrace();30 }31 returndate2;32 }33
34 /**
35 * 获取当前的时间 yyyy-MM-dd36 *@return
37 */
38 public staticDate getNowDay(){39 Date date = newDate();40 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");41 String createdate =sdf.format(date);42 Date date2 = null;43 try{44 date2 =sdf.parse(createdate);45 } catch(ParseException e) {46 e.printStackTrace();47 }48 returndate2;49 }50
51 /**
52 * 获取当前的时间 yyyy-MM-dd53 *@return
54 */
55 public staticString getNowDayStr(){56 Date date = newDate();57 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");58 String createdate =sdf.format(date);59 returncreatedate;60 }61
62 /**
63 * 获取当前时间 yyyyMMddHHmmss64 *@return
65 */
66 public staticString getNowDayStr2(){67 Date date = newDate();68 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");69 String createdate =sdf.format(date);70 returncreatedate;71 }72
73 /**
74 * 获取前一天的时间 yyyy-MM-dd75 *@return
76 */
77 public staticString getLastDayStr() {78 Calendar ca =Calendar.getInstance();79 ca.setTime(newDate());80 ca.add(Calendar.DATE, -1);81 Date lastDay = ca.getTime(); //结果
82 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");83 String lastDayStr =sf.format(lastDay);84 returnlastDayStr;85 }86
87 /**
88 * 获取后一天的时间 yyyy-MM-dd89 *@return
90 */
91 public staticString getAfterDayStr() {92 Calendar ca =Calendar.getInstance();93 ca.setTime(newDate());94 ca.add(Calendar.DATE, 1);95 Date lastDay = ca.getTime(); //结果
96 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");97 String lastDayStr =sf.format(lastDay);98 returnlastDayStr;99 }100
101 /**
102 * 字符串转为日期格式103 *104 *@paramdateString105 *@return
106 *@throwsParseException107 */
108 public staticDate stringFormatDate(String dateString){109 SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");110 Date date = null;111 try{112 date =bartDateFormat.parse(dateString);113 } catch(ParseException e) {114 e.printStackTrace();115 }116 returndate;117 }118
119 /**
120 * 字符串转为日期格式121 *122 *@paramdateString123 *@return
124 *@throwsParseException125 */
126 public staticDate stringFormatDateTime(String dateString){127 SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");128 Date date = null;129 try{130 date =bartDateFormat.parse(dateString);131 } catch(ParseException e) {132 e.printStackTrace();133 }134 returndate;135 }136
137 /**
138 * 字符串转为日期格式139 *140 *@paramdateString141 *@return
142 *@throwsParseException143 */
144 public static Date stringFormatDateTimeNoSecond(String dateString) throwsParseException {145 SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");146 Date date =bartDateFormat.parse(dateString);147 returndate;148 }149
150 /**
151 * 字符串转为日期格式152 *153 *@paramdateString154 *@return
155 *@throwsParseException156 */
157 public static Date stringFormatDateTime2(String dateString) throwsParseException {158 SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");159 Date date =bartDateFormat.parse(dateString);160 returndate;161 }162
163 /**
164 * 将时间格式化为含时分秒的字符串165 *166 *@paramdate167 *@return
168 *@throwsParseException169 */
170 public staticString dateTimeFormatString(Date date){171 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");172 returndateFormat.format(date);173 }174
175 /**
176 * 将时间格式化为不含时分秒的字符串177 *178 *@paramdate179 *@return
180 *@throwsParseException181 */
182 public static String dateFormatString(Date date) throwsParseException {183 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");184 returndateFormat.format(date);185 }186
187 /**
188 * 将时间格式化为不含时分秒的字符串MM/dd189 *190 *@paramdate191 *@return
192 *@throwsParseException193 */
194 public static String dateFormatString2(Date date) throwsParseException {195 SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd");196 returndateFormat.format(date);197 }198
199 /**
200 * 将时间格式化为不含时分秒的字符串MM201 *202 *@paramdate203 *@return
204 *@throwsParseException205 */
206 public static String dateFormatString3(Date date) throwsParseException {207 SimpleDateFormat dateFormat = new SimpleDateFormat("M");208 returndateFormat.format(date);209 }210
211 /**
212 * 将int型日期转换为日期213 *214 *@paramdateInt215 *@return
216 */
217 public static Date intFormatDate(intdateInt) {218 Calendar c =Calendar.getInstance();219 long millions = new Long(dateInt).longValue() * 1000;220 c.setTimeInMillis(millions);221 returnc.getTime();222 }223
224
225 //获取当天的开始时间
226 public staticDate getDayBegin() {227 Calendar cal = newGregorianCalendar();228 cal.set(Calendar.HOUR_OF_DAY, 0);229 cal.set(Calendar.MINUTE, 0);230 cal.set(Calendar.SECOND, 0);231 cal.set(Calendar.MILLISECOND, 0);232 returncal.getTime();233 }234
235 //获取当天的结束时间
236 public staticDate getDayEnd() {237 Calendar cal = newGregorianCalendar();238 cal.set(Calendar.HOUR_OF_DAY, 23);239 cal.set(Calendar.MINUTE, 59);240 cal.set(Calendar.SECOND, 59);241 returncal.getTime();242 }243
244 //获取昨天的开始时间
245 public staticDate getBeginDayOfYesterday() {246 Calendar cal = newGregorianCalendar();247 cal.setTime(getDayBegin());248 cal.add(Calendar.DAY_OF_MONTH, -1);249 returncal.getTime();250 }251
252 //获取昨天的结束时间
253 public staticDate getEndDayOfYesterDay() {254 Calendar cal = newGregorianCalendar();255 cal.setTime(getDayEnd());256 cal.add(Calendar.DAY_OF_MONTH, -1);257 returncal.getTime();258 }259
260 //获取前天的开始时间
261 public staticDate getBeginDayOfYesterday2() {262 Calendar cal = newGregorianCalendar();263 cal.setTime(getDayBegin());264 cal.add(Calendar.DAY_OF_MONTH, -2);265 returncal.getTime();266 }267
268 //获取前天的结束时间
269 public staticDate getEndDayOfYesterDay2() {270 Calendar cal = newGregorianCalendar();271 cal.setTime(getDayEnd());272 cal.add(Calendar.DAY_OF_MONTH, -2);273 returncal.getTime();274 }275
276 //获取前3天的开始时间
277 public staticDate getBeginDayOfYesterday3() {278 Calendar cal = newGregorianCalendar();279 cal.setTime(getDayBegin());280 cal.add(Calendar.DAY_OF_MONTH, -3);281 returncal.getTime();282 }283
284 //获取前3天的结束时间
285 public staticDate getEndDayOfYesterDay3() {286 Calendar cal = newGregorianCalendar();287 cal.setTime(getDayEnd());288 cal.add(Calendar.DAY_OF_MONTH, -3);289 returncal.getTime();290 }291
292 //获取前4天的开始时间
293 public staticDate getBeginDayOfYesterday4() {294 Calendar cal = newGregorianCalendar();295 cal.setTime(getDayBegin());296 cal.add(Calendar.DAY_OF_MONTH, -4);297 returncal.getTime();298 }299
300 //获取前4天的结束时间
301 public staticDate getEndDayOfYesterDay4() {302 Calendar cal = newGregorianCalendar();303 cal.setTime(getDayEnd());304 cal.add(Calendar.DAY_OF_MONTH, -4);305 returncal.getTime();306 }307
308 //获取前5天的开始时间
309 public staticDate getBeginDayOfYesterday5() {310 Calendar cal = newGregorianCalendar();311 cal.setTime(getDayBegin());312 cal.add(Calendar.DAY_OF_MONTH, -5);313 returncal.getTime();314 }315
316 //获取前5天的结束时间
317 public staticDate getEndDayOfYesterDay5() {318 Calendar cal = newGregorianCalendar();319 cal.setTime(getDayEnd());320 cal.add(Calendar.DAY_OF_MONTH, -5);321 returncal.getTime();322 }323
324 //获取前6天的开始时间
325 public staticDate getBeginDayOfYesterday6() {326 Calendar cal = newGregorianCalendar();327 cal.setTime(getDayBegin());328 cal.add(Calendar.DAY_OF_MONTH, -6);329 returncal.getTime();330 }331
332 //获取前6天的结束时间
333 public staticDate getEndDayOfYesterDay6() {334 Calendar cal = newGregorianCalendar();335 cal.setTime(getDayEnd());336 cal.add(Calendar.DAY_OF_MONTH, -6);337 returncal.getTime();338 }339
340 //获取明天的开始时间
341 public staticDate getBeginDayOfTomorrow() {342 Calendar cal = newGregorianCalendar();343 cal.setTime(getDayBegin());344 cal.add(Calendar.DAY_OF_MONTH, 1);345
346 returncal.getTime();347 }348
349 //获取明天的结束时间
350 public staticDate getEndDayOfTomorrow() {351 Calendar cal = newGregorianCalendar();352 cal.setTime(getDayEnd());353 cal.add(Calendar.DAY_OF_MONTH, 1);354 returncal.getTime();355 }356
357 //获取本周的开始时间
358 @SuppressWarnings("unused")359 public staticDate getBeginDayOfWeek() {360 Date date = newDate();361 if (date == null) {362 return null;363 }364 Calendar cal =Calendar.getInstance();365 cal.setTime(date);366 int dayofweek =cal.get(Calendar.DAY_OF_WEEK);367 if (dayofweek == 1) {368 dayofweek += 7;369 }370 cal.add(Calendar.DATE, 2 -dayofweek);371 returngetDayStartTime(cal.getTime());372 }373
374 //获取本周的结束时间
375 public staticDate getEndDayOfWeek() {376 Calendar cal =Calendar.getInstance();377 cal.setTime(getBeginDayOfWeek());378 cal.add(Calendar.DAY_OF_WEEK, 6);379 Date weekEndSta =cal.getTime();380 returngetDayEndTime(weekEndSta);381 }382
383 //获取上周的开始时间
384 @SuppressWarnings("unused")385 public staticDate getBeginDayOfLastWeek() {386 Date date = newDate();387 if (date == null) {388 return null;389 }390 Calendar cal =Calendar.getInstance();391 cal.setTime(date);392 int dayofweek =cal.get(Calendar.DAY_OF_WEEK);393 if (dayofweek == 1) {394 dayofweek += 7;395 }396 cal.add(Calendar.DATE, 2 - dayofweek - 7);397 returngetDayStartTime(cal.getTime());398 }399
400 //获取上周的结束时间
401 public staticDate getEndDayOfLastWeek() {402 Calendar cal =Calendar.getInstance();403 cal.setTime(getBeginDayOfLastWeek());404 cal.add(Calendar.DAY_OF_WEEK, 6);405 Date weekEndSta =cal.getTime();406 returngetDayEndTime(weekEndSta);407 }408
409 //获取上2周的开始时间
410 @SuppressWarnings("unused")411 public staticDate getBeginDayOfLast2Week() {412 Date date = newDate();413 if (date == null) {414 return null;415 }416 Calendar cal =Calendar.getInstance();417 cal.setTime(date);418 int dayofweek =cal.get(Calendar.DAY_OF_WEEK);419 if (dayofweek == 1) {420 dayofweek += 7;421 }422 cal.add(Calendar.DATE, 2 - dayofweek - 14);423 returngetDayStartTime(cal.getTime());424 }425
426 //获取上2周的结束时间
427 public staticDate getEndDayOfLast2Week() {428 Calendar cal =Calendar.getInstance();429 cal.setTime(getBeginDayOfLast2Week());430 cal.add(Calendar.DAY_OF_WEEK, 6);431 Date weekEndSta =cal.getTime();432 returngetDayEndTime(weekEndSta);433 }434
435 //获取上3周的开始时间
436 @SuppressWarnings("unused")437 public staticDate getBeginDayOfLast3Week() {438 Date date = newDate();439 if (date == null) {440 return null;441 }442 Calendar cal =Calendar.getInstance();443 cal.setTime(date);444 int dayofweek =cal.get(Calendar.DAY_OF_WEEK);445 if (dayofweek == 1) {446 dayofweek += 7;447 }448 cal.add(Calendar.DATE, 2 - dayofweek - 21);449 returngetDayStartTime(cal.getTime());450 }451
452 //获取上3周的结束时间
453 public staticDate getEndDayOfLast3Week() {454 Calendar cal =Calendar.getInstance();455 cal.setTime(getBeginDayOfLast3Week());456 cal.add(Calendar.DAY_OF_WEEK, 6);457 Date weekEndSta =cal.getTime();458 returngetDayEndTime(weekEndSta);459 }460
461 //获取本月的开始时间
462 public staticDate getBeginDayOfMonth() {463 Calendar calendar =Calendar.getInstance();464 calendar.set(getNowYear(), getNowMonth() - 1, 1);465 returngetDayStartTime(calendar.getTime());466 }467
468 //获取本月的结束时间
469 public staticDate getEndDayOfMonth() {470 Calendar calendar =Calendar.getInstance();471 calendar.set(getNowYear(), getNowMonth() - 1, 1);472 int day = calendar.getActualMaximum(5);473 calendar.set(getNowYear(), getNowMonth() - 1, day);474 returngetDayEndTime(calendar.getTime());475 }476
477 //获取上月的开始时间
478 public staticDate getBeginDayOfLastMonth() {479 Calendar calendar =Calendar.getInstance();480 calendar.set(getNowYear(), getNowMonth() - 2, 1);481 returngetDayStartTime(calendar.getTime());482 }483
484 //获取上月的结束时间
485 public staticDate getEndDayOfLastMonth() {486 Calendar calendar =Calendar.getInstance();487 calendar.set(getNowYear(), getNowMonth() - 2, 1);488 int day = calendar.getActualMaximum(5);489 calendar.set(getNowYear(), getNowMonth() - 2, day);490 returngetDayEndTime(calendar.getTime());491 }492
493 //获取上2月的开始时间
494 public staticDate getBeginDayOfLast2Month() {495 Calendar calendar =Calendar.getInstance();496 calendar.set(getNowYear(), getNowMonth() - 3, 1);497 returngetDayStartTime(calendar.getTime());498 }499
500 //获取上2月的结束时间
501 public staticDate getEndDayOfLast2Month() {502 Calendar calendar =Calendar.getInstance();503 calendar.set(getNowYear(), getNowMonth() - 3, 1);504 int day = calendar.getActualMaximum(5);505 calendar.set(getNowYear(), getNowMonth() - 3, day);506 returngetDayEndTime(calendar.getTime());507 }508
509 //获取上月的开始时间
510 public staticDate getBeginDayOfLast3Month() {511 Calendar calendar =Calendar.getInstance();512 calendar.set(getNowYear(), getNowMonth() - 4, 1);513 returngetDayStartTime(calendar.getTime());514 }515
516 //获取上月的结束时间
517 public staticDate getEndDayOfLast3Month() {518 Calendar calendar =Calendar.getInstance();519 calendar.set(getNowYear(), getNowMonth() - 4, 1);520 int day = calendar.getActualMaximum(5);521 calendar.set(getNowYear(), getNowMonth() - 4, day);522 returngetDayEndTime(calendar.getTime());523 }524
525 //获取本年的开始时间
526 public staticjava.util.Date getBeginDayOfYear() {527 Calendar cal =Calendar.getInstance();528 cal.set(Calendar.YEAR, getNowYear());529 //cal.set
530 cal.set(Calendar.MONTH, Calendar.JANUARY);531 cal.set(Calendar.DATE, 1);532
533 returngetDayStartTime(cal.getTime());534 }535
536 //获取本年的结束时间
537 public staticjava.util.Date getEndDayOfYear() {538 Calendar cal =Calendar.getInstance();539 cal.set(Calendar.YEAR, getNowYear());540 cal.set(Calendar.MONTH, Calendar.DECEMBER);541 cal.set(Calendar.DATE, 31);542 returngetDayEndTime(cal.getTime());543 }544
545 //获取某个日期的开始时间
546 public staticTimestamp getDayStartTime(Date d) {547 Calendar calendar =Calendar.getInstance();548 if (null !=d)549 calendar.setTime(d);550 calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,551 0, 0);552 calendar.set(Calendar.MILLISECOND, 0);553 return newTimestamp(calendar.getTimeInMillis());554 }555
556 //获取某个日期的结束时间
557 public staticTimestamp getDayEndTime(Date d) {558 Calendar calendar =Calendar.getInstance();559 if (null !=d)560 calendar.setTime(d);561 calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,562 59, 59);563 calendar.set(Calendar.MILLISECOND, 999);564 return newTimestamp(calendar.getTimeInMillis());565 }566
567 //获取今年是哪一年
568 public staticInteger getNowYear() {569 Date date = newDate();570 GregorianCalendar gc =(GregorianCalendar) Calendar.getInstance();571 gc.setTime(date);572 return Integer.valueOf(gc.get(1));573 }574
575 //获取本月是哪一月
576 public static intgetNowMonth() {577 Date date = newDate();578 GregorianCalendar gc =(GregorianCalendar) Calendar.getInstance();579 gc.setTime(date);580 return gc.get(2) + 1;581 }582
583 //两个日期相减得到的天数
584 public static intgetDiffDays(Date beginDate, Date endDate) {585
586 if (beginDate == null || endDate == null) {587 throw new IllegalArgumentException("getDiffDays param is null!");588 }589
590 long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24);591
592 int days = newLong(diff).intValue();593
594 returndays;595 }596
597 //两个日期相减得到的毫秒数
598 public static longdateDiff(Date beginDate, Date endDate) {599 long date1ms =beginDate.getTime();600 long date2ms =endDate.getTime();601 return date2ms -date1ms;602 }603
604 //获取两个日期中的最大日期
605 public staticDate max(Date beginDate, Date endDate) {606 if (beginDate == null) {607 returnendDate;608 }609 if (endDate == null) {610 returnbeginDate;611 }612 if(beginDate.after(endDate)) {613 returnbeginDate;614 }615 returnendDate;616 }617
618 //获取两个日期中的最小日期
619 public staticDate min(Date beginDate, Date endDate) {620 if (beginDate == null) {621 returnendDate;622 }623 if (endDate == null) {624 returnbeginDate;625 }626 if(beginDate.after(endDate)) {627 returnendDate;628 }629 returnbeginDate;630 }631
632 //返回某月该季度的第一个月
633 public staticDate getFirstSeasonDate(Date date) {634 final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};635 Calendar cal =Calendar.getInstance();636 cal.setTime(date);637 int sean =SEASON[cal.get(Calendar.MONTH)];638 cal.set(Calendar.MONTH, sean * 3 - 3);639 returncal.getTime();640 }641
642 //返回某个日期下几天的日期
643 public static Date getNextDay(Date date, inti) {644 Calendar cal = newGregorianCalendar();645 cal.setTime(date);646 cal.set(Calendar.DATE, cal.get(Calendar.DATE) +i);647 returncal.getTime();648 }649
650 //返回某个日期前几天的日期
651 public static Date getFrontDay(Date date, inti) {652 Calendar cal = newGregorianCalendar();653 cal.setTime(date);654 cal.set(Calendar.DATE, cal.get(Calendar.DATE) -i);655 returncal.getTime();656 }657
658 //返回昨天的日期
659 public staticString getYesterDay() {660 Date date = new Date();//取时间
661 Calendar calendar = newGregorianCalendar();662 calendar.setTime(date);663 calendar.add(Calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
664 date = calendar.getTime(); //这个时间就是日期往后推一天的结果
665 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");666 String dateString =formatter.format(date);667 returndateString;668 }669
670 //获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
671 @SuppressWarnings({ "rawtypes", "unchecked"})672 public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, intk) {673 List list = newArrayList();674 if (beginYear ==endYear) {675 for (int j = beginMonth; j <= endMonth; j++) {676 list.add(getTimeList(beginYear, j, k));677
678 }679 } else{680 {681 for (int j = beginMonth; j < 12; j++) {682 list.add(getTimeList(beginYear, j, k));683 }684
685 for (int i = beginYear + 1; i < endYear; i++) {686 for (int j = 0; j < 12; j++) {687 list.add(getTimeList(i, j, k));688 }689 }690 for (int j = 0; j <= endMonth; j++) {691 list.add(getTimeList(endYear, j, k));692 }693 }694 }695 returnlist;696 }697
698 //获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
699 @SuppressWarnings({ "unchecked", "rawtypes"})700 public static List getTimeList(int beginYear, int beginMonth, intk) {701 List list = newArrayList();702 Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);703 int max =begincal.getActualMaximum(Calendar.DATE);704 for (int i = 1; i < max; i = i +k) {705 list.add(begincal.getTime());706 begincal.add(Calendar.DATE, k);707 }708 begincal = newGregorianCalendar(beginYear, beginMonth, max);709 list.add(begincal.getTime());710 returnlist;711 }712 }