时间操作定义类
1 package com.zhouyy.netBank.util; 2 3 import org.springframework.util.StringUtils; 4 5 import java.beans.PropertyEditorSupport; 6 import java.sql.Timestamp; 7 import java.text.DateFormat; 8 import java.text.ParseException; 9 import java.text.SimpleDateFormat; 10 import java.util.*; 11 12 /** 13 * 类描述:时间操作定义类 14 * 15 */ 16 public class DateUtils extends PropertyEditorSupport { 17 // 各种时间格式 18 public static final SimpleDateFormat date_sdf = new SimpleDateFormat("yyyy-MM-dd"); 19 // 各种时间格式 20 public static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd"); 21 // 各种时间格式 22 public static final SimpleDateFormat date_sdf_wz = new SimpleDateFormat("yyyy年MM月dd日"); 23 public static final SimpleDateFormat time_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 24 public static final SimpleDateFormat yyyymmddhhmmss = new SimpleDateFormat("yyyyMMddHHmmss"); 25 public static final SimpleDateFormat short_time_sdf = new SimpleDateFormat("HH:mm"); 26 public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 27 // 以毫秒表示的时间 28 private static final long DAY_IN_MILLIS = 24 * 3600 * 1000; 29 private static final long HOUR_IN_MILLIS = 3600 * 1000; 30 private static final long MINUTE_IN_MILLIS = 60 * 1000; 31 private static final long SECOND_IN_MILLIS = 1000; 32 33 // Grace style 34 // public static final String PATTERN_GRACE = "yyyy/MM/dd HH:mm:ss"; 35 // public static final String PATTERN_GRACE_NORMAL = "yyyy/MM/dd HH:mm"; 36 // public static final String PATTERN_GRACE_SIMPLE = "yyyy/MM/dd"; 37 38 // Classical style 39 public static final String PATTERN_CLASSICAL = "yyyy-MM-dd HH:mm:ss"; 40 public static final String PATTERN_CLASSICAL_NORMAL = "yyyy-MM-dd HH:mm"; 41 public static final String PATTERN_CLASSICAL_SIMPLE = "yyyy-MM-dd"; 42 43 //CH style 44 public static final String PATTERN_CH = "yyyy年MM月dd日 HH时mm分ss秒"; 45 public static final String PATTERN_CH_NORMAL = "yyyy年MM月dd日 HH时mm分"; 46 public static final String PATTERN_CH_SIMPLE = "yyyy年MM月dd日"; 47 48 // 指定模式的时间格式 49 private static SimpleDateFormat getSDFormat(String pattern) { 50 return new SimpleDateFormat(pattern); 51 } 52 53 /** 54 * 当前日历,这里用中国时间表示 55 * 56 * @return 以当地时区表示的系统当前日历 57 */ 58 public static Calendar getCalendar() { 59 return Calendar.getInstance(); 60 } 61 62 /** 63 * 指定毫秒数表示的日历 64 * 65 * @param millis 毫秒数 66 * @return 指定毫秒数表示的日历 67 */ 68 public static Calendar getCalendar(long millis) { 69 Calendar cal = Calendar.getInstance(); 70 cal.setTime(new Date(millis)); 71 return cal; 72 } 73 74 // //////////////////////////////////////////////////////////////////////////// 75 // getDate 76 // 各种方式获取的Date 77 // //////////////////////////////////////////////////////////////////////////// 78 79 /** 80 * 当前日期 81 * 82 * @return 系统当前时间 83 */ 84 public static Date getDate() { 85 return new Date(); 86 } 87 88 /** 89 * 当前日期 不带时分秒 90 * 91 * @return 系统当前时间 92 */ 93 public static Date getDateYMD() { 94 return DateUtils.str2Date(DateUtils.date2Str(DateUtils.yyyyMMdd), DateUtils.yyyyMMdd); 95 } 96 97 /** 98 * 获取日期 不带时分秒 99 * 100 * @return 系统当前时间 101 */ 102 public static Date getDateYMD(Date date) { 103 return DateUtils.str2Date(DateUtils.date2Str(date, DateUtils.yyyyMMdd), DateUtils.yyyyMMdd); 104 } 105 106 /** 107 * 指定毫秒数表示的日期 108 * 109 * @param millis 毫秒数 110 * @return 指定毫秒数表示的日期 111 */ 112 public static Date getDate(long millis) { 113 return new Date(millis); 114 } 115 116 /** 117 * 时间戳转换为字符串 118 * 119 * @param time 120 * @return 121 */ 122 public static String timestamptoStr(Timestamp time) { 123 Date date = null; 124 if (null != time) { 125 date = new Date(time.getTime()); 126 } 127 return date2Str(date_sdf); 128 } 129 130 /** 131 * 字符串转换时间戳 132 * 133 * @param str 134 * @return 135 */ 136 public static Timestamp str2Timestamp(String str) { 137 Date date = str2Date(str, date_sdf); 138 return new Timestamp(date.getTime()); 139 } 140 141 /** 142 * 字符串转换成日期 143 * 144 * @param str 145 * @param sdf 146 * @return 147 */ 148 public static Date str2Date(String str, SimpleDateFormat sdf) { 149 if (null == str || "".equals(str)) { 150 return null; 151 } 152 Date date = null; 153 try { 154 date = sdf.parse(str); 155 return date; 156 } catch (ParseException e) { 157 e.printStackTrace(); 158 } 159 return null; 160 } 161 162 /** 163 * 日期转换为字符串 164 * 165 * @param date 日期 166 * @param format 日期格式 167 * @return 字符串 168 */ 169 public static String date2Str(SimpleDateFormat date_sdf) { 170 Date date = getDate(); 171 if (null == date) { 172 return null; 173 } 174 return date_sdf.format(date); 175 } 176 177 /** 178 * 格式化时间 179 * 180 * @param date 181 * @param format 182 * @return 183 */ 184 public static String dateformat(String date, String format) { 185 SimpleDateFormat sformat = new SimpleDateFormat(format); 186 Date _date = null; 187 try { 188 _date = sformat.parse(date); 189 } catch (ParseException e) { 190 // TODO Auto-generated catch block 191 e.printStackTrace(); 192 } 193 return sformat.format(_date); 194 } 195 196 /** 197 * 日期转换为字符串 198 * 199 * @param date 日期 200 * @param format 日期格式 201 * @return 字符串 202 */ 203 public static String date2StrCNY(Date date) { 204 if (null == date) { 205 return null; 206 } 207 return date2Str(date,date_sdf_wz); 208 } 209 210 /** 211 * 日期转换为字符串 212 * 213 * @param date 日期 214 * @param format 日期格式 215 * @return 字符串 216 */ 217 public static String date2Str(Date date, SimpleDateFormat date_sdf) { 218 if (null == date) { 219 return null; 220 } 221 return date_sdf.format(date); 222 } 223 224 /** 225 * 日期转换为字符串 226 * 227 * @param date 日期 228 * @param format 日期格式 229 * @return 字符串 230 */ 231 public static String getDate(String format) { 232 Date date = new Date(); 233 if (null == date) { 234 return null; 235 } 236 SimpleDateFormat sdf = new SimpleDateFormat(format); 237 return sdf.format(date); 238 } 239 240 /** 241 * 指定毫秒数的时间戳 242 * 243 * @param millis 毫秒数 244 * @return 指定毫秒数的时间戳 245 */ 246 public static Timestamp getTimestamp(long millis) { 247 return new Timestamp(millis); 248 } 249 250 /** 251 * 以字符形式表示的时间戳 252 * 253 * @param time 毫秒数 254 * @return 以字符形式表示的时间戳 255 */ 256 public static Timestamp getTimestamp(String time) { 257 return new Timestamp(Long.parseLong(time)); 258 } 259 260 /** 261 * 系统当前的时间戳 262 * 263 * @return 系统当前的时间戳 264 */ 265 public static Timestamp getTimestamp() { 266 return new Timestamp(new Date().getTime()); 267 } 268 269 /** 270 * 指定日期的时间戳 271 * 272 * @param date 指定日期 273 * @return 指定日期的时间戳 274 */ 275 public static Timestamp getTimestamp(Date date) { 276 return new Timestamp(date.getTime()); 277 } 278 279 /** 280 * 指定日历的时间戳 281 * 282 * @param cal 指定日历 283 * @return 指定日历的时间戳 284 */ 285 public static Timestamp getCalendarTimestamp(Calendar cal) { 286 return new Timestamp(cal.getTime().getTime()); 287 } 288 289 public static Timestamp gettimestamp() { 290 Date dt = new Date(); 291 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 292 String nowTime = df.format(dt); 293 java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime); 294 return buydate; 295 } 296 297 // //////////////////////////////////////////////////////////////////////////// 298 // getMillis 299 // 各种方式获取的Millis 300 // //////////////////////////////////////////////////////////////////////////// 301 302 /** 303 * 系统时间的毫秒数 304 * 305 * @return 系统时间的毫秒数 306 */ 307 public static long getMillis() { 308 return new Date().getTime(); 309 } 310 311 /** 312 * 指定日历的毫秒数 313 * 314 * @param cal 指定日历 315 * @return 指定日历的毫秒数 316 */ 317 public static long getMillis(Calendar cal) { 318 return cal.getTime().getTime(); 319 } 320 321 /** 322 * 指定日期的毫秒数 323 * 324 * @param date 指定日期 325 * @return 指定日期的毫秒数 326 */ 327 public static long getMillis(Date date) { 328 return date.getTime(); 329 } 330 331 /** 332 * 指定时间戳的毫秒数 333 * 334 * @param ts 指定时间戳 335 * @return 指定时间戳的毫秒数 336 */ 337 public static long getMillis(Timestamp ts) { 338 return ts.getTime(); 339 } 340 341 // //////////////////////////////////////////////////////////////////////////// 342 // formatDate 343 // 将日期按照一定的格式转化为字符串 344 // //////////////////////////////////////////////////////////////////////////// 345 346 /** 347 * 默认方式表示的系统当前日期,具体格式:年-月-日 348 * 349 * @return 默认日期按“年-月-日“格式显示 350 */ 351 public static String formatDate() { 352 return date_sdf.format(getCalendar().getTime()); 353 } 354 355 /** 356 * 默认方式表示的系统当前日期,具体格式:yyyy-MM-dd HH:mm:ss 357 * 358 * @return 默认日期按“yyyy-MM-dd HH:mm:ss“格式显示 359 */ 360 public static String formatDateTime() { 361 return datetimeFormat.format(getCalendar().getTime()); 362 } 363 364 /** 365 * 获取时间字符串 366 */ 367 public static String getDataString(SimpleDateFormat formatstr) { 368 return formatstr.format(getCalendar().getTime()); 369 } 370 371 /** 372 * 指定日期的默认显示,具体格式:年-月-日 373 * 374 * @param cal 指定的日期 375 * @return 指定日期按“年-月-日“格式显示 376 */ 377 public static String formatDate(Calendar cal) { 378 return date_sdf.format(cal.getTime()); 379 } 380 381 /** 382 * 指定日期的默认显示,具体格式:年-月-日 383 * 384 * @param date 指定的日期 385 * @return 指定日期按“年-月-日“格式显示 386 */ 387 public static String formatDate(Date date) { 388 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 389 return sdf.format(date); 390 } 391 392 /** 393 * 指定毫秒数表示日期的默认显示,具体格式:年-月-日 394 * 395 * @param millis 指定的毫秒数 396 * @return 指定毫秒数表示日期按“年-月-日“格式显示 397 */ 398 public static String formatDate(long millis) { 399 return date_sdf.format(new Date(millis)); 400 } 401 402 /** 403 * 默认日期按指定格式显示 404 * 405 * @param pattern 指定的格式 406 * @return 默认日期按指定格式显示 407 */ 408 public static String formatDate(String pattern) { 409 return getSDFormat(pattern).format(getCalendar().getTime()); 410 } 411 412 /** 413 * 指定日期按指定格式显示 414 * 415 * @param cal 指定的日期 416 * @param pattern 指定的格式 417 * @return 指定日期按指定格式显示 418 */ 419 public static String formatDate(Calendar cal, String pattern) { 420 return getSDFormat(pattern).format(cal.getTime()); 421 } 422 423 /** 424 * 指定日期按指定格式显示 425 * 426 * @param date 指定的日期 427 * @param pattern 指定的格式 428 * @return 指定日期按指定格式显示 429 */ 430 public static String formatDate(Date date, String pattern) { 431 return getSDFormat(pattern).format(date); 432 } 433 434 // //////////////////////////////////////////////////////////////////////////// 435 // formatTime 436 // 将日期按照一定的格式转化为字符串 437 // //////////////////////////////////////////////////////////////////////////// 438 439 /** 440 * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分 441 * 442 * @return 默认日期按“年-月-日 时:分“格式显示 443 */ 444 public static String formatTime() { 445 return time_sdf.format(getCalendar().getTime()); 446 } 447 448 /** 449 * 指定毫秒数表示日期的默认显示,具体格式:年-月-日 时:分 450 * 451 * @param millis 指定的毫秒数 452 * @return 指定毫秒数表示日期按“年-月-日 时:分“格式显示 453 */ 454 public static String formatTime(long millis) { 455 return time_sdf.format(new Date(millis)); 456 } 457 458 /** 459 * 指定日期的默认显示,具体格式:年-月-日 时:分 460 * 461 * @param cal 指定的日期 462 * @return 指定日期按“年-月-日 时:分“格式显示 463 */ 464 public static String formatTime(Calendar cal) { 465 return time_sdf.format(cal.getTime()); 466 } 467 468 /** 469 * 指定日期的默认显示,具体格式:年-月-日 时:分 470 * 471 * @param date 指定的日期 472 * @return 指定日期按“年-月-日 时:分“格式显示 473 */ 474 public static String formatTime(Date date) { 475 return time_sdf.format(date); 476 } 477 478 // //////////////////////////////////////////////////////////////////////////// 479 // formatShortTime 480 // 将日期按照一定的格式转化为字符串 481 // //////////////////////////////////////////////////////////////////////////// 482 483 /** 484 * 默认方式表示的系统当前日期,具体格式:时:分 485 * 486 * @return 默认日期按“时:分“格式显示 487 */ 488 public static String formatShortTime() { 489 return short_time_sdf.format(getCalendar().getTime()); 490 } 491 492 /** 493 * 指定毫秒数表示日期的默认显示,具体格式:时:分 494 * 495 * @param millis 指定的毫秒数 496 * @return 指定毫秒数表示日期按“时:分“格式显示 497 */ 498 public static String formatShortTime(long millis) { 499 return short_time_sdf.format(new Date(millis)); 500 } 501 502 /** 503 * 指定日期的默认显示,具体格式:时:分 504 * 505 * @param cal 指定的日期 506 * @return 指定日期按“时:分“格式显示 507 */ 508 public static String formatShortTime(Calendar cal) { 509 return short_time_sdf.format(cal.getTime()); 510 } 511 512 /** 513 * 指定日期的默认显示,具体格式:时:分 514 * 515 * @param date 指定的日期 516 * @return 指定日期按“时:分“格式显示 517 */ 518 public static String formatShortTime(Date date) { 519 return short_time_sdf.format(date); 520 } 521 522 // //////////////////////////////////////////////////////////////////////////// 523 // parseDate 524 // parseCalendar 525 // parseTimestamp 526 // 将字符串按照一定的格式转化为日期或时间 527 // //////////////////////////////////////////////////////////////////////////// 528 529 /** 530 * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间 531 * 532 * @param src 将要转换的原始字符窜 533 * @param pattern 转换的匹配格式 534 * @return 如果转换成功则返回转换后的日期 535 * @throws ParseException 536 * @throws AIDateFormatException 537 */ 538 public static Date parseDate(String src, String pattern) 539 throws ParseException { 540 return getSDFormat(pattern).parse(src); 541 542 } 543 544 /** 545 * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间 546 * 547 * @param src 将要转换的原始字符窜 548 * @param pattern 转换的匹配格式 549 * @return 如果转换成功则返回转换后的日期 550 * @throws ParseException 551 * @throws AIDateFormatException 552 */ 553 public static Calendar parseCalendar(String src, String pattern) 554 throws ParseException { 555 556 Date date = parseDate(src, pattern); 557 Calendar cal = Calendar.getInstance(); 558 cal.setTime(date); 559 return cal; 560 } 561 562 public static String formatAddDate(String src, String pattern, int amount) 563 throws ParseException { 564 Calendar cal; 565 cal = parseCalendar(src, pattern); 566 cal.add(Calendar.DATE, amount); 567 return formatDate(cal); 568 } 569 570 /** 571 * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间 572 * 573 * @param src 将要转换的原始字符窜 574 * @param pattern 转换的匹配格式 575 * @return 如果转换成功则返回转换后的时间戳 576 * @throws ParseException 577 * @throws AIDateFormatException 578 */ 579 public static Timestamp parseTimestamp(String src, String pattern) 580 throws ParseException { 581 Date date = parseDate(src, pattern); 582 return new Timestamp(date.getTime()); 583 } 584 585 // //////////////////////////////////////////////////////////////////////////// 586 // dateDiff 587 // 计算两个日期之间的差值 588 // //////////////////////////////////////////////////////////////////////////// 589 590 /** 591 * 计算两个时间之间的差值,根据标志的不同而不同 592 * 593 * @param flag 计算标志,表示按照年/月/日/时/分/秒等计算 594 * @param calSrc 减数 595 * @param calDes 被减数 596 * @return 两个日期之间的差值 597 */ 598 public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) { 599 600 long millisDiff = getMillis(calSrc) - getMillis(calDes); 601 602 if (flag == 'y') { 603 return (calSrc.get(calSrc.YEAR) - calDes.get(calDes.YEAR)); 604 } 605 606 if (flag == 'M') { 607 return (calSrc.get(calSrc.MONTH) + 608 ( (calSrc.get(calSrc.YEAR) - calDes.get(calDes.YEAR))*12 ) - calDes.get(calDes.MONTH)); 609 } 610 611 if (flag == 'd') { 612 return (int) (millisDiff / DAY_IN_MILLIS); 613 } 614 615 if (flag == 'h') { 616 return (int) (millisDiff / HOUR_IN_MILLIS); 617 } 618 619 if (flag == 'm') { 620 return (int) (millisDiff / MINUTE_IN_MILLIS); 621 } 622 623 if (flag == 's') { 624 return (int) (millisDiff / SECOND_IN_MILLIS); 625 } 626 627 return 0; 628 } 629 630 /** 631 * String类型 转换为Date, 632 * 如果参数长度为10 转换格式”yyyy-MM-dd“ 633 * 如果参数长度为19 转换格式”yyyy-MM-dd HH:mm:ss“ 634 * * @param text 635 * String类型的时间值 636 */ 637 public void setAsText(String text) throws IllegalArgumentException { 638 if (StringUtils.hasText(text)) { 639 try { 640 if (text.indexOf(":") == -1 && text.length() == 10) { 641 setValue(this.date_sdf.parse(text)); 642 } else if (text.indexOf(":") > 0 && text.length() == 19) { 643 setValue(this.datetimeFormat.parse(text)); 644 } else { 645 throw new IllegalArgumentException( 646 "Could not parse date, date format is error "); 647 } 648 } catch (ParseException ex) { 649 IllegalArgumentException iae = new IllegalArgumentException( 650 "Could not parse date: " + ex.getMessage()); 651 iae.initCause(ex); 652 throw iae; 653 } 654 } else { 655 setValue(null); 656 } 657 } 658 659 public static int getYear() { 660 GregorianCalendar calendar = new GregorianCalendar(); 661 calendar.setTime(getDate()); 662 return calendar.get(Calendar.YEAR); 663 } 664 665 public static String getAfterMonth(int month, int day) { 666 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 667 GregorianCalendar calendar = new GregorianCalendar(); 668 calendar.add(2, month); 669 calendar.add(5, day); 670 String date = format.format(calendar.getTime()); 671 return date; 672 } 673 674 public static String getAfterDate(int day) { 675 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 676 GregorianCalendar calendar = new GregorianCalendar(); 677 calendar.add(5, day); 678 String date = format.format(calendar.getTime()); 679 return date; 680 } 681 682 public static String getAfterMinute(int minute) { 683 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 684 GregorianCalendar calendar = new GregorianCalendar(); 685 calendar.add(12, minute); 686 String date = format.format(calendar.getTime()); 687 return date; 688 } 689 690 public static String getAfterSecond(int second) { 691 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 692 GregorianCalendar calendar = new GregorianCalendar(); 693 calendar.add(13, second); 694 String date = format.format(calendar.getTime()); 695 return date; 696 } 697 698 699 public static String getDateStr(Date date, String Pattern) { 700 SimpleDateFormat formatter = new SimpleDateFormat(Pattern); 701 return formatter.format(date); 702 } 703 704 705 /** 706 * 日期的增加天数 707 * 708 * @param date 日期 709 * @param addDay 增加的天数,正数表示增加,负数表示减少 710 * @return Date 日期的增加月数后的结果 711 */ 712 public static Date addDay(Date date, int addDay) { 713 Calendar calendar = Calendar.getInstance(); 714 calendar.setTime(date); 715 calendar.add(Calendar.DAY_OF_MONTH, addDay); 716 return new Date(calendar.getTime().getTime()); 717 } 718 719 /** 720 * 将datetime转换为date类型 721 * 722 * @param date 723 * @return 724 * @author zuoy 2017-09-15 725 */ 726 public static Date parseDate(Date date){ 727 return parse(DateUtils.date2Str(date, DateUtils.date_sdf), "yyyy-MM-dd"); 728 } 729 730 /** 731 * 静态方法 根据两个日期求相差天数 正数表示d1大于d2天数 负数表示d1小于d2天数 732 * add 7.30 733 * 734 * @author zdl 735 */ 736 public static int dayDiffs(Date d1, Date d2) { 737 if (d1 == null || d2 == null) { 738 return 0; 739 } 740 long tt = d1.getTime() - d2.getTime(); 741 int days = (int) (tt / (24 * 60 * 60 * 1000)); 742 return days; 743 744 } 745 746 /** 747 * 验证日期是否是周末 748 */ 749 public static boolean checkHoliday(Date date) { 750 Calendar ca = Calendar.getInstance(); 751 ca.setTime(date); 752 //判断日期是否是周六周日 753 if (ca.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || ca.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { 754 return true; 755 } 756 return false; 757 } 758 759 /** 760 * 验证日期是否是周末 761 */ 762 public static int getDay(Date date) { 763 Calendar ca = Calendar.getInstance(); 764 ca.setTime(date); 765 return ca.get(Calendar.DAY_OF_MONTH); 766 } 767 768 /***********2017年10月24日 byzhouyy新增以下代码***********/ 769 770 /** 771 * 根据默认格式将指定字符串解析成日期 772 * 773 * @param str 指定字符串 774 * @return 返回解析后的日期 775 */ 776 public static Date parse(String str) { 777 return parse(str, PATTERN_CLASSICAL); 778 } 779 780 /** 781 * 根据指定格式将指定字符串解析成日期 782 * 783 * @param str 指定日期 784 * @param pattern 指定格式 785 * @return 返回解析后的日期 786 */ 787 public static Date parse(String str, String pattern) { 788 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 789 try { 790 return sdf.parse(str); 791 } catch (Exception e) { 792 e.printStackTrace(); 793 } 794 return null; 795 } 796 797 /** 798 * 根据默认格式将日期转格式化成字符串 799 * 800 * @param date 指定日期 801 * @return 返回格式化后的字符串 802 */ 803 public static String format(Date date) { 804 return format(date, PATTERN_CLASSICAL); 805 } 806 807 /** 808 * 根据指定格式将指定日期格式化成字符串 809 * 810 * @param date 指定日期 811 * @param pattern 指定格式 812 * @return 返回格式化后的字符串 813 */ 814 public static String format(Date date, String pattern) { 815 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 816 return sdf.format(date); 817 } 818 819 /** 820 * 获取时间date1与date2相差的秒数 821 * 822 * @param date1 起始时间 823 * @param date2 结束时间 824 * @return 返回相差的秒数 825 */ 826 public static int getOffsetSeconds(Date date1, Date date2) { 827 int seconds = (int) ((date2.getTime() - date1.getTime()) / 1000); 828 return seconds; 829 } 830 831 /** 832 * 获取时间date1与date2相差的分钟数 833 * 834 * @param date1 起始时间 835 * @param date2 结束时间 836 * @return 返回相差的分钟数 837 */ 838 public static int getOffsetMinutes(Date date1, Date date2) { 839 return getOffsetSeconds(date1, date2) / 60; 840 } 841 842 /** 843 * 获取时间date1与date2相差的小时数 844 * 845 * @param date1 起始时间 846 * @param date2 结束时间 847 * @return 返回相差的小时数 848 */ 849 public static int getOffsetHours(Date date1, Date date2) { 850 return getOffsetMinutes(date1, date2) / 60; 851 } 852 853 /** 854 * 获取时间date1与date2相差的天数数 855 * 856 * @param date1 起始时间 857 * @param date2 结束时间 858 * @return 返回相差的天数 859 */ 860 public static int getOffsetDays(Date date1, Date date2) { 861 return getOffsetHours(date1, date2) / 24; 862 } 863 864 /** 865 * 获取时间date1与date2相差的周数 866 * 867 * @param date1 起始时间 868 * @param date2 结束时间 869 * @return 返回相差的周数 870 */ 871 public static int getOffsetWeeks(Date date1, Date date2) { 872 return getOffsetDays(date1, date2) / 7; 873 } 874 875 /** 876 * 获取重置指定日期的时分秒后的时间 877 * 878 * @param date 指定日期 879 * @param hour 指定小时 880 * @param minute 指定分钟 881 * @param second 指定秒 882 * @return 返回重置时分秒后的时间 883 */ 884 public static Date getResetTime(Date date, int hour, int minute, int second) { 885 Calendar cal = Calendar.getInstance(); 886 if (date != null) { 887 cal.setTime(date); 888 } 889 cal.set(Calendar.HOUR_OF_DAY, hour); 890 cal.set(Calendar.SECOND, minute); 891 cal.set(Calendar.MINUTE, second); 892 cal.set(Calendar.MILLISECOND, 0); 893 return cal.getTime(); 894 } 895 896 /** 897 * 返回指定日期的起始时间 898 * 899 * @param date 指定日期(例如2014-08-01) 900 * @return 返回起始时间(例如2014-08-01 00:00:00) 901 */ 902 public static Date getIntegralStartTime(Date date) { 903 return getResetTime(date, 0, 0, 0); 904 } 905 906 /** 907 * 返回指定日期的结束时间 908 * 909 * @param date 指定日期(例如2014-08-01) 910 * @return 返回结束时间(例如2014-08-01 23:59:59) 911 */ 912 public static Date getIntegralEndTime(Date date) { 913 return getResetTime(date, 23, 59, 59); 914 } 915 916 /** 917 * 获取指定日期累加年月日后的时间 918 * 919 * @param date 指定日期 920 * @param year 指定年数 921 * @param month 指定月数 922 * @param day 指定天数 923 * @return 返回累加年月日后的时间 924 */ 925 public static Date rollDate(Date date, int year, int month, int day) { 926 Calendar cal = Calendar.getInstance(); 927 if (date != null) { 928 cal.setTime(date); 929 } 930 cal.add(Calendar.YEAR, year); 931 cal.add(Calendar.MONTH, month); 932 cal.add(Calendar.DAY_OF_MONTH, day); 933 return cal.getTime(); 934 } 935 936 /** 937 * 获取指定日期累加指定月数后的时间 938 * 939 * @param date 指定日期 940 * @param month 指定月数 941 * @return 返回累加月数后的时间 942 */ 943 public static Date rollMonth(Date date, int month) { 944 return rollDate(date, 0, month, 0); 945 } 946 947 /** 948 * 获取指定日期累加指定天数后的时间 949 * 950 * @param date 指定日期 951 * @param day 指定天数 952 * @return 返回累加天数后的时间 953 */ 954 public static Date rollDay(Date date, int day) { 955 return rollDate(date, 0, 0, day); 956 } 957 958 /** 959 * 计算指定日期所在月份的天数 960 * 961 * @param date 指定日期 962 * @return 返回所在月份的天数 963 */ 964 public static int getDayOfMonth(Date date) { 965 Calendar cal = Calendar.getInstance(); 966 if (date != null) { 967 cal.setTime(date); 968 } 969 int dayOfMonth = cal.getActualMaximum(Calendar.DATE); 970 return dayOfMonth; 971 } 972 973 /** 974 * 获取当月第一天的起始时间,例如2014-08-01 00:00:00 975 * 976 * @return 返回当月第一天的起始时间 977 */ 978 public static Date getMonthStartTime() { 979 Calendar cal = Calendar.getInstance(); 980 cal.set(Calendar.DAY_OF_MONTH, 1); 981 return getIntegralStartTime(cal.getTime()); 982 } 983 984 /** 985 * 获取当月最后一天的结束时间,例如2014-08-31 23:59:59 986 * 987 * @return 返回当月最后一天的结束时间 988 */ 989 public static Date getMonthEndTime() { 990 Calendar cal = Calendar.getInstance(); 991 cal.set(Calendar.DAY_OF_MONTH, getDayOfMonth(cal.getTime())); 992 return getIntegralEndTime(cal.getTime()); 993 } 994 995 /** 996 * 获取上个月第一天的起始时间,例如2014-07-01 00:00:00 997 * 998 * @return 返回上个月第一天的起始时间 999 */ 1000 public static Date getLastMonthStartTime() { 1001 Calendar cal = Calendar.getInstance(); 1002 cal.add(Calendar.MONTH, -1); 1003 cal.set(Calendar.DAY_OF_MONTH, 1); 1004 return getIntegralStartTime(cal.getTime()); 1005 } 1006 1007 /** 1008 * 获取上个月最后一天的结束时间,例如2014-07-31 23:59:59 1009 * 1010 * @return 返回上个月最后一天的结束时间 1011 */ 1012 public static Date getLastMonthEndTime() { 1013 Calendar cal = Calendar.getInstance(); 1014 cal.add(Calendar.MONTH, -1); 1015 cal.set(Calendar.DAY_OF_MONTH, getDayOfMonth(cal.getTime())); 1016 return getIntegralEndTime(cal.getTime()); 1017 } 1018 1019 /** 1020 * 获取下个月第一天的起始时间,例如2014-09-01 00:00:00 1021 * 1022 * @return 返回下个月第一天的起始时间 1023 */ 1024 public static Date getNextMonthStartTime() { 1025 Calendar cal = Calendar.getInstance(); 1026 cal.add(Calendar.MONTH, 1); 1027 cal.set(Calendar.DAY_OF_MONTH, 1); 1028 return getIntegralStartTime(cal.getTime()); 1029 } 1030 1031 /** 1032 * 获取下个月最后一天的结束时间,例如2014-09-30 23:59:59 1033 * 1034 * @return 返回下个月最后一天的结束时间 1035 */ 1036 public static Date getNextMonthEndTime() { 1037 Calendar cal = Calendar.getInstance(); 1038 cal.add(Calendar.MONTH, 1); 1039 cal.set(Calendar.DAY_OF_MONTH, getDayOfMonth(cal.getTime())); 1040 return getIntegralEndTime(cal.getTime()); 1041 } 1042 1043 /** 1044 * 获取当前季度第一天的起始时间 1045 * 1046 * @return 返回当前季度第一天的起始时间 1047 */ 1048 public static Date getQuarterStartTime() { 1049 Calendar cal = Calendar.getInstance(); 1050 int month = cal.get(Calendar.MONTH); 1051 if (month < 3) { 1052 cal.set(Calendar.MONTH, 0); 1053 } else if (month < 6) { 1054 cal.set(Calendar.MONTH, 3); 1055 } else if (month < 9) { 1056 cal.set(Calendar.MONTH, 6); 1057 } else { 1058 cal.set(Calendar.MONTH, 9); 1059 } 1060 cal.set(Calendar.DAY_OF_MONTH, 1); 1061 return getIntegralStartTime(cal.getTime()); 1062 } 1063 1064 /** 1065 * 获取当前季度最后一天的结束时间 1066 * 1067 * @return 返回当前季度最后一天的结束时间 1068 */ 1069 public static Date getQuarterEndTime() { 1070 Calendar cal = Calendar.getInstance(); 1071 int month = cal.get(Calendar.MONTH); 1072 if (month < 3) { 1073 cal.set(Calendar.MONTH, 2); 1074 } else if (month < 6) { 1075 cal.set(Calendar.MONTH, 5); 1076 } else if (month < 9) { 1077 cal.set(Calendar.MONTH, 8); 1078 } else { 1079 cal.set(Calendar.MONTH, 11); 1080 } 1081 cal.set(Calendar.DAY_OF_MONTH, getDayOfMonth(cal.getTime())); 1082 return getIntegralEndTime(cal.getTime()); 1083 } 1084 1085 /** 1086 * 获取前一个工作日 1087 * 1088 * @return 返回前一个工作日 1089 */ 1090 public static Date getPrevWorkday() { 1091 Calendar cal = Calendar.getInstance(); 1092 cal.add(Calendar.DAY_OF_MONTH, -1); 1093 if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { 1094 cal.add(Calendar.DAY_OF_MONTH, -2); 1095 } 1096 if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { 1097 cal.add(Calendar.DAY_OF_MONTH, -1); 1098 } 1099 return getIntegralStartTime(cal.getTime()); 1100 } 1101 1102 /** 1103 * 获取下一个工作日 1104 * 1105 * @return 返回下个工作日 1106 */ 1107 public static Date getNextWorkday() { 1108 Calendar cal = Calendar.getInstance(); 1109 cal.add(Calendar.DAY_OF_MONTH, 1); 1110 if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { 1111 cal.add(Calendar.DAY_OF_MONTH, 2); 1112 } 1113 if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { 1114 cal.add(Calendar.DAY_OF_MONTH, 1); 1115 } 1116 return getIntegralStartTime(cal.getTime()); 1117 } 1118 1119 /** 1120 * 获取当周的第一个工作日 1121 * 1122 * @return 返回第一个工作日 1123 */ 1124 public static Date getFirstWorkday() { 1125 Calendar cal = Calendar.getInstance(); 1126 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 1127 return getIntegralStartTime(cal.getTime()); 1128 } 1129 1130 /** 1131 * 获取当周的最后一个工作日 1132 * 1133 * @return 返回最后一个工作日 1134 */ 1135 public static Date getLastWorkday() { 1136 Calendar cal = Calendar.getInstance(); 1137 cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); 1138 return getIntegralStartTime(cal.getTime()); 1139 } 1140 1141 /** 1142 * 判断指定日期是否是工作日 1143 * 1144 * @param date 指定日期 1145 * @return 如果是工作日返回true,否则返回false 1146 */ 1147 public static boolean isWorkday(Date date) { 1148 Calendar cal = Calendar.getInstance(); 1149 if (date != null) { 1150 cal.setTime(date); 1151 } 1152 int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); 1153 return !(dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY); 1154 } 1155 1156 /** 1157 * 获取指定日期是星期几 1158 * 1159 * @param date 指定日期 1160 * @return 返回星期几的描述 1161 */ 1162 public static String getWeekdayDesc(Date date) { 1163 final String[] weeks = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; 1164 Calendar cal = Calendar.getInstance(); 1165 if (date != null) { 1166 cal.setTime(date); 1167 } 1168 return weeks[cal.get(Calendar.DAY_OF_WEEK) - 1]; 1169 } 1170 1171 1172 /***********2017年10月25日 by左宇新增以下代码***********/ 1173 1174 /** 1175 * 获取当年的第一天 1176 * @return 1177 */ 1178 public static Date getCurrYearFirst(){ 1179 Calendar currCal=Calendar.getInstance(); 1180 int currentYear = currCal.get(Calendar.YEAR); 1181 return getYearFirst(currentYear); 1182 } 1183 1184 /** 1185 * 获取当年的最后一天 1186 * @return 1187 */ 1188 public static Date getCurrYearLast(){ 1189 Calendar currCal=Calendar.getInstance(); 1190 int currentYear = currCal.get(Calendar.YEAR); 1191 return getYearLast(currentYear); 1192 } 1193 1194 /** 1195 * 获取某年第一天日期 1196 * @param year 年份 1197 * @return Date 1198 */ 1199 public static Date getYearFirst(int year){ 1200 Calendar calendar = Calendar.getInstance(); 1201 calendar.clear(); 1202 calendar.set(Calendar.YEAR, year); 1203 Date currYearFirst = calendar.getTime(); 1204 return currYearFirst; 1205 } 1206 1207 /** 1208 * 获取某年最后一天日期 1209 * @param year 年份 1210 * @return Date 1211 */ 1212 public static Date getYearLast(int year){ 1213 Calendar calendar = Calendar.getInstance(); 1214 calendar.clear(); 1215 calendar.set(Calendar.YEAR, year); 1216 calendar.roll(Calendar.DAY_OF_YEAR, -1); 1217 Date currYearLast = calendar.getTime(); 1218 return currYearLast; 1219 } 1220 /*********** end ***********/ 1221 1222 /** 1223 * 获取指定日期的月份第一天的日期 1224 * @param date 1225 * @return 1226 */ 1227 public static Date getMonthFirstDate(Date date){ 1228 Calendar cal = Calendar.getInstance(); 1229 cal.setTime(date); 1230 cal.set(Calendar.DAY_OF_MONTH, 1); 1231 return getIntegralStartTime(cal.getTime()); 1232 } 1233 /** 1234 * 获取指定日期的下个月第一天的日期 1235 * @param date 1236 * @return 1237 */ 1238 public static Date getNextMonthFirstDate(Date date){ 1239 Calendar cal = Calendar.getInstance(); 1240 cal.setTime(date); 1241 cal.add(Calendar.MONTH, 1); 1242 cal.set(Calendar.DAY_OF_MONTH, 1); 1243 return getIntegralStartTime(cal.getTime()); 1244 } 1245 1246 1247 /***********2018年02月06日 by左宇新增以下代码***********/ 1248 1249 /** 1250 * 获取任意时间的年份第一天 1251 * @param date 1252 * @return 1253 * @author zuoy 2018/02/06 1254 */ 1255 public static Date getFirstYearDate(Date date) { 1256 Calendar calendar = Calendar.getInstance(); 1257 calendar.setTime(date); 1258 int currentYear = calendar.get(Calendar.YEAR); 1259 return getYearFirst(currentYear); 1260 } 1261 1262 /** 1263 * 获取任意时间的年份最后一天 1264 * @param date 1265 * @return 1266 * @author zuoy 2018/02/06 1267 */ 1268 public static Date getLastYearDate(Date date) { 1269 Calendar calendar = Calendar.getInstance(); 1270 calendar.setTime(date); 1271 int currentYear = calendar.get(Calendar.YEAR); 1272 return getYearLast(currentYear); 1273 } 1274 /** 1275 * 返回指定日期的季的第一天 1276 * 1277 * @param date 1278 * @return 1279 */ 1280 public static Date getFirstQuarterDate(Date date) { 1281 Calendar c = Calendar.getInstance(); 1282 c.setTime(date); 1283 int currentMonth = c.get(Calendar.MONTH) + 1; 1284 if (currentMonth >= 1 && currentMonth <= 3) { 1285 c.set(Calendar.MONTH, 0); 1286 }else if (currentMonth >= 4 && currentMonth <= 6) { 1287 c.set(Calendar.MONTH, 3); 1288 }else if (currentMonth >= 7 && currentMonth <= 9) { 1289 c.set(Calendar.MONTH, 4); 1290 }else if (currentMonth >= 10 && currentMonth <= 12) { 1291 c.set(Calendar.MONTH, 9); 1292 }else{ 1293 c.set(Calendar.DATE, 1); 1294 } 1295 c.set(Calendar.DATE, 1); 1296 return parseDate(c.getTime()); 1297 } 1298 1299 /** 1300 * 返回指定日期的季的最后一天 1301 * 1302 * @param date 1303 * @return 1304 */ 1305 public static Date getLastQuarterDate(Date date) { 1306 Calendar c = Calendar.getInstance(); 1307 c.setTime(date); 1308 int currentMonth = c.get(Calendar.MONTH) + 1; 1309 if (currentMonth >= 1 && currentMonth <= 3) { 1310 c.set(Calendar.MONTH, 2); 1311 c.set(Calendar.DATE, 31); 1312 } else if (currentMonth >= 4 && currentMonth <= 6) { 1313 c.set(Calendar.MONTH, 5); 1314 c.set(Calendar.DATE, 30); 1315 } else if (currentMonth >= 7 && currentMonth <= 9) { 1316 c.set(Calendar.MONTH, 8); 1317 c.set(Calendar.DATE, 30); 1318 } else if (currentMonth >= 10 && currentMonth <= 12) { 1319 c.set(Calendar.MONTH, 11); 1320 c.set(Calendar.DATE, 31); 1321 } 1322 return parseDate(c.getTime()); 1323 } 1324 1325 /** 1326 * 获取任意时间的月第一天 1327 * @param date 1328 * @return 1329 * @author zuoy 2018/02/06 1330 */ 1331 public static Date getFirstMonthDate(Date date) { 1332 Calendar calendar = Calendar.getInstance(); 1333 calendar.setTime(date); 1334 calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); 1335 return parseDate(calendar.getTime()); 1336 } 1337 1338 /** 1339 * 获取任意时间的月的最后一天 1340 * @param date 1341 * @return 1342 * @author zuoy 2018/02/06 1343 */ 1344 public static Date getLastMonthDate(Date date) { 1345 Calendar calendar = Calendar.getInstance(); 1346 calendar.setTime(date); 1347 calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); 1348 return parseDate(calendar.getTime()); 1349 } 1350 1351 /** 1352 * 获取任意时间的周一 1353 * @param date 1354 * @return 1355 * @author zuoy 2018/02/06 1356 */ 1357 public static Date getFirstWeekDate(Date date) { 1358 Calendar calendar = Calendar.getInstance(); 1359 calendar.setTime(date); 1360 //判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了 1361 int dayWeek = calendar.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 1362 if(1 == dayWeek) { 1363 calendar.add(Calendar.DAY_OF_MONTH, -1); 1364 } 1365 calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 1366 int day = calendar.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 1367 calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek()-day);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 1368 return parseDate(calendar.getTime()); 1369 } 1370 1371 /** 1372 * 获取任意时间的周末 1373 * @param date 1374 * @return 1375 * @author zuoy 2018/02/06 1376 */ 1377 public static Date getLastWeekDate(Date date) { 1378 Date firstDate = getFirstWeekDate(date); 1379 Calendar calendar = Calendar.getInstance(); 1380 calendar.setTime(firstDate); 1381 calendar.add(Calendar.DATE, 6); 1382 return parseDate(calendar.getTime()); 1383 } 1384 1385 /** 1386 * 获取日期属于当年第几季度 1387 * 1388 * @param date 1389 * @return 1390 */ 1391 public static int getQuarterOfYear(Date date) { 1392 Calendar calendar = Calendar.getInstance(); 1393 calendar.setTime(date); 1394 return calendar.get(Calendar.MONTH) / 3 + 1; 1395 } 1396 1397 /** 1398 * 获取日期属于当年第几周 1399 * @param date 1400 * @return 1401 * @author zuoy 2018/02/06 1402 */ 1403 public static int getWeekOfYear(Date date){ 1404 Calendar calendar = Calendar.getInstance(); 1405 calendar.setFirstDayOfWeek(Calendar.MONDAY); 1406 calendar.setTime(date); 1407 return calendar.get(Calendar.WEEK_OF_YEAR); 1408 } 1409 1410 /** 1411 * 获取指定日期年份 1412 * @param date 1413 * @return 1414 * @author zuoy 2018/02/06 1415 */ 1416 public static int getYear(Date date) { 1417 Calendar calendar = Calendar.getInstance(); 1418 calendar.setTime(date); 1419 return calendar.get(Calendar.YEAR); 1420 } 1421 /** 1422 * 获取指定日期月份 1423 * @param date 1424 * @return 1425 * @author zuoy 2018/02/06 1426 */ 1427 public static int getMonth(Date date) { 1428 Calendar calendar = Calendar.getInstance(); 1429 calendar.setTime(date); 1430 return calendar.get(Calendar.MONTH) + 1; 1431 } 1432 1433 /** 1434 * 日期的增加月份 1435 * 1436 * @param date 日期 1437 * @param addMonth 增加的月份,正数表示增加,负数表示减少 1438 * @return Date 日期的增加月数后的结果 1439 * @author zuoy 2018/02/09 1440 */ 1441 public static Date addMonth(Date date, int addMonth) { 1442 Calendar calendar = Calendar.getInstance(); 1443 calendar.setTime(date); 1444 calendar.add(Calendar.MONTH, addMonth); 1445 return new Date(calendar.getTime().getTime()); 1446 } 1447 /*********** end ***********/ 1448 1449 /***********2018年03月13日 by左宇新增以下代码***********/ 1450 /** 1451 * 日期的增加年份 1452 * 1453 * @param date 日期 1454 * @param addYear 增加的周数,正数表示增加,负数表示减少 1455 * @return Date 日期的增加年份后的结果 1456 */ 1457 public static Date addYear(Date date, int addYear) { 1458 Calendar calendar = Calendar.getInstance(); 1459 calendar.setTime(date); 1460 calendar.add(Calendar.YEAR, addYear); 1461 return new Date(calendar.getTime().getTime()); 1462 } 1463 1464 /** 1465 * 日期的增加季度 1466 * 1467 * @param date 日期 1468 * @param addQuarter 增加的周数,正数表示增加,负数表示减少 1469 * @return Date 日期的增加季度后的结果 1470 */ 1471 public static Date addQuarter(Date date, int addQuarter) { 1472 int newAddQuarter = addQuarter < 0 ? addQuarter - 3 : addQuarter + 3; 1473 Calendar calendar = Calendar.getInstance(); 1474 calendar.setTime(date); 1475 calendar.add(Calendar.MONTH, newAddQuarter); 1476 return new Date(calendar.getTime().getTime()); 1477 } 1478 /** 1479 * 日期的增加周数 1480 * 1481 * @param date 日期 1482 * @param addWeek 增加的周数,正数表示增加,负数表示减少 1483 * @return Date 日期的增加周数后的结果 1484 */ 1485 public static Date addWeek(Date date, int addWeek) { 1486 Calendar calendar = Calendar.getInstance(); 1487 calendar.setTime(date); 1488 calendar.add(Calendar.WEEK_OF_YEAR, addWeek); 1489 return new Date(calendar.getTime().getTime()); 1490 } 1491 1492 /** 1493 * 获取日期属于当月第几周 1494 * 1495 * @param date 1496 * @return 1497 * @throws Exception 1498 */ 1499 public static int getWeekOfMonth(Date date){ 1500 Calendar calendar = Calendar.getInstance(); 1501 calendar.setTime(date); 1502 //第几周 1503 int week = calendar.get(Calendar.WEEK_OF_MONTH); 1504 //第几天,从周日开始 1505 int day = calendar.get(Calendar.DAY_OF_WEEK); 1506 return week; 1507 } 1508 1509 /** 1510 * 将字符串转为yyyy-MM-dd日期类型 1511 * 1512 * @param dateStr 1513 * @return 1514 * @author zuoy 2018-03-13 1515 */ 1516 public static Date parseDate(String dateStr) { 1517 return parse(dateStr, "yyyy-MM-dd"); 1518 } 1519 1520 /*********** end ***********/ 1521 1522 1523 }