清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; import org.apache.commons.lang.StringUtils; /** * 日期工具类 * * @author yugc */ public final class DateUtil { private DateUtil() { } /** * yyyy-MM-dd */ public static final String FORMAT1 = "yyyy-MM-dd" ; /** * yyyy.MM.dd */ public static final String FORMAT2 = "yyyy.MM.dd" ; /** * yyyy/MM/dd */ public static final String FORMAT3 = "yyyy/MM/dd" ; /** * yyyy-MM-dd HH:mm */ public static final String FORMAT4 = "yyyy-MM-dd HH:mm" ; /** * yyyy.MM.dd HH:mm */ public static final String FORMAT5 = "yyyy.MM.dd HH:mm" ; /** * yyyy/MM/dd HH:mm */ public static final String FORMAT6 = "yyyy/MM/dd HH:mm" ; /** * yyyy-MM-dd HH:mm:ss */ public static final String FORMAT7 = "yyyy-MM-dd HH:mm:ss" ; /** * YYYY-MM-dd HH-mm-ss */ public static final String FORMAT15 = "YYYY-MM-dd HH-mm-ss" ; /** * yyyy.MM.dd HH:mm:ss */ public static final String FORMAT8 = "yyyy.MM.dd HH:mm:ss" ; /** * yyyy/MM/dd HH:mm:ss */ public static final String FORMAT9 = "yyyy/MM/dd HH:mm:ss" ; /** * yyyy_MM_dd_HH_mm_ss */ public static final String FORMAT10 = "yyyy_MM_dd_HH_mm_ss" ; /** * yy-MM-dd */ public static final String FORMAT11 = "yy-MM-dd" ; /** * yyyyMMdd */ public static final String FORMAT12 = "yyyyMMdd" ; /** * yyyyMMddHHmmss */ public static final String FORMAT13 = "yyyyMMddHHmmss" ; /** * yyyyMM */ public static final String FORMAT14 = "yyyyMM" ; public static Date getCurrentDate() { return new Date(System.currentTimeMillis()); } public static Date getYesterDay() { return addDay( new Date(System.currentTimeMillis()), - 1 ); } public static String getYesterDayString() { return parseDateToString(addDay( new Date(System.currentTimeMillis()), - 1 ), FORMAT1); } /** * 得到年月日的路径 * @return */ public static String getThisYearMonthDay(String dateString) { Date date = parseStringToDate(dateString, FORMAT12); return DateUtil.getYear(date) + "/" + DateUtil.getMonth(date) + "/" + DateUtil.getDay(date) + "/" ; } /** * 返回当前日期或时间 * * @param format * @return */ public static String getCurrentDate(String format) { if (StringUtils.isBlank(format)) { format = FORMAT1; } Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat(format); String currentTime = formatter.format(date); return currentTime; } /** * 返回当前时间 * * @return */ public static String getCurrentTime() { String datetime = getCurrentDate(FORMAT7); String time = datetime.substring(datetime.indexOf( " " ) + 1 ); return time; } /** * 返回当前时间加随机数 * * @return */ public static String getCurrentDateTimeRandom() { String datetime = getCurrentDate(DateUtil.FORMAT10) + "_" + Math.random(); return datetime; } /** * 返回当前时间和日期 * @return */ public static String getCurrentDateTimeString() { return getCurrentDate(DateUtil.FORMAT7); } /** * 返回当前日期 * * @return */ public static Date getCurrentDateTime() { String datetime = getCurrentDate(FORMAT7); return parseStringToDate(datetime, "" ); } public static Timestamp getCurrentTimestamp() { String datetime = getCurrentDate(FORMAT7); return parseStringToTimestamp(datetime, "" ); } public static Timestamp getCurrentTimestamp(String format) { String datetime = getCurrentDate(format); return parseStringToTimestamp(datetime, "" ); } /** * 日期转换为字符串 * * @param date 日期 * @param format 格式 * @return 返回字符型日期 */ public static String parseDateToString(Date date, String format) { String result = "" ; DateFormat formatter = null ; try { if (date != null ) { if (StringUtils.isBlank(format)) { formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); } else { formatter = new SimpleDateFormat(format); } result = formatter.format(date); } } catch (Exception e) { } return result; } /** * 时间1-时间2的毫秒 * * @param t1 * @param t2 * @return */ public static long between(Timestamp t1, Timestamp t2) { if ((t1 != null ) && (t2 != null )) { return t1.getTime() - t2.getTime(); } return 0 ; } /** * 两个日期date1-date2相减,相差的天数 * * @param date1 * 日期 * @param date2 * 日期 * @return 返回相减后的日期 */ public static int betweenTwoDates(Date date1, Date date2) { return ( int ) ((getMillis(date1) - getMillis(date2)) / ( 24 * 3600 * 1000 )); } /** * 将字符串转换为日期 * * @param str * @return * @throws ParseException */ public static Date parseStringToDate(String str, String format) { DateFormat formatter = null ; Date date = null ; if (StringUtils.isNotBlank(str)) { if (StringUtils.isBlank(format)) { formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); } else { formatter = new SimpleDateFormat(format); } try { date = formatter.parse(str); } catch (ParseException e) { e.printStackTrace(); } } return date; } /** * 返回2007到今年的年份 * * @return */ public static List<Integer> get2007ToThisYear() { // 当前时间 Calendar c = Calendar.getInstance(); c.setTime( new Date()); // 返回的年份 List<Integer> the2007ToThisYearList = new ArrayList<Integer>(); // 当前年 int thisYear = c.get(Calendar.YEAR); for ( int i = thisYear; i >= 2007 ; i--) { the2007ToThisYearList.add(i); } return the2007ToThisYearList; } /** * 获取当前月的前几个月份的时间 * @param months * @return */ public static Date getDateBeforeMonths( int months) { // 当前时间 Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -months); return c.getTime(); } /** * 获取当前日期的前几天的时间 * @param days * @return */ public static Date getDateBeforeDays( int days) { // 当前时间 Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, -days); return c.getTime(); } /** * 返回1-12月份 * * @return */ public static List<String> getAllMonth() { List<String> theMonthList = new ArrayList<String>(); for ( int i = 1 ; i < 13 ; i++) { if (i < 10 ) { theMonthList.add( "0" + i); } else { theMonthList.add( "" + i); } } return theMonthList; } /** * 返回日期中的年份 * * @param date * 日期 * @return 返回年份 */ public static int getYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.YEAR); } /** * 返回日期中的月份 * * @param date * 日期 * @return 返回月份 */ public static int getMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.MONTH) + 1 ; } /** * 返回日期中的日 * * @param date * 日期 * @return 返回日 */ public static int getDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.DAY_OF_MONTH); } /** * 返回日期中的小时 * * @param date * 日期 * @return 返回小时 */ public static int getHour(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.HOUR_OF_DAY); } /** * 返回日期中的分钟 * * @param date * 日期 * @return 返回分钟 */ public static int getMinute(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.MINUTE); } /** * 返回日期中的秒钟 * * @param date * 日期 * @return 返回秒钟 */ public static int getSecond(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.SECOND); } /** * 返回日期代表的毫秒 * * @param date * 日期 * @return 返回毫秒 */ public static long getMillis(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.getTimeInMillis(); } /** * 返回当前日期代表的毫秒 * * @return */ public static long getCurrentTimeMillis() { return System.currentTimeMillis(); } public static Date addMonth(Date date, int month) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(c.MONTH, month); return c.getTime(); } /** * 日期加天数,可以向前加,向后加 * * @param date * 日期 * @param day * 天数 * @return 返回相加后的日期 */ public static Date addDay(Date date, int day) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(getMillis(date) + (( long ) day) * 24 * 3600 * 1000 ); return c.getTime(); } /** * 日期加秒数,可以向前加,向后加 * * @param date * 日期 * @param second * 秒数 * @return 返回相加后的日期 */ public static Date addSecond(Date date, long second) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(getMillis(date) + second * 1000 ); return c.getTime(); } /** * 根据一个日期,返回是星期几 * * @param sdate * @return */ public static String getWeekByDate(String sdate) { // 再转换为时间 Date date = parseStringToDate(sdate, "" ); Calendar c = Calendar.getInstance(); c.setTime(date); int day = c.get(Calendar.DAY_OF_WEEK); // day中存的就是星期几了,其范围 1~7 // 1=星期日 7=星期六,其他类推 return day + "" ; } /** * 根据一个日期,返回是星期几 * 1=星期日 7=星期六,其他类推 * * @param sdate * @return */ public static int getWeekByDate(Date date) { // 再转换为时间 Calendar c = Calendar.getInstance(); c.setTime(date); // day中存的就是星期几了,其范围 1~7 // 1=星期日 7=星期六,其他类推 return c.get(Calendar.DAY_OF_WEEK); } /** * 日期为一年中的第几周 * @return */ public static String getWeekNum(Date date) { Calendar c = Calendar.getInstance(Locale.CHINA); c.setTime(date); String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR)); return week; } public static java.sql.Date parseUtilDateToSqlDate(Date date) { if (date != null ) { return new java.sql.Date(date.getTime()); } else { return null ; } } public static java.sql.Date parseStringToSqlDate(String dateStr, String format) { Date date = null ; if (StringUtils.isBlank(format)) { date = parseStringToDate(dateStr, "yyyy-MM-dd HH:mm:ss" ); } else { date = parseStringToDate(dateStr, format); } return parseUtilDateToSqlDate(date); } public static Timestamp parseUtilDateToTimestamp(Date date, String format) { return parseStringToTimestamp(parseDateToString(date, format), format); } public static Date parseTimestampToUtilDate(Timestamp date, String format) { return parseStringToDate(parseDateToString(date, format), format); } public static Timestamp parseStringToTimestamp(String dateStr, String format) { if (StringUtils.isBlank(dateStr)) { return null ; } Date date = null ; if (StringUtils.isBlank(format)) { date = parseStringToDate(dateStr, "yyyy-MM-dd HH:mm:ss" ); } else { date = parseStringToDate(dateStr, format); } if (date != null ) { long t = date.getTime(); return new Timestamp(t); } else { return null ; } } /** * 获取时间2099-12-31,23:59:59 * * @return */ public static Timestamp getMaxTimestamp() { return DateUtil.parseStringToTimestamp( "2099-12-31 23:59:59" , DateUtil.FORMAT7); } /** * 返回日期中的年月日,格式化成yyyyMMdd * @param date * @return */ public static String getYearMonthDay(Date date) { SimpleDateFormat formatter = new SimpleDateFormat(FORMAT12); String currentTime = formatter.format(date); return currentTime; } /** * 取得指定月份的第一天 * * @param strdate String * @return String */ public static String getMonthBegin(String strdate) { return parseDateToString(parseStringToDate(strdate, "yyyy-MM" ), "" ); } /** * 取得指定月份的最后一天 * * @param strdate String * @return String */ public static String getMonthEnd(String strdate) { Date date = parseStringToDate(getMonthBegin(strdate), "" ); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, 1 ); calendar.add(Calendar.DAY_OF_YEAR, - 1 ); return parseDateToString(calendar.getTime(), "" ); } public static String getPreviousMonthBegin() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1 ); Date date = new Date(cal.getTimeInMillis()); return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM" ), FORMAT1); } public static String getPreviousMonthEnd() { Date date = parseStringToDate(getPreviousMonthBegin(), FORMAT1); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, 1 ); calendar.add(Calendar.DAY_OF_YEAR, - 1 ); return parseDateToString(calendar.getTime(), FORMAT1); } /** * 上个月 * * @return */ public static String getPreviousMonth() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1 ); Date date = new Date(cal.getTimeInMillis()); return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM" ), FORMAT14); } /** * 上两个月 * * @return */ public static String getPreviousTwoMonth() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 2 ); Date date = new Date(cal.getTimeInMillis()); return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM" ), FORMAT14); } /** * 判断两个日期是否在同一周 * @param date1 * @param date2 * @return */ public static boolean isSameWeekDates(Date date1, Date date2) { Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(date1); cal2.setTime(date2); int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR); if ( 0 == subYear) { if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) { return true ; } } else if (( 1 == subYear) && ( 11 == cal2.get(Calendar.MONTH))) { // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周 if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) { return true ; } } else if ((- 1 == subYear) && ( 11 == cal1.get(Calendar.MONTH))) { if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) { return true ; } } return false ; } public static void main(String[] args) { System.out.println(getPreviousMonthBegin()); System.out.println(getPreviousMonthEnd()); System.out.println(getYearMonthDay( new Date())); System.out.println(getYearMonthDay(parseStringToDate( "2009-11-2 12:1:21" , FORMAT7))); System.out.println( "current time: " + getCurrentDateTime()); System.out.println( "addsecond: " + addSecond(getCurrentDateTime(), -1L)); Date date = new Date(); System.out.println( "current date: " + date.toString()); System.out.println( "test parseDateToString: " + parseDateToString(date, "" )); System.out.println( "test parseStringToDate: " + parseStringToDate( "1990-01-1 00:00:00" , "" )); System.out.println( "test getYear: " + getYear(date)); System.out.println( "test getMonth: " + getMonth(date)); System.out.println( "test getDay: " + getDay(date)); System.out.println( "test getHour: " + getHour(date)); System.out.println( "test getMinute: " + getMinute(date)); System.out.println( "test getSecond: " + getSecond(date)); System.out.println( "test getMillis: " + getMillis(date)); System.out.println( "test addDate: " + addDay(date, 2 )); System.out.println( "test betweenTwoDays: " + betweenTwoDates(date, addDay(date, 2 ))); System.out.println( "test getWeekNum: " + getWeekNum(addDay(date, - 2 ))); System.out.println( "test getWeekByDate: " + getWeekByDate(parseDateToString(date, "" ))); System.out.println( "test getMonthBegin: " + getMonthBegin(parseDateToString(date, "" ))); System.out.println( "test getMonthEnd: " + getMonthEnd(parseDateToString(date, "" ))); System.out.println( "test isSameWeekDates: " + isSameWeekDates(date, addDay(date, - 2 ))); System.out.println(getPreviousTwoMonth()); System.out.println(getPreviousMonth()); List<Integer> yearList = get2007ToThisYear(); List<String> monthList = getAllMonth(); } public static String addOneDay(String stopTime) { String finishTime = stopTime; Date finishDate = null ; if (stopTime != null && ! "" .equals(stopTime)) { if (stopTime.length() < 19 ) { finishTime = stopTime.substring( 0 , 10 ) + " 00:00:00" ; } finishDate = DateUtil.parseStringToDate(finishTime, DateUtil.FORMAT7); finishDate = DateUtil.addDay(finishDate, 1 ); return DateUtil.parseDateToString(finishDate, DateUtil.FORMAT7); } else { return null ; } } /** * 给date加1天 * @param date * @return */ public static Date addOneDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, 1 ); return calendar.getTime(); } } |