1 /**
2 * 格式化字符串为日期格式3 *4 *@paramdateStr 需要格式化的字符串5 *@paramformat 需要的日期格式,例如"yyyy-MM-dd HH:mm:ss"6 *@return
7 */
8 public staticDate formatDate(String dateStr, String format) {9 SimpleDateFormat dateFormat = newSimpleDateFormat(format,10 Locale.CHINESE);11 try{12 returndateFormat.parse(dateStr);13 } catch(ParseException e) {14 e.printStackTrace();15 }16
17 return null;18 }19
20 /**
21 * 格式化字符串为"yyyy-MM-dd HH:mm:ss"的日期22 *23 *@paramdateStr24 *@return
25 */
26 public staticDate formatDate(String dateStr) {27 if(StrUtil.isBlank(dateStr)){28 return null;29 }30 return formatDate(dateStr, "yyyy-MM-dd HH:mm:ss");31 }32
33 public static String getFormatDateStr(longdate, String format) {34 return formatDate2Str(newDate(date), format);35 }36
37 public staticString getMonthDayStr(Date date) {38 if (date == null) {39 return "";40 }41 return formatDate2Str(date, "MM-dd");42 }43
44 public staticString getNormalDateStr(Date date) {45 if (date == null) {46 return "";47 }48 return formatDate2Str(date, "yyyy-MM-dd HH:mm:ss");49 }50
51 public staticString formatDate2Str(Date date, String formatter) {52 SimpleDateFormat sdf = newSimpleDateFormat(formatter);53 returnsdf.format(date);54 }55
56 /**
57 * date 日期加上,或减去几天58 *59 *@paramdate60 *@paramday61 *@return
62 */
63 public static Date addDateInDiff(Date date, intday) {64 Calendar cal =Calendar.getInstance();65 cal.setTime(date);66 cal.add(Calendar.DATE, day);67 returncal.getTime();68 }69
70 public static Date addMinuteInDiff(Date date, intminute) {71 Calendar cal =Calendar.getInstance();72 cal.setTime(date);73 cal.add(Calendar.MINUTE, minute);74 returncal.getTime();75 }76
77 public static Date addSecondInDiff(Date date, intsec) {78 Calendar cal =Calendar.getInstance();79 cal.setTime(date);80 cal.add(Calendar.SECOND, sec);81 returncal.getTime();82 }83
84 /**
85 * 日期加个月86 *87 *@paramdate88 *@parammon89 *@return
90 */
91 public static Date addMonthInDiff(Date date, intmon) {92 Calendar cal =Calendar.getInstance();93 cal.setTime(date);94 cal.add(Calendar.MONTH, mon);95 returncal.getTime();96 }97
98 public static Date addYearInDiff(Date date, intmon) {99 Calendar cal =Calendar.getInstance();100 cal.setTime(date);101 cal.add(Calendar.YEAR, mon);102 returncal.getTime();103 }104
105 /**
106 * 获取当前日期是星期几
107 *108 *@paramdt109 *@return当前日期是星期几110 */
111 public staticString getWeekOfDate(Date dt) {112 String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};113 Calendar cal =Calendar.getInstance();114 cal.setTime(dt);115 int w = cal.get(Calendar.DAY_OF_WEEK) - 1;116 if (w < 0)117 w = 0;118 returnweekDays[w];119 }120
121 /**
122 * 获取当前日期是周几
123 *124 *@paramdt125 *@return当前日期是周几126 */
127 public staticString getWeekOfDate2(Date dt) {128 String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};129 Calendar cal =Calendar.getInstance();130 cal.setTime(dt);131 int w = cal.get(Calendar.DAY_OF_WEEK) - 1;132 if (w < 0)133 w = 0;134 returnweekDays[w];135 }136
137 /**
138 *
139 * 判断date和当前日期是否在同一周内140 * 注:141 * Calendar类提供了一个获取日期在所属年份中是第几周的方法,对于上一年末的某一天142 * 和新年初的某一天在同一周内也一样可以处理,例如2012-12-31和2013-01-01虽然在143 * 不同的年份中,但是使用此方法依然判断二者属于同一周内144 *145 *146 *@paramdate147 *@return
148 */
149 public static booleanisSameWeekWithToday(Date date) {150 return isSameWeek(date, newDate());151 }152
153 public static booleanisSameWeek(Date date1, Date date2) {154 if (date1 == null || date2 == null) {155 return false;156 }157
158 //0.先把Date类型的对象转换Calendar类型的对象
159 Calendar date1Cal =Calendar.getInstance();160 Calendar date2Cal =Calendar.getInstance();161
162 date1Cal.setTime(date1);163 date2Cal.setTime(date2);164
165 //1.比较当前日期在年份中的周数是否相同
166 if (date1Cal.get(Calendar.WEEK_OF_YEAR) ==date2Cal167 .get(Calendar.WEEK_OF_YEAR)) {168 return true;169 } else{170 return false;171 }172 }173
174 /**
175 * 取得当前日期所在周的第一天176 *177 *@paramdate178 *@return
179 */
180 public staticDate getFirstDayOfWeek(Date date) {181 Calendar c = newGregorianCalendar();182 c.setFirstDayOfWeek(Calendar.MONDAY);183 c.setTime(date);184 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); //Monday
185 returnc.getTime();186 }187
188 /**
189 * 取得当前日期是多少周190 *191 *@paramdate192 *@return
193 */
194 public static intgetWeekOfYear(Date date) {195 Calendar c = newGregorianCalendar();196 c.setFirstDayOfWeek(Calendar.MONDAY);197 c.setMinimalDaysInFirstWeek(7);198 c.setTime(date);199
200 returnc.get(Calendar.WEEK_OF_YEAR);201 }202
203 /**
204 * 得到某一年周的总数205 *206 *@paramyear207 *@return
208 */
209 public static int getMaxWeekNumOfYear(intyear) {210 Calendar c = newGregorianCalendar();211 c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);212
213 returngetWeekOfYear(c.getTime());214 }215
216 /**
217 * 得到某年某周的第一天218 *219 *@paramyear220 *@paramweek221 *@return
222 */
223 public static Date getFirstDayOfWeek(int year, intweek) {224 Calendar c = newGregorianCalendar();225 c.set(Calendar.YEAR, year);226 c.set(Calendar.MONTH, Calendar.JANUARY);227 c.set(Calendar.DATE, 1);228
229 Calendar cal =(GregorianCalendar) c.clone();230 cal.add(Calendar.DATE, week * 7);231
232 returngetFirstDayOfWeek(cal.getTime());233 }234
235 /**
236 * 得到某年某周的最后一天237 *238 *@paramyear239 *@paramweek240 *@return
241 */
242 public static Date getLastDayOfWeek(int year, intweek) {243 Calendar c = newGregorianCalendar();244 c.set(Calendar.YEAR, year);245 c.set(Calendar.MONTH, Calendar.JANUARY);246 c.set(Calendar.DATE, 1);247
248 Calendar cal =(GregorianCalendar) c.clone();249 cal.add(Calendar.DATE, week * 7);250
251 returngetLastDayOfWeek(cal.getTime());252 }253
254 /**
255 * 取得当前日期所在周的最后一天256 *257 *@paramdate258 *@return
259 */
260 public staticDate getLastDayOfWeek(Date date) {261 Calendar c = newGregorianCalendar();262 c.setFirstDayOfWeek(Calendar.MONDAY);263 c.setTime(date);264 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); //Sunday
265 returnc.getTime();266 }267
268 /**
269 * 获得当前日期所在月份的最后一天(最后一个day)270 *271 *@paramdate272 *@returnDate273 */
274 public staticDate getLastDayOfMonth(Date date) {275 Calendar ca =Calendar.getInstance();276 ca.setTime(date);277 ca.set(Calendar.DAY_OF_MONTH,278 ca.getActualMaximum(Calendar.DAY_OF_MONTH));279 returnca.getTime();280 }281
282 public staticDate getFirstDayOfMonth(Date date) {283 Calendar c =Calendar.getInstance();284 c.add(Calendar.MONTH, 0);285 c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
286 returnc.getTime();287 }288
289 /**
290 * 获取两个日期之间的天数291 *292 *@paramstartDate293 *@paramendDate294 *@return
295 */
296 public staticLong getDaysBetween(Date startDate, Date endDate) {297 Calendar fromCalendar =Calendar.getInstance();298 fromCalendar.setTime(startDate);299 fromCalendar.set(Calendar.HOUR_OF_DAY, 0);300 fromCalendar.set(Calendar.MINUTE, 0);301 fromCalendar.set(Calendar.SECOND, 0);302 fromCalendar.set(Calendar.MILLISECOND, 0);303
304 Calendar toCalendar =Calendar.getInstance();305 toCalendar.setTime(endDate);306 toCalendar.set(Calendar.HOUR_OF_DAY, 0);307 toCalendar.set(Calendar.MINUTE, 0);308 toCalendar.set(Calendar.SECOND, 0);309 toCalendar.set(Calendar.MILLISECOND, 0);310
311 return (toCalendar.getTime().getTime() -fromCalendar.getTime()312 .getTime()) / (1000 * 60 * 60 * 24);313 }314
315 /**
316 * 计算两个分钟差317 *318 *@return
319 */
320 public staticLong getMinutesBetween(Date startDate, Date endDate) {321 long diff = startDate.getTime() - endDate.getTime();//这样得到的差值是微秒级别
322 long days = diff / (1000 * 60 * 60 * 24);323
324 long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);325 long minutes = diff / (1000 * 60);326 System.out.println("" + days + "天" + hours + "小时" + minutes + "分");327 returnminutes;328 }329
330 /**
331 * 计算两个小时差332 *333 *@return
334 */
335 public staticLong getHourBetween(Date startDate, Date endDate) {336 long diff = startDate.getTime() - endDate.getTime();//这样得到的差值是微秒级别
337 long days = diff / (1000 * 60 * 60 * 24);338
339 long hours = diff / (1000 * 60 * 60);340 System.out.println("" + days + "天" + hours + "小时" + "分");341 returnhours;342 }343
344 /**
345 * 计算两个秒差346 *347 *@return
348 */
349 public staticLong getSecondBetween(Date startDate, Date endDate) {350 long diff = startDate.getTime() - endDate.getTime();//这样得到的差值是微秒级别
351 long seconds = diff / (1000);352 returnseconds;353 }354
355 /**
356 * 日期转换为今天,明天,后天357 * startDate (2018-09-10 00:00:00)358 */
359 public staticString getDateDesc(Date startDate) {360 String todayStr = formatDate2Str(new Date(), "yyyy-MM-dd");361 Date today = formatDate(todayStr + " 00:00:00");362 long days =getDaysBetween(today, startDate);363 if (days == 0) {364 return "今天";365 } else if (days == 1) {366 return "明天";367 } else if (days == 2) {368 return "后天";369 }370 return formatDate2Str(startDate, "yyyy-MM-dd");371 }372
373 /**
374 * 显示时间,如果与当前时间差别小于一天,则自动用**秒(分,小时)前,如果大于一天则用format规定的格式显示375 *376 *@paramctime 时间377 *@paramformat 格式 格式描述:例如:yyyy-MM-dd yyyy-MM-dd HH:mm:ss378 *@return
379 */
380 public staticString showTime(Date ctime, String format) {381 //System.out.println("当前时间是:"+new382 //Timestamp(System.currentTimeMillis()));383
384 //System.out.println("发布时间是:"+df.format(ctime).toString());
385 String r = "";386 if (ctime == null)387 returnr;388 if (format == null)389 format = "MM-dd HH:mm";390
391 boolean isSameYear = isSameYear(ctime, newDate());392 if (!isSameYear) {393 format = "yy-M-d";394 SimpleDateFormat df = newSimpleDateFormat(format);395 returndf.format(ctime);396 }397
398 long nowtimelong =System.currentTimeMillis();399
400 long ctimelong =ctime.getTime();401 long result = Math.abs(nowtimelong -ctimelong);402
403 if (result < 60000) {//一分钟内404 //long seconds = result / 1000;405 //if(seconds == 0){406 //r = "刚刚";407 //}else{408 //r = seconds + "秒前";409 //}
410 r = "刚刚";411 } else if (result >= 60000 && result < 3600000) {//一小时内
412 long seconds = result / 60000;413 r = seconds + "分钟前";414 } else if (result >= 3600000 && result < 86400000) {//一天内
415 long seconds = result / 3600000;416 r = seconds + "小时前";417 } else if (result > 86400000 && result < 172800000) {//三十天内418 //long seconds = result / 86400000;419 //r = seconds + "天前";
420 r = "昨天";421 } else if (result >= 172800000) {422 //format = "M-d";
423 SimpleDateFormat df = newSimpleDateFormat(format);424 r =df.format(ctime).toString();425 }426 //else{//日期格式427 //format="MM-dd HH:mm";428 //SimpleDateFormat df = new SimpleDateFormat(format);429 //r = df.format(ctime).toString();430 //}
431 returnr;432 }433
434 public static booleanisSameYear(Date ctime, Date nTime) {435 Calendar cDate =Calendar.getInstance();436 cDate.setTime(ctime);437 Calendar nDate =Calendar.getInstance();438 nDate.setTime(nTime);439 int cYear =cDate.get(Calendar.YEAR);440 int nYear =nDate.get(Calendar.YEAR);441 if (cYear ==nYear) {442 return true;443 }444 return false;445 }446
447 /***448 * 出生日期转换年龄449 ***/
450 public static intgetAgeByBirthday(Date birthday) {451 Calendar cal =Calendar.getInstance();452 if (birthday.getTime() > newDate().getTime()) {453 return 0;454 }455 int year =cal.get(Calendar.YEAR);456 int month = cal.get(Calendar.MONTH) + 1;457 int day =cal.get(Calendar.DAY_OF_MONTH);458
459 cal.setTime(birthday);460 int yearBirth =cal.get(Calendar.YEAR);461 int monthBirth = cal.get(Calendar.MONTH) + 1;462 int dayBirth =cal.get(Calendar.DAY_OF_MONTH);463 int age = year -yearBirth;464 if (monthBirth >month)465 return age - 1;466 if (monthBirth == month && dayBirth >day)467 return age - 1;468 return age > 0 ? age : 0;469 }470
471 /***472 * 根据年龄取得出生日期473 *474 *@paramage475 *@return
476 */
477 public static String getBirthdayByAge(intage) {478 Calendar cal =Calendar.getInstance();479 int year =cal.get(Calendar.YEAR);480 int month = cal.get(Calendar.MONTH) + 1;481 int day =cal.get(Calendar.DAY_OF_MONTH);482 int birthYear = year -age;483 Date birthDay = formatDate(birthYear + "-" + month + "-" +day,484 "yyyy-MM-dd");485 return formatDate2Str(birthDay, "yyyy-MM-dd");486 }487
488
489 /**
490 * 判断结束时间是否早于当前时间491 **/
492 public static booleanisTimeout(String date) {493 if (date == null)494 return false;495 long now = newDate().getTime();496 long end =formatDate(date.toString()).getTime();497 if (now >end)498 return true;499 return false;500 }501
502 /**
503 *@paramdate504 *@return
505 */
506 public static booleanisTimeout(Date date) {507 if (date == null) return false;508 long now = newDate().getTime();509 long end =date.getTime();510 if (now > end) return true;511 return false;512 }513
514 /**
515 * 判断date2时间是否早于date1时间516 **/
517 public static booleanisTimeout(String date1, String date2) {518 if (date1 == null || date2 == null)519 return false;520 long start =formatDate(date2.toString()).getTime();521 long end =formatDate(date1.toString()).getTime();522 if (start >end)523 return true;524 return false;525 }526
527 /**
528 * 判断传入的时间是否已满一周年529 */
530 public static booleanisLastYear(Date date) {531 Calendar calendar =Calendar.getInstance();532 Date lastYear = newDate(System.currentTimeMillis());533 calendar.setTime(lastYear);534 calendar.add(Calendar.YEAR, -1);535 lastYear =calendar.getTime();536 if (date.getTime()
542 public static intgetSpecVoteFailTime() {543 Date nowDate = newDate();544 Date addDateInDiff = addDateInDiff(nowDate, 1);545 String endDateStr = formatDate2Str(addDateInDiff, "yyyy-MM-dd") + " 00:00:00";546 Date formatDate =formatDate(endDateStr);547 long time1 =formatDate.getTime();548 long time2 =nowDate.getTime();549 long abs = Math.abs(time1 -time2);550 long l = abs / 1000;551 returnInteger.valueOf(String.valueOf(l));552 }553
554 //2018-09-17 9时 转成日期格式
555 public staticString convertShowTimeToDate(String date) {556 StringBuilder str = newStringBuilder();557 str.append(date);558 if (date.length() == 13) {559 str.insert(11, 0);560 }561 date = date.replace("时", ":00:00");562 System.out.println(date);563 returndate;564 }565
566 //日期格式转成2018-09-17 9时
567 public staticString convertDateToShowTime(Date date) {568 String dateStr = UtilDate.formatDate2Str(date, "yyyy-MM-dd HH");569 String regx = "\\s0";570 dateStr = dateStr.replaceAll(regx, " ") + "时";571 System.out.println(dateStr);572 returndateStr;573 }574
575
576 /**
577 * 第几个工作日578 *579 *@paramdate580 *@paramdays581 *@return
582 */
583 public static Date getWorkDte(Date date, intdays) {584
585 Calendar calendar =Calendar.getInstance();586 calendar.setTime(date);587 if (days > 0) {588 for (int i = 1; i <= days; i++) {589 calendar.add(Calendar.DAY_OF_YEAR, 1);590 if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) ==Calendar.SUNDAY) {591 i--;592 }593 }594 } else if (days < 0) {595 for (int i = 0; i > days; i--) {596 calendar.add(Calendar.DAY_OF_YEAR, -1);597 if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) ==Calendar.SUNDAY) {598 i++;599 }600 }601 }602 returncalendar.getTime();603 }604
605 /**
606 * 几小时几分钟607 *608 *@paramseconds609 *@return
610 */
611 public static String getTimeStrBySeconds(intseconds) {612 String timeStr = "";613 int hour = seconds / (60 * 60);614 int minute = seconds % (60 * 60) / 60;615 if (0 ==hour) {616 timeStr = seconds % (60 * 60) / 60 + "分钟";617
618 } else{619 timeStr = seconds / (60 * 60) + "小时" + seconds % (60 * 60) / 60 + "分钟";620
621 }622 returntimeStr;623 }624
625 //判断选择的日期是否是本周
626 public static boolean isThisWeek(longtime)627 {628 Calendar calendar =Calendar.getInstance();629 int currentWeek =calendar.get(Calendar.WEEK_OF_YEAR);630 calendar.setTime(newDate(time));631 int paramWeek =calendar.get(Calendar.WEEK_OF_YEAR);632 if(paramWeek==currentWeek){633 return true;634 }635 return false;636 }637 //判断选择的日期是否是今天
638 public static boolean isToday(longtime)639 {640 return isThisTime(time,"yyyy-MM-dd");641 }642 //判断选择的日期是否是本月
643 public static boolean isThisMonth(longtime)644 {645 return isThisTime(time,"yyyy-MM");646 }647 private static boolean isThisTime(longtime,String pattern) {648 Date date = newDate(time);649 SimpleDateFormat sdf = newSimpleDateFormat(pattern);650 String param = sdf.format(date);//参数时间
651 String now = sdf.format(new Date());//当前时间
652 if(param.equals(now)){653 return true;654 }655 return false;656 }657
658 /**
659 * 当天时间获取时分660 *@return
661 */
662 public staticString getIsTodayStartTime(Date date){663 //Date date = UtilDate.formatDate(time, "yyyy-MM-dd HH:mm");
664 long time1 =date.getTime();665 boolean today =UtilDate.isToday(time1);666 String todayTime = UtilDate.getFormatDateStr(date.getTime(),"yyyy-MM-dd HH:mm");667 if(today){668
669 String regex = " ";670 String[] split =todayTime.split(regex);671 return "今天 " + split[1];672 }673 returntodayTime;674 }675
676 /**
677 * 当天时间获取时分678 *@return
679 */
680 public staticString getIsTodayEndTime(Date date){681 //Date date = UtilDate.formatDate(time, "yyyy-MM-dd HH:mm");
682 long time1 =date.getTime();683 boolean today =UtilDate.isToday(time1);684 String todayTime = UtilDate.getFormatDateStr(date.getTime(),"yyyy-MM-dd HH:mm");685 if(today){686 String regex = " ";687 String[] split =todayTime.split(regex);688 return split[1];689 }690 return todayTime.substring(11);691 }