日期操作工具类
1 import com.google.common.collect.Lists; 2 import org.apache.commons.lang3.StringUtils; 3 4 import java.sql.Timestamp; 5 import java.text.ParseException; 6 import java.text.SimpleDateFormat; 7 import java.util.*; 8 9 public class DateUtils { 10 11 /** 12 * Default locale is CHINA 13 */ 14 public static final Locale DEFAULT_LOCALE = Locale.CHINA; 15 16 public static final String FORMAT_DATE_DEFAULT = "yyyy-MM-dd"; 17 18 public static final String FORMAT_DATE_YYYYMMDD = "yyyyMMdd"; 19 20 public static final String FORMAT_DATE_YYYY_MM_DD = "yyyy-MM-dd"; 21 22 public static final String FORMAT_DATE_PATTERN_1 = "yyyy/MM/dd"; 23 24 public static final String FORMAT_DATE_PATTERN_2 = "yyyy/M/dd"; 25 26 public static final String FORMAT_DATE_PATTERN_3 = "yyyy/MM/d"; 27 28 public static final String FORMAT_DATE_PATTERN_4 = "yyyy/M/d"; 29 30 public static final String FORMAT_DATE_YYYY_MM_DD_HHMMSS = "yyyyMMddHHmmss"; 31 32 public static final String FORMAT_DATE_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; 33 34 public static final String FORMAT_DATE_YYYY_MM_DD_HH_MM_SS_1 = "yyyy/MM/dd HH:mm:ss"; 35 36 public static final String FORMAT_DATE_YYYY_MM_DD_HH_MM_SS_FFF = "yyyy-MM-dd HH:mm:ss FFF"; 37 38 public static final String FORMAT_DATE_MM_DD_HH_MM_SS = "MM-dd HH:mm:ss"; 39 40 public static final String FORMAT_DATE_MM_DD_HH_MM = "MM-dd HH:mm"; 41 42 public static final String FORMAT_DATE_YYYY_MM_DD_HHMM = "yyyy-MM-dd HHmm"; 43 44 public static final String FORMAT_DATE_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm"; 45 46 public static final String FORMAT_DATE_HH_MM = "HH:mm"; 47 48 public static final String FORMAT_DATE_HH_MM_SS = "HH:mm:ss"; 49 50 public static final String FORMAT_DATE_HHMM = "HHmm"; 51 52 public static final String FORMAT_DATE_HHMMSS = "HHmmss"; 53 54 public static final String FORMAT_DATE_YYMMDD = "yyMMdd"; 55 56 public static final String FORMAT_WORK_TIME = "yyyy-MM-dd HHmmss"; 57 58 public static final String FORMAT_DATE_YYYY_MM_DDHHMMSS = "yyyy-MM-ddHHmmss"; 59 60 public static final String FORMAT_DATE_YYYYMMDD_BLANK_HHMMSS = "yyyyMMdd HH:mm:ss"; 61 62 public static final String FORMAT_DATE_YYMMDDHH = "yyMMddHH"; 63 public static final String FORMAT_DATE_YYYYMMDDHH = "yyyyMMddHH"; 64 65 public static final String FORMAT_DATE_YYYY_MM_DD_HH = "yyyy-MM-dd HH:00:00"; 66 67 public static final String FORMAT_DATE_YYYYMMDD_DOTS = "yyyy.MM.dd"; 68 69 public static final String FORMAT_DATE_YYYY_MM_DDTHH_MM_SS_FFFZ = "yyyy-MM-dd\'T\'HH:mm:ss.SSS+0800"; 70 71 public static final String DATE_END = " 23:59:59"; 72 73 /** 74 * 获取月份 75 */ 76 public static final String FORMAT_DATE_MM = "MM"; 77 78 /** 79 * 获取年份 80 */ 81 public static final String FORMAT_DATE_YYYY = "yyyy"; 82 83 public static final String FORMAT_DATE_YYYY_MM = "yyyy-MM"; 84 85 public static final String DATEYYYYMMREG = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})"; 86 87 /** 88 * Compares two Dates from their string value. 89 * 90 * @param stringValue1 Date 1 as string value. 91 * @param stringValue2 Date 2 as string value. 92 * @return the value <code>0</code> if the argument stringValue1 is equal to 93 * stringValue2; a value less than <code>0</code> if this stringValue1 94 * is before the stringValue2 as Date; and a value greater than 95 * <code>0</code> if this stringValue1 is after the stringValue2. 96 * @since 1.2 97 */ 98 public static final int compareDate(String stringValue1, String stringValue2) throws ParseException { 99 Date date1 = tryParse(stringValue1); 100 if (date1 == null) 101 throw new ParseException("Can not parse " + stringValue1 + " to Date.", 0); 102 Date date2 = tryParse(stringValue2); 103 if (date2 == null) 104 throw new ParseException("Can not parse " + stringValue1 + " to Date.", 0); 105 return date1.compareTo(date2); 106 } 107 108 /* 109 * 给定时间于当期时间的比较,差值为分钟 110 */ 111 public static final long compareDateToMin(String stringValue1) throws ParseException { 112 113 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 114 java.util.Date now = Calendar.getInstance().getTime(); 115 java.util.Date date = df.parse(stringValue1); 116 long l = now.getTime() - date.getTime(); 117 118 return l / (60 * 1000); 119 120 } 121 122 /** 123 * Returns current system date as formatted string value with default format 124 * pattern. 125 * 126 * @return current system date. 127 * @see #FORMAT_DATE_DEFAULT 128 */ 129 public static final String getCurrentDateAsString() { 130 return getCurrentDateAsString(FORMAT_DATE_DEFAULT); 131 } 132 133 /** 134 * Returns current system date as formatted string value with given format 135 * pattern. 136 * 137 * @param formatPattern format pattern. 138 * @return current system date. 139 */ 140 public static final String getCurrentDateAsString(String formatPattern) { 141 Date date = getCurrentDate(); 142 return format(date, formatPattern); 143 } 144 145 /** 146 * Returns current system date. 147 * 148 * @return current system date. 149 */ 150 public static final Date getCurrentDate() { 151 return Calendar.getInstance().getTime(); 152 } 153 154 public static final Date getCurrentDateZero() { 155 Date now = getCurrentDate(); 156 try { 157 SimpleDateFormat format = new SimpleDateFormat(FORMAT_DATE_YYYY_MM_DD); 158 return format.parse(format.format(now)); 159 } catch (ParseException e) { 160 return now; 161 } 162 } 163 164 /** 165 * @return 166 */ 167 public static final Timestamp getCurrentTimestamp() { 168 return new Timestamp(Calendar.getInstance().getTimeInMillis()); 169 } 170 171 /** 172 * Format Date value as string value with default format pattern. 173 * 174 * @param date Date value. 175 * @return formatted date as string value. 176 * @see #FORMAT_DATE_DEFAULT 177 */ 178 public static final String format(Date date) { 179 if (date == null) { 180 return ""; 181 } 182 return format(date, FORMAT_DATE_DEFAULT); 183 } 184 185 /** 186 * Format Date value as string value with default format pattern. 187 * 188 * @param date Date value. 189 * @return formatted date as string value. 190 * @see #FORMAT_DATE_DEFAULT 191 */ 192 public static final String formatDateTime(Date date) { 193 if (date == null) { 194 return ""; 195 } 196 return format(date, FORMAT_DATE_YYYY_MM_DD_HH_MM_SS); 197 } 198 199 /** 200 * Format Date value as string value with default format pattern. 201 * 202 * @param date Date value. 203 * @return formatted date as string value. 204 * @see #FORMAT_DATE_DEFAULT 205 */ 206 public static final String formatTimestamp(Date date) { 207 if (date == null) { 208 return ""; 209 } 210 return format(date, "yyyy-MM-dd HH:mm:ss.SSS"); 211 } 212 213 /** 214 * Format Date value as string value with default format pattern. 215 * 216 * @param date Date value. 217 * @return formatted date as string value. 218 * @see #FORMAT_DATE_DEFAULT 219 */ 220 public static final Date parseTimestamp(String date) { 221 if (date == null) { 222 return null; 223 } 224 return parse(date, "yyyy-MM-dd HH:mm:ss.SSS"); 225 } 226 227 /** 228 * Format Date value as string value with given format pattern. 229 * 230 * @param date Date value. 231 * @param formatPattern format pattern. 232 * @return formatted date as string value. 233 * @see #FORMAT_DATE_DEFAULT 234 * @see #FORMAT_DATE_YYYY_MM_DD 235 * @see #FORMAT_DATE_YYYY_MM_DD_HH_MM 236 * @see #FORMAT_DATE_YYYY_MM_DD_HH_MM_SS 237 * @see #FORMAT_DATE_YYYY_MM_DD_HHMMSS 238 */ 239 public static final String format(Date date, String formatPattern) { 240 if (date == null) { 241 return ""; 242 } 243 return new SimpleDateFormat(formatPattern).format(date); 244 } 245 246 /** 247 * @param date 248 * @return 249 */ 250 public static final String formatTimestamp(Timestamp date) { 251 return format(date, FORMAT_DATE_YYYY_MM_DD_HH_MM_SS); 252 } 253 254 /** 255 * Parse string value to Date with default format pattern. 256 * 257 * @param stringValue date value as string. 258 * @return Date represents stringValue. 259 * @see #FORMAT_DATE_DEFAULT 260 */ 261 public static final Date parse(String stringValue) { 262 return parse(stringValue, FORMAT_DATE_DEFAULT); 263 } 264 265 /** 266 * Parse string value to Date with given format pattern. 267 * 268 * @param stringValue date value as string. 269 * @param formatPattern format pattern. 270 * @return Date represents stringValue, null while parse exception occurred. 271 * @see #FORMAT_DATE_DEFAULT 272 */ 273 public static final Date parse(String stringValue, String formatPattern) { 274 SimpleDateFormat format = new SimpleDateFormat(formatPattern); 275 try { 276 return format.parse(stringValue); 277 } catch (ParseException e) { 278 throw new RuntimeException(e); 279 } 280 } 281 282 /** 283 * @param stringValue 284 * @param format 285 * @return 286 */ 287 public static final boolean isDate(String stringValue, String formatPattern) { 288 try { 289 if (StringUtils.isEmpty(stringValue) || StringUtils.isEmpty(formatPattern)) { 290 return false; 291 } 292 293 SimpleDateFormat format = new SimpleDateFormat(formatPattern); 294 return null != format.parse(stringValue); 295 } catch (ParseException e) { 296 return false; 297 } 298 } 299 300 public static final boolean isDateStrictWay(String stringValue, String formatPattern) { 301 try { 302 if (StringUtils.isEmpty(stringValue) || StringUtils.isEmpty(formatPattern)) { 303 return false; 304 } 305 SimpleDateFormat format = new SimpleDateFormat(formatPattern); 306 format.setLenient(false); 307 return null != format.parse(stringValue); 308 } catch (ParseException e) { 309 return false; 310 } 311 } 312 313 public static final Date parseDate(String stringValue, String formatPattern) { 314 if (StringUtils.isEmpty(stringValue)) { 315 return null; 316 } 317 return parse(stringValue, formatPattern); 318 } 319 320 /** 321 * Try to parse string value to date. 322 * 323 * @param stringValue string value. 324 * @return Date represents stringValue, null while parse exception occurred. 325 */ 326 public static final Date tryParse(String stringValue) { 327 Date date = parse(stringValue, FORMAT_DATE_YYYY_MM_DD); 328 if (date != null) { 329 return date; 330 } 331 date = parse(stringValue, FORMAT_DATE_YYYYMMDD); 332 if (date != null) { 333 return date; 334 } 335 date = parse(stringValue, FORMAT_DATE_YYYY_MM_DD_HHMMSS); 336 if (date != null) { 337 return date; 338 } 339 date = parse(stringValue, FORMAT_DATE_YYYY_MM_DD_HH_MM_SS); 340 if (date != null) { 341 return date; 342 } 343 date = parse(stringValue, FORMAT_DATE_YYYY_MM_DD_HHMM); 344 if (date != null) { 345 return date; 346 } 347 date = parse(stringValue, FORMAT_DATE_PATTERN_1); 348 if (date != null) { 349 return date; 350 } 351 date = parse(stringValue, FORMAT_DATE_PATTERN_2); 352 if (date != null) { 353 return date; 354 } 355 date = parse(stringValue, FORMAT_DATE_PATTERN_3); 356 if (date != null) { 357 return date; 358 } 359 date = parse(stringValue, FORMAT_DATE_PATTERN_4); 360 if (date != null) { 361 return date; 362 } 363 return date; 364 } 365 366 /** 367 * get day of week 368 * 369 * @param SUN_FST_DAY_OF_WEEK 370 * @return 371 */ 372 public static int getDayOfWeek(int SUN_FST_DAY_OF_WEEK) { 373 if (SUN_FST_DAY_OF_WEEK > 7 || SUN_FST_DAY_OF_WEEK < 1) 374 return 0; 375 if (SUN_FST_DAY_OF_WEEK == 1) 376 return 7; 377 return SUN_FST_DAY_OF_WEEK - 1; 378 } 379 380 public static Timestamp parseTimestamp(String stringValue, String formatPattern) { 381 if (StringUtils.isEmpty(stringValue)) { 382 return null; 383 } 384 return new Timestamp(parse(stringValue, formatPattern).getTime()); 385 } 386 387 public static Timestamp parseTimestamp(Date d, String formatPattern) { 388 if (d == null) { 389 return null; 390 } 391 return parseTimestamp(format(d, formatPattern), formatPattern); 392 } 393 394 public static Timestamp parseTimestamp(Date d) { 395 return new Timestamp(d.getTime()); 396 } 397 398 // ----------------------------------------------------------------------- 399 400 /** 401 * Adds a number of milliseconds to a date returning a new object. The original 402 * date object is unchanged. 403 * 404 * @param date the date, not null 405 * @param amount the amount to add, may be negative 406 * @return the new date object with the amount added 407 * @throws IllegalArgumentException if the date is null 408 */ 409 public static Date addMilliseconds(Date date, int amount) { 410 return add(date, Calendar.MILLISECOND, amount); 411 } 412 413 /** 414 * Adds a number of milliseconds to a date returning a new object. The original 415 * date object is unchanged. 416 * 417 * @param date the date, not null 418 * @param amount the amount to add, may be negative 419 * @return the new date object with the amount added 420 * @throws IllegalArgumentException if the date is null 421 */ 422 public static Date addMilliseconds(Date date, long amount) { 423 if (date == null) { 424 throw new IllegalArgumentException("The date must not be null"); 425 } 426 Calendar c = Calendar.getInstance(); 427 c.setTimeInMillis(date.getTime() + amount); 428 return c.getTime(); 429 } 430 431 // ----------------------------------------------------------------------- 432 433 /** 434 * Adds a number of minutes to a date returning a new object. The original date 435 * object is unchanged. 436 * 437 * @param date the date, not null 438 * @param amount the amount to add, may be negative 439 * @return the new date object with the amount added 440 * @throws IllegalArgumentException if the date is null 441 */ 442 public static Date addMinutes(Date date, int amount) { 443 return add(date, Calendar.MINUTE, amount); 444 } 445 446 // ----------------------------------------------------------------------- 447 448 /** 449 * Adds to a date returning a new object. The original date object is unchanged. 450 * 451 * @param date the date, not null 452 * @param calendarField the calendar field to add to 453 * @param amount the amount to add, may be negative 454 * @return the new date object with the amount added 455 * @throws IllegalArgumentException if the date is null 456 */ 457 public static Date add(Date date, int calendarField, int amount) { 458 if (date == null) { 459 throw new IllegalArgumentException("The date must not be null"); 460 } 461 Calendar c = Calendar.getInstance(); 462 c.setTime(date); 463 c.add(calendarField, amount); 464 return c.getTime(); 465 } 466 467 /** 468 * <p> 469 * Checks if two date objects are on the same day ignoring time. 470 * </p> 471 * 472 * <p> 473 * 28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true. 28 Mar 2002 13:45 474 * and 12 Mar 2002 13:45 would return false. 475 * </p> 476 * 477 * @param date1 the first date, not altered, not null 478 * @param date2 the second date, not altered, not null 479 * @return true if they represent the same day 480 * @throws IllegalArgumentException if either date is <code>null</code> 481 * @since 2.1 482 */ 483 public static boolean isSameDay(Date date1, Date date2) { 484 if (date1 == null || date2 == null) { 485 throw new IllegalArgumentException("The date must not be null"); 486 } 487 Calendar cal1 = Calendar.getInstance(); 488 cal1.setTime(date1); 489 Calendar cal2 = Calendar.getInstance(); 490 cal2.setTime(date2); 491 return isSameDay(cal1, cal2); 492 } 493 494 /** 495 * <p> 496 * Checks if two calendar objects are on the same day ignoring time. 497 * </p> 498 * 499 * <p> 500 * 28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true. 28 Mar 2002 13:45 501 * and 12 Mar 2002 13:45 would return false. 502 * </p> 503 * 504 * @param cal1 the first calendar, not altered, not null 505 * @param cal2 the second calendar, not altered, not null 506 * @return true if they represent the same day 507 * @throws IllegalArgumentException if either calendar is <code>null</code> 508 * @since 2.1 509 */ 510 public static boolean isSameDay(Calendar cal1, Calendar cal2) { 511 if (cal1 == null || cal2 == null) { 512 throw new IllegalArgumentException("The date must not be null"); 513 } 514 return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) 515 && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)); 516 } 517 518 /** 519 * @param date 520 * @return Date 521 * @Title: getFirstDayOfWeek 522 * @Description: 取得指定日期所在周的第一天 523 * @author yangzhi 524 */ 525 public static Date getFirstDayOfWeek(Date date) { 526 Calendar c = new GregorianCalendar(); 527 c.setFirstDayOfWeek(Calendar.MONDAY); 528 c.setTime(date); 529 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday 530 return c.getTime(); 531 } 532 533 /** 534 * @param currentDate 535 * @param week 536 * @return Date 537 * @Title: addWeekDayOfWeek 538 * @Description: 在指定的日期上+指定的周 539 * @author yangzhi 540 */ 541 public static Date addWeekDayOfWeek(Date currentDate, int week) { 542 Calendar c = new GregorianCalendar(); 543 c.setTime(currentDate); 544 c.add(Calendar.WEEK_OF_YEAR, week); 545 return c.getTime(); 546 } 547 548 /** 549 * @param startDate 550 * @param endDate 551 * @return 552 * @Description 获取两个时间间隔,单位ms 553 */ 554 public static long getDateInterval(String startDate, String endDate) { 555 Date startTime = parse(startDate, DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS); 556 Date endTime = parse(endDate, DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS); 557 return endTime.getTime() - startTime.getTime(); 558 } 559 560 /** 561 * 判断两个时间在一个间隔内 562 * 563 * @param startTime 564 * @param endTime 565 * @param interval 566 * @return 567 */ 568 public static boolean isTwoDateInInterval(Date startTime, Date endTime, int interval) { 569 if (null == startTime || null == endTime) { 570 return false; 571 } 572 573 Calendar cal = Calendar.getInstance(); 574 cal.setTime(startTime); 575 cal.add(Calendar.DAY_OF_YEAR, interval); 576 return cal.getTime().after(endTime); 577 } 578 579 /** 580 * 获取两个时间段内的所有时间集合 581 * 582 * @param startDate 583 * @param endDate 584 * @return 585 */ 586 public static List<Date> getDatesBetweenTwoDate(String startDate, String endDate) { 587 Date startTime = parse(startDate, DateUtils.FORMAT_DATE_DEFAULT); 588 Date endTime = parse(endDate, DateUtils.FORMAT_DATE_DEFAULT); 589 List<Date> resultlist = new ArrayList<>(); 590 resultlist.add(startTime); // 把开始时间加入集合 591 Calendar cal = Calendar.getInstance(); 592 cal.setTime(startTime); 593 while (true) { 594 cal.add(Calendar.DAY_OF_MONTH, 1); 595 if (endTime.after(cal.getTime())) { 596 resultlist.add(cal.getTime()); 597 } else { 598 break; 599 } 600 } 601 resultlist.add(endTime); 602 return resultlist; 603 } 604 605 /** 606 * 将指定格式时间转换为另一种格式 607 * 608 * @param sourceDate 609 * @param sourceFormat 610 * @param targetFormat 611 * @return 612 */ 613 public static String formatDate(String sourceDate, String sourceFormat, String targetFormat) { 614 try { 615 return DateUtils.format(DateUtils.parse(sourceDate, sourceFormat), targetFormat); 616 } catch (Exception e) { 617 return null; 618 } 619 } 620 621 /** 622 * 获取指定日期星期天 623 * 624 * @param sourceDate 625 * @param format 626 * @return 627 */ 628 public static String formatToSundayDate(String sourceDate, String format) { 629 Calendar cal = Calendar.getInstance(); 630 cal.setTime(parse(sourceDate, format)); 631 cal.set(Calendar.DAY_OF_WEEK, 1); 632 return format(cal.getTime(), format); 633 } 634 635 /** 636 * 获取指定日期星期天和前N个星期天 637 * 638 * @param updateTime 639 * @param format 640 * @param size 641 * @return 642 */ 643 public static List<String> listSundayDate(long updateTime, String format, int size) { 644 List<String> sundayDates = Lists.newArrayList(); 645 Calendar cal = Calendar.getInstance(); 646 cal.setTimeInMillis(updateTime); 647 cal.set(Calendar.DAY_OF_WEEK, 1); 648 sundayDates.add(format(cal.getTime(), format)); 649 while (size-- > 0) { 650 cal.add(Calendar.DATE, -7); 651 sundayDates.add(format(cal.getTime(), format)); 652 } 653 return sundayDates; 654 } 655 656 /** 657 * 秒转换为 x时x分x秒 显示 658 * 659 * @param milliSeconds 660 * @return 661 */ 662 public static String formatMilliSeconds(int milliSeconds) { 663 int seconds = milliSeconds / 1000; 664 int second = seconds % 60; 665 int minute = (seconds / 60) % 60; 666 int hour = (seconds / (60 * 60)) % 60; 667 668 StringBuilder formatTime = new StringBuilder(); 669 if (hour > 0) { 670 formatTime.append(hour).append(" 时 "); 671 formatTime.append(minute).append(" 分 "); 672 } else if (minute > 0) { 673 formatTime.append(minute).append(" 分 "); 674 } 675 formatTime.append(second).append(" 秒"); 676 return formatTime.toString(); 677 } 678 679 /** 680 * 时间是否已经过去 681 * 682 * @param stringValue 683 * @param formatPattern 684 * @return 685 */ 686 public static boolean isOvertime(String stringValue, String formatPattern) { 687 if (StringUtils.isBlank(stringValue)) { 688 return false; 689 } 690 691 try { 692 return DateUtils.parse(stringValue, formatPattern).before(DateUtils.getCurrentDate()); 693 } catch (Exception e) { 694 return false; 695 } 696 } 697 698 public static Timestamp parseYYYYMMDD(String stringValue, String formatPattern) { 699 stringValue = stringValue.replaceAll(DATEYYYYMMREG, "$1-$2-$3 $4:$5:$6"); 700 if (StringUtils.isEmpty(stringValue)) { 701 return null; 702 } 703 return new Timestamp(parse(stringValue, formatPattern).getTime()); 704 } 705 706 707 /** 708 * @param beginDateStr 制定开始日期 709 * @param format 转换格式 710 * @description: 制定日期到当前的所有间隔日期 711 * @author: ChenYao(https : / / blog.csdn.net / chenyao1994) 712 * @return: java.util.List<java.lang.String> 713 * @CeateDate: 2020/6/16 16:22 714 */ 715 public static List<String> findIntervalDates(String beginDateStr, String format) { 716 List<String> intervalDateList = new ArrayList<>(); 717 //intervalDateList.add(beginDateStr); 718 Calendar calBegin = Calendar.getInstance(); 719 // 使用给定的 Date 设置此 Calendar 的时间 720 calBegin.setTime(DateUtils.parse(beginDateStr, format)); 721 Calendar calEnd = Calendar.getInstance(); 722 // 使用给定的 Date 设置此 Calendar 的时间 723 Date endDate = new Date(); 724 calEnd.setTime(endDate); 725 // 测试此日期是否在指定日期之后 726 while (endDate.after(calBegin.getTime())) { 727 // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 728 calBegin.add(Calendar.HOUR_OF_DAY, 1); 729 intervalDateList.add(DateUtils.format(calBegin.getTime(), format)); 730 } 731 if (!intervalDateList.isEmpty()) { 732 intervalDateList.remove(intervalDateList.size() - 1); 733 } 734 return intervalDateList; 735 } 736 }
本文来自博客园,作者:风光小磊,转载请注明原文链接:https://www.cnblogs.com/lei-z/p/13689890.html