DateUtil.java 53.3 KB
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 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
package cn.com.glsx.admin.modules.utils;

import cn.com.glsx.admin.common.constants.ConstantsElectricFence;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DurationFormatUtils;
import org.apache.http.client.utils.DateUtils;
import org.eclipse.jetty.util.StringUtil;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 日期工具类
 *
 * @author liuyf@didihu.com.cn
 * @version 1.0
 * @history 2017年9月7日 create by liuyf@didihu.com.cn
 */
public class DateUtil extends org.apache.commons.lang.time.DateUtils {

    public static String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static String YMD = "yyyy-MM-dd";
    public final static String[] pattern = {"yyyy-MM-dd HH:mm:ss"};

    public enum TimeUnitType {
        DAY(1), HOUR(2), MINUTE(3), SECOND(4);

        private int value = 0;

        TimeUnitType(int value) {
            this.value = value;
        }

        public int value() {
            return value;
        }
    }

    public enum CustomTimeType {
        TIMETYPE_TODAY(1),
        /**
         * 今天
         */
        TIMETYPE_YESTERDAY(2),
        /**
         * 昨天
         */
        TIMETYPE_THISWEEK(3),
        /**
         * 本周
         */
        TIMETYPE_LASTWEEK(4),
        /**
         * 上周
         */
        TIMETYPE_THISMONTH(5),
        /**
         * 本月
         */
        TIMETYPE_LASTMONTH(6),
        /**
         * 上月
         */
        TIMETYPE_SCOPE(7);
        /**
         * 时间段
         */

        private int value = 0;

        CustomTimeType(int value) {
            this.value = value;
        }

        public int value() {
            return value;
        }
    }


    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date getCurrentDate() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static Date parseDate(String date) {
        return parseDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date parseDateYmd(String date) {
        return parseDate(date, "yyyy-MM-dd");
    }

    public static String formatDateYm(Date date) {
        return formatDate(date, "yyyyMM");
    }

    public static String formatDate(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 返回格式 yyyy年MM月dd日HH时
     *
     * @param date
     * @return
     */
    public static String formatDateChineseYmdh(Date date) {
        return formatDate(date, "yyyy年MM月dd日HH时");
    }

    /**
     * 返回格式 yyyy年MM月dd日HH时mm分ss秒
     *
     * @param date
     * @return
     */
    public static String formatDateChineseYmdhms(Date date) {
        return formatDate(date, "yyyy年MM月dd日HH时mm分ss秒");
    }

    public static String formatSerial(Date date) {
        return formatDate(date, "yyyyMMddHHmmss");
    }

    public static String formatDateYmd(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    public static String formatDate(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        try {
            return formatter.format(date);

        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }

    public static Date getBeforeOrAfterTime(Date date, TimeUnitType timeUnitType, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (TimeUnitType.SECOND.value() == timeUnitType.value()) {
            cal.add(Calendar.SECOND, num);
        } else if (TimeUnitType.MINUTE.value() == timeUnitType.value()) {
            cal.add(Calendar.MINUTE, num);
        } else if (TimeUnitType.HOUR.value() == timeUnitType.value()) {
            cal.add(Calendar.HOUR, num);
        } else if (TimeUnitType.DAY.value() == timeUnitType.value()) {
            cal.add(Calendar.DATE, num);
        }
        return cal.getTime();
    }

    public static String getBeforeOrAfterTimeString(Date date, TimeUnitType timeUnitType, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (TimeUnitType.SECOND.value() == timeUnitType.value()) {
            cal.add(Calendar.SECOND, num);
        } else if (TimeUnitType.MINUTE.value() == timeUnitType.value()) {
            cal.add(Calendar.MINUTE, num);
        } else if (TimeUnitType.HOUR.value() == timeUnitType.value()) {
            cal.add(Calendar.HOUR, num);
        } else if (TimeUnitType.DAY.value() == timeUnitType.value()) {
            cal.add(Calendar.DATE, num);
        }
        return formatDate(cal.getTime());
    }

    public static Date parseDate(String date, String pattern) {
        if (StringUtil.isBlank(date)) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        try {
            return formatter.parse(date);
        } catch (ParseException e) {
            return null;
        }

    }

    /**
     * 将时间置为23时59分钟59秒
     *
     * @param date
     * @return
     */
    public static Date setFullPassDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 将时间后退2小时
     *
     * @param date
     * @return
     */
    public static Date getFallBack2Hour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 2);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 将时间精确到小时
     *
     * @param date
     * @return
     */
    public static Date getTimeHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取两个时间间隔的天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getDiffDays(Date startDate, Date endDate) {
        long difftime = endDate.getTime() - startDate.getTime();
        return difftime / (24L * 60L * 60L * 1000L);
    }


    /**
     * 根据日期获取当天起始时间
     *
     * @param date
     * @return
     */
    public static Date getStartDateOfCurrentDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getStartYesterday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 根据日期获取下一天起始时间
     *
     * @param date
     * @return
     */
    public static Date getStartDateOfNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 根据日期当前日期顺延一周后的起始时间
     *
     * @param date
     * @return
     */
    public static Date getStartDateOfNextSevenDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 7);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 根据日期当前日期顺延一周后的起始时间
     *
     * @param date
     * @return
     */
    public static Date getStartDateOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 根据日期当前日期顺延一月后的起始时间
     *
     * @param date
     * @return
     */
    public static Date getStartDateOfNextMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /*
     * 封装一天只能的时间区域
     */
    public static List<Date> getStaticByDateDateArea(Date date) {
        List<Date> dates = new ArrayList<Date>();
        Date startdate = getStartDateOfCurrentDay(date);
        Date nextday = getStartDateOfNextDay(date);
        int step = 2;
        dates.add(startdate);
        for (int i = 1; i < 12; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startdate);
            calendar.add(Calendar.HOUR_OF_DAY, i * step);
            dates.add(calendar.getTime());
        }
        dates.add(nextday);
        return dates;
    }

    /*
     * 封装一周之内时间区域
     */
    public static List<Date> getStaticByWeekDateArea(Date date) {
        List<Date> dates = new ArrayList<Date>();
        Date startdate = getStartDateOfCurrentDay(date);
        Date nextday = getStartDateOfNextSevenDay(date);
        dates.add(startdate);
        for (int i = 1; i < 7; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startdate);
            calendar.add(Calendar.DAY_OF_MONTH, i);
            dates.add(calendar.getTime());
        }
        dates.add(nextday);
        return dates;
    }

    /*
     * 封装一周之内时间区域List<String>
     */
    public static List<String> getStaticByWeekLabel(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        List<String> dates = new ArrayList<String>();
        Date startdate = getStartDateOfCurrentDay(date);
        dates.add(dateFormat.format(startdate));
        for (int i = 1; i < 7; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startdate);
            calendar.add(Calendar.DAY_OF_MONTH, i);
            dates.add(dateFormat.format(calendar.getTime()));
        }
        return dates;
    }

    /*
     * 封装一月之内时间区域
     */
    public static List<Date> getStaticByMonthDateArea(Date date) {
        List<Date> dates = new ArrayList<Date>();
        Date startdate = getStartDateOfMonth(date);
        Date nextday = getStartDateOfNextMonth(date);
        long daydiff = getDiffDays(startdate, nextday);
        dates.add(startdate);
        for (int i = 1; i < daydiff; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startdate);
            calendar.add(Calendar.DAY_OF_MONTH, i);
            dates.add(calendar.getTime());
        }
        dates.add(nextday);
        return dates;
    }

    /*
     *封装一点时间之内的时间区域(天)
     */
    public static List<Date> getStaticBySE(Date startDate, Date endDate) {
        List<Date> dates = new ArrayList<Date>();

        long daydiff = getDiffDays(startDate, endDate);
        dates.add(startDate);
        for (int i = 1; i < daydiff; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.DAY_OF_MONTH, i);
            dates.add(calendar.getTime());
        }
        dates.add(endDate);
        return dates;
    }

    public static List<List<String>> getStaticYMD(Date startDate, Date endDate) {
        List<List<String>> dateStr = new ArrayList<>();
        // 获取两个日期的天数差
        long daydiff = getDiffDays(startDate, endDate);
        dateStr.add(Lists.newArrayList(DateUtil.formatDateYmd(startDate)));
        for (int i = 1; i < daydiff; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.DAY_OF_MONTH, i);
            dateStr.add(Lists.newArrayList(DateUtil.formatDateYmd(calendar.getTime())));
        }
        //如果开始时间和结束时间是同一天。只取一天
        if (Objects.equals(DateUtil.formatDateYmd(startDate), DateUtil.formatDateYmd(endDate))) {
            return dateStr;
        }
        dateStr.add(Lists.newArrayList(DateUtil.formatDateYmd(endDate)));
        return dateStr;
    }


    /*
     * 封装一月之内时间区域
     */
    public static List<String> getStaticByMonthLabel(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
        List<String> dates = new ArrayList<String>();
        Date startdate = getStartDateOfMonth(date);
        Date nextday = getStartDateOfNextMonth(date);
        long daydiff = getDiffDays(startdate, nextday);
        dates.add(dateFormat.format(startdate));
        for (int i = 1; i < daydiff; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startdate);
            calendar.add(Calendar.DAY_OF_MONTH, i);
            dates.add(dateFormat.format(calendar.getTime()));
        }
        return dates;
    }

    public static String formatDate(String format, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * 根据时间段拼装sql语句
     * 时间段查询,例:DateUtils.appendSqlByTimeRange(Constants.TIMETYPE_SCOPE, "t.createtime", "2014-09-17", "2014-09-18");
     *
     * @param timeRange 查询时间:全部-0、今天-1、昨天-2、本周-3、上周-4、本月-5、上月-6、时间段-7、近12小时-8、近24小时-9、近7天-10、近30天-11
     * @param field     字段名,例:t.createtime
     * @param times     时间段查询,开始时间和结束时间
     * @return 2017.9.7  mym  修改  新增 近7天  近30天
     */
    public static String appendSqlByTimeRange(Integer timeRange, String field, String... times) {
        if (StringUtils.isEmpty(field)) {
            return "";
        }

        String appendSql = "";
        if (timeRange != null) {
            switch (timeRange) {
                case ConstantsElectricFence.TIMETYPE_TODAY://今天
                    appendSql += " and date_format(" + field + ",'%Y-%m-%d') = date_format(NOW(),'%Y-%m-%d')";
                    break;
                case ConstantsElectricFence.TIMETYPE_YESTERDAY:// 昨天
                    appendSql += " and date_format(" + field + ",'%Y-%m-%d')=date_format(DATE_ADD(NOW(),INTERVAL -1 DAY),'%Y-%m-%d')";
                    break;
                case ConstantsElectricFence.TIMETYPE_THISWEEK:// 本周
                    appendSql += " and YEARWEEK(date_format(" + field + ",'%Y-%m-%d'),5) = YEARWEEK(now(),5) ";
                    break;
                case ConstantsElectricFence.TIMETYPE_LASTWEEK:// 上周
                    appendSql += " and YEARWEEK(date_format(" + field + ",'%Y-%m-%d'),5) = YEARWEEK(now(),5)-1 ";
                    break;
                case ConstantsElectricFence.TIMETYPE_THISMONTH:// 本月
                    appendSql += " and date_format(" + field + ",'%Y-%m')=date_format(now(),'%Y-%m') ";
                    break;
                case ConstantsElectricFence.TIMETYPE_LASTMONTH:// 上月
                    appendSql += " and date_format(" + field + ",'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m') ";
                    break;
                case ConstantsElectricFence.TIMETYPE_IN_7_DAYS:// 近7天
                    appendSql += " and date_sub(curdate(), INTERVAL 7 DAY) <= date(" + field + ") ";
                    break;
                case ConstantsElectricFence.TIMETYPE_IN_30_DAYS:// 近30天
                    appendSql += " and date_sub(curdate(), INTERVAL 30 DAY) <= date(" + field + ") ";
                    break;
                case ConstantsElectricFence.TIMETYPE_SCOPE:// 时间段
                    try {
                        if (null != times && times.length == 2) {
                            String stime = times[0];
                            String etime = times[1];
                            //时间校验
                            Date sdate = parseDate(stime, "yyyy-MM-dd");
                            Date edate = parseDate(etime, "yyyy-MM-dd");
                            if (null != sdate && null != edate && (edate.after(sdate) || sdate.equals(edate))) {
                                appendSql += " and " + field + " >= '" + stime + "' and " + field + " <= '" + getToCondtionDay(parseDate(etime, "yyyy-MM-dd")) + "'";
                            }
                        }
                        break;
                    } catch (Exception ex) {
                        break;
                    }
                case ConstantsElectricFence.TIMETYPE_SCOPE_WITH_TIME:
                    try {
                        if (null != times && times.length == 2) {
                            String stime = times[0];
                            String etime = times[1];
                            //时间校验
                            Date sdate = parseDate(stime);
                            Date edate = parseDate(etime);
                            if (null != sdate && null != edate && (edate.after(sdate) || sdate.equals(edate))) {
                                appendSql += " and " + field + " >= '" + stime + "' and " + field + " <= '" + getToCondtionDay(parseDate(etime)) + "'";
                            }
                        }
                        break;
                    } catch (Exception ex) {
                        break;
                    }
            }
        }
        return appendSql;
    }

    public static String getCurrentTime(String format) {
        if (format == "" || format == null) {
            format = YMDHMS;
        }
        Date date = new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat(format);
        return dateFm.format(date);
    }

    /**
     * 获得今天日期
     *
     * @return
     */
    public static String getTodayDate() {
        Date date = new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        return dateFm.format(date);
    }

    /**
     * 获得昨天日期
     *
     * @return
     */
    public static String getYesterday() {
      /*  Date date = new Date();
        date = new Date(date.getTime() - 1000 * 60 * 60 * 24);

        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        return dateFm.format(date);*/
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -1);
        Date date = cal.getTime();
        return dateFm.format(date);

    }

    public static String getTomorrow() {
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, 1);
        Date date = cal.getTime();
        return dateFm.format(date);

    }

    /****
     * 时间加一天
     * @param d1
     * @return
     */
    public static String getToCondtionDay(Date d1) {
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(d1);
        cal.add(Calendar.DATE, 1);
        Date date = cal.getTime();
        return dateFm.format(date);

    }

    /**
     * 获得本周一的日期
     *
     * @return
     */
    public static String getMondayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayofweek == 0) {
            dayofweek = 7;
        }
        c.add(Calendar.DATE, -dayofweek + 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(c.getTime());
    }

    /**
     * 获取本周星期天的日期
     *
     * @return
     */
    public static String getCurrentWeekday() {
//    	int mondayPlus = getMondayPlus();
//    	GregorianCalendar currentDate = new GregorianCalendar();
//    	currentDate.add(GregorianCalendar.DATE, mondayPlus+6);
//    	Date monday = currentDate.getTime();
//    	DateFormat df = DateFormat.getDateInstance();
//    	String preMonday = df.format(monday);
        Calendar cal = Calendar.getInstance();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        cal.add(Calendar.WEEK_OF_YEAR, 1);
        String preMonday = df.format(cal.getTime());
        return preMonday;
    }

    /****
     * 获取下周一日期
     * @return
     */
    public static String getXiaWeekday() {
//    	int mondayPlus = getMondayPlus();
//    	GregorianCalendar currentDate = new GregorianCalendar();
//    	currentDate.add(GregorianCalendar.DATE, mondayPlus+6);
//    	Date monday = currentDate.getTime();
//    	DateFormat df = DateFormat.getDateInstance();
//    	String preMonday = df.format(monday);
        Calendar cal = Calendar.getInstance();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        cal.add(Calendar.WEEK_OF_YEAR, 1);
        cal.add(Calendar.DATE, 1);
        String preMonday = df.format(cal.getTime());
        return preMonday;
    }


    private static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            return 0;
        } else {
            return 1 - dayOfWeek;
        }
    }

    /**
     * 根据原来的时间(Date)获得相对偏移 N 月的时间(Date)
     *
     * @param protoDate   原来的时间(java.util.Date)
     * @param monthOffset (向前移正数,向后移负数)
     * @return 时间(java.util.Date)
     */
    public static Date getOffsetMonthDate(Date protoDate, int monthOffset) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(protoDate);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - monthOffset);
        return cal.getTime();
    }

    /**
     * 根据原来的时间(Date)获得相对偏移 N 天的时间(Date)
     *
     * @param protoDate  原来的时间(java.util.Date)
     * @param dateOffset (向前移正数,向后移负数)
     * @return 时间(java.util.Date)
     */
    public static Date getOffsetDayDate(Date protoDate, int dateOffset) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(protoDate);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - dateOffset);
        return cal.getTime();
    }

    /**
     * 得到上个月的最后一天
     */
    public static String getLastMonthLastDay() {
        //上个月最后一天
        Calendar calendar = Calendar.getInstance();
        //calendar.add(Calendar.MONTH, 1);    //加一个月
        calendar.set(Calendar.DATE, 1);        //设置为该月第一天
        calendar.add(Calendar.DATE, -1);    //再减一天即为上个月最后一天
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(calendar.getTime());
    }

    /**
     * 得到上个月的第一天
     */
    public static String getLastMonthFirstDay() {
        //上个月第一天
        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(new Date());
        gcLast.add(Calendar.MONTH, -1);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(gcLast.getTime());
    }

    /**
     * 获取上周一的日期
     *
     * @param date
     * @return
     */
    public static Date getLastWeekMonday(Date date) {
        Date a = org.apache.commons.lang.time.DateUtils.addDays(date, -1);
        Calendar cal = Calendar.getInstance();
        cal.setTime(a);
        cal.add(Calendar.WEEK_OF_YEAR, -1);// 一周
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    /**
     * 获取上周日的日期
     *
     * @param date
     * @return
     */
    public static Date getLastWeekSunday(Date date) {
        Date a = org.apache.commons.lang.time.DateUtils.addDays(date, -1);
        Calendar cal = Calendar.getInstance();
        cal.setTime(a);
        cal.set(Calendar.DAY_OF_WEEK, 1);
        return cal.getTime();
    }

    /***
     * 时间转为字符串
     * @param str 秒
     * @return
     */
    public static String conver(int str) {
        if (str > 0) {
            int s = str;
            int N = s / 3600;
            s = s % 3600;
            int K = s / 60;
            s = s % 60;
            int M = s;
            return N + "小时" + K + "分" + M + "秒";
        } else {
            return "0";
        }
    }

    /***
     * 得到本月的第一天
     * @return
     */
    public static String getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(calendar.getTime());
    }

    /***
     * 上月第一天
     * @return
     */
    public static String getPreviousMonthFirst() {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(5, 1);
        lastDate.add(2, -1);
        str = sdf.format(lastDate.getTime());
        return str;
    }

    /***
     * 上月最后一天
     * @return
     */
    public static String getPreviousMonthEnd() {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.add(2, -1);
        lastDate.set(5, 1);
        lastDate.roll(5, -1);
        str = sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * 获取本月最后一天的日期
     *
     * @return
     */
    public static String getEndDateOfThisMonth() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DATE, 1);
        cal.roll(Calendar.DATE, -1);
        Date endTime = cal.getTime();
        return sdf.format(endTime);
    }

    /**
     * 获取某月的最后一天
     *
     * @throws
     * @Title:getLastDayOfMonth
     * @Description:
     * @param:@param year
     * @param:@param month
     * @param:@return
     * @return:String
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());

        return lastDayOfMonth;
    }

    /**
     * 格式化传的时间为 X天X小时X分X秒
     *
     * @param durationTime
     * @return
     */
    public static String formatTimeInSec(long durationTime) {
        if (durationTime > 0) {
            int s = (int) durationTime;
            int day = s / (3600 * 24);
            s = s % (3600 * 24);
            int hour = s / 3600;
            s = s % 3600;
            int min = s / 60;
            s = s % 60;
            int sec = s;
            return day + "天" + hour + "小时" + min + "分" + sec + "秒";
        } else {
            return "0";
        }
    }

    /**
     * 格式化传的时间为 X天X小时X分
     *
     * @param durationTime
     * @return
     */
    public static String formatTimeInSec1(long durationTime) {
        if (durationTime > 0) {
            int s = (int) durationTime;
            int day = s / (3600 * 24);
            s = s % (3600 * 24);
            int hour = s / 3600;
            s = s % 3600;
            return day + "天" + hour + "小时";
        } else {
            return "0";
        }
    }

    /**
     * 格式化传的时间为 X天X小时X分
     *
     * @param durationTime
     * @return
     */
    public static String formatTimeInSec2(long durationTime) {
        if (durationTime > 0) {
            int s = (int) durationTime;
            int day = s / (3600 * 24);
            s = s % (3600 * 24);
            int hour = s / 3600;
            s = s % 3600;
            int min = s / 60;
            s = s % 60;
            int sec = s;
            return day + "天" + hour + "小时" + min + "分";
        } else {
            return "0";
        }
    }

    /**
     * 格式化   xx小时xx分钟
     *
     * @param time
     * @return
     */
    public static String formatTimeInSec3(long time) {
        if (time > 0 && time <= 60) {
            return "1分钟";
        } else if (time % 3600 == 0) {
            return time / 3600 + "小时";
        } else if (time > 60 && time < 3600) {
            return time / 60 + "分钟";
        } else if (time > 3600) {
            int s = (int) time;
            int hour = s / 3600;
            s = s % 3600;
            int min = s / 60;
            s = s % 60;
            int sec = s;
            if (s > 0) {
                min = min + 1;
            }
            if (min == 60) {
                return hour + 1 + "小时";
            }
            return hour + "小时" + min + "分钟";
        } else {
            return "0";
        }
    }


    /**
     * 将时间后退8小时  北京时间转格林威治时间
     *
     * @param inputDate HH:mm:ss
     * @return
     */
    public static String getFallBack8Hour(String inputDate) {
        String recDate = "";
        SimpleDateFormat inputFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = null;
        try {
            date = inputFormat.parse(inputDate);
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(date.getTime());
            // System.out.println("北京时间:" + cal.getTime().toString().substring(11, 19));
            cal.add(Calendar.HOUR, -8);
            recDate = cal.getTime().toString().substring(11, 19);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return recDate;
    }

    /**
     * 将时间前进8小时  格林威治时间转北京时间
     *
     * @param inputDate HH:mm:ss
     * @return
     */
    public static String getGoAhead8Hour(String inputDate) {
        String recDate = "";
        SimpleDateFormat inputFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = null;
        try {
            date = inputFormat.parse(inputDate);
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(date.getTime());
            cal.add(Calendar.HOUR, +8);
            recDate = cal.getTime().toString().substring(11, 19);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return recDate;
    }

    /**
     * 作者:sanri <br/>
     * 时间:2017-4-28下午1:35:59<br/>
     * 功能:date1 时间大于(晚于)date2 返回 true ,其余返回 false <br/>
     * 入参: <br/>
     */
    public static boolean compareDate(String date1, String date2) {
        DateFormat df = new SimpleDateFormat(YMDHMS);
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() > dt2.getTime()) {
                return true;
            } else if (dt1.getTime() < dt2.getTime()) {
                return false;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 将时间前进N分钟
     *
     * @param date
     * @param minute 前进的分钟数
     * @return
     */
    public static Date getMinuteBefore(Date date, int minute) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minute);//要前进的分钟数
        return cal.getTime();
    }

    public static boolean compareDate(Date date1, Date date2) {
        if (date1.getTime() > date2.getTime()) {
            return true;
        } else if (date1.getTime() < date2.getTime()) {
            return false;
        } else {
            return false;
        }
    }

    /**
     * 将字符串转为unix时间戳
     *
     * @param dateString 2016-04-12 13:46:03
     * @return
     */
    public static String dateToTimeStamp(String dateString) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long stamp = 0;
        try {
            Date date = df.parse(dateString);
            stamp = date.getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return stamp + "";
    }

    /**
     * unix时间戳转换为字符串
     *
     * @param timeString 1454216477
     * @return
     */
    public static Date timestampToDate(String timeString) {
        //  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long timestamp = Long.parseLong(timeString) * 1000;
        //  String date = sdf.format(new java.util.Date(timestamp));
        return new Date(timestamp);
    }

    /**
     * unix时间戳转换为dateFormat
     *
     * @param timestampString
     * @return
     */
    public static String timestampToDateString(String timestampString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = sdf.format(new Date(timestamp));
        return date;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param one 时间1
     * @param two 时间2
     * @return long[] 返回值为:{天, 时, 分, 秒}
     */
    public static long[] getDistanceTimes(Date one, Date two) {
        long time1 = one.getTime();
        long time2 = two.getTime();
        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = (diff / (60 * 60 * 1000) - day * 24);
        long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        return new long[]{day, hour, min, sec};
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式:1990-01-01 12:00:00
     * @param str2 时间参数 2 格式:2009-01-01 12:00:00
     * @return String 返回值为:xx天xx小时xx分xx秒
     */
    public static String getDistanceTime(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            long day = diff / (24 * 60 * 60 * 1000);
            long hour = (diff / (60 * 60 * 1000) - day * 24);
            long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            return day + "天" + hour + "小时" + min + "分" + sec + "秒";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式:1990-01-01 12:00:00
     * @param str2 时间参数 2 格式:2009-01-01 12:00:00
     * @return String 返回值为:xx天xx小时xx分xx秒
     */
    public static String getDistanceTimeIgnoreZero(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            long day = diff / (24 * 60 * 60 * 1000);
            long hour = (diff / (60 * 60 * 1000) - day * 24);
            long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            return getIgnoreZero(day, "天") + getIgnoreZero(hour, "小时") + getIgnoreZero(min, "分") + getIgnoreZero(sec, "秒");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 去掉0
     */
    private static String getIgnoreZero(long number, String unit) {
        if (number == 0) {
            return "";
        }
        return number + unit;
    }

    /**
     * 比较两个日期时间差
     *
     * @param startDate
     * @param endDate
     * @param timeUnitType
     * @return
     */
    public static long getDifferenceBetweenTwoTime(Date startDate, Date endDate, TimeUnitType timeUnitType) {
        long duration = endDate.getTime() - startDate.getTime();
        if (timeUnitType != null) {
            if (TimeUnitType.SECOND.value() == timeUnitType.value()) {
                return TimeUnit.MILLISECONDS.toSeconds(duration);
            } else if (TimeUnitType.MINUTE.value() == timeUnitType.value()) {
                return TimeUnit.MILLISECONDS.toMinutes(duration);
            } else if (TimeUnitType.HOUR.value() == timeUnitType.value()) {
                return TimeUnit.MILLISECONDS.toHours(duration);
            } else if (TimeUnitType.DAY.value() == timeUnitType.value()) {
                return TimeUnit.MILLISECONDS.toDays(duration);
            }
        }
        return duration;
    }

    public static long getDistanceDay(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            long day = diff / (24 * 60 * 60 * 1000);
            return day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取几天前的时间
     *
     * @return
     */
    public static String getReductionDay(int dayNum) {
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -dayNum);
        Date date = cal.getTime();
        return dateFm.format(date);

    }

    /**
     * 校验时间格式
     *
     * @param yyyyMM
     */
    public static boolean checkyyyyMM(String yyyyMM) {
        if (null == parseDate(yyyyMM, "yyyyMM") || yyyyMM.length() > 6 || yyyyMM.length() < 5) {
            return false;
        }
        int year = Integer.parseInt(yyyyMM.substring(0, 4));
        int month = Integer.parseInt(yyyyMM.substring(4));
        Calendar cal = Calendar.getInstance();
        if (year <= 0 || year > cal.get(Calendar.YEAR) || month <= 0 || month > 12) {
            return false;
        }
        return true;
    }

    /**
     * 时间往前或者往后推移N天
     *
     * @param days
     * @param date
     * @return
     */
    public static Date getDayBefore(int days, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -days);//把日期往后增加一天.整数往后推,负数往前移动
        return cal.getTime();
    }

    public static String getDayBeforeStr(int days, String dateStr) {
        DateFormat df = new SimpleDateFormat(YMDHMS);
        try {
            Date date = df.parse(dateStr);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DATE, -days);//把日期往后增加一天.整数往后推,负数往前移动
            date = cal.getTime();
            return df.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

//    public static void main(String[] args) throws ParseException {
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        long difference = getDifferenceBetweenTwoTime(new Date(), df.parse("2016-05-31 15:20:30"), TimeUnitType.MINUTE);
//        System.out.println(difference);
//    }

    public static long naturalIntervalDay(long millis1, long millis2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(millis1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(millis2);
        return naturalIntervalDay(calendar1, calendar2);
    }

    /**
     * 作者:sanri <br/>
     * 时间:2017-9-15下午12:41:04<br/>
     * 功能:计算两个日期间的自然日期间隔 从后一天开始计算,只算天数<br/>
     * 注:此方法没有包含当天
     *
     * @param cal1
     * @param cal2
     * @return
     */
    public static long naturalIntervalDay(Calendar cal1, Calendar cal2) {
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        //add by sanri at 2017/11/21 解决不同年,算出来的天数不一致的问题
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);

        if (year1 == year2) {
            //如果是同年,则直接可以时间相减
            return Math.abs(day2 - day1);
        }
        int yearSwap = year1;
        if (year1 > year2) {
            //交换两个年
            year1 = year2;
            year2 = yearSwap;
        }
        int timeDistance = 0;
        for (int i = year1; i < year2; i++) {
            if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {    //闰年
                timeDistance += 366;
            } else {
                timeDistance += 365;
            }
        }

        return Math.abs(timeDistance + (day2 - day1));
    }

    /**
     * 作者:sanri <br/>
     * 时间:2017-9-15上午10:17:05<br/>
     * 功能:计算两个日期的间隔,使用时间戳间隔然后取整 <br/>
     *
     * @param date1    日期 1
     * @param date2    日期 2
     * @param timeUnit
     * @return 两个日期的时间间隔
     */
    public static long interval(Date date1, Date date2, TimeUnit timeUnit) {
        return interval(date1.getTime(), date2.getTime(), timeUnit);
    }

    public static long interval(Calendar cal1, Calendar cal2, TimeUnit timeUnit) {
        return interval(cal1.getTimeInMillis(), cal2.getTimeInMillis(), timeUnit);
    }

    public static long interval(long millis1, long millis2, TimeUnit timeUnit) {
        long duration = Math.abs(millis2 - millis1);
        return interval(duration, timeUnit);
    }

    public static long interval(long duration, TimeUnit timeUnit) {
        return timeUnit.convert(duration, TimeUnit.MILLISECONDS);
    }


    public static long naturalIntervalDay(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        return naturalIntervalDay(calendar1, calendar2);
    }

    /**
     * 计算当前时间与指定时间的时间差
     *
     * @param time
     * @return
     * @author lidh
     */
    @SuppressWarnings("null")
    public static long getTimeDifference(String time) {
        Date parseDate = DateUtils.parseDate(time, pattern);
        return System.currentTimeMillis() - parseDate.getTime();
    }

    /**
     * 获取开始时间和结束时间的具体天数
     *
     * @param dBegin
     * @param dEnd
     * @return
     */
    public static List<Date> findDates(Date dBegin, Date dEnd) {
        List<Date> lDate = new ArrayList<Date>();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    public static int getMaxDayOfNow(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    public static int getMaxDayOfCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        int maxNum = cal.getMaximum(Calendar.DAY_OF_MONTH);
        return maxNum;
    }


    public static String getYearAndMonthYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat("yyyy-MM").format(cal.getTime());
        return yesterday;
    }

    public static String getYearOfNow() {
        Calendar cal = Calendar.getInstance();
        return String.valueOf(cal.get(Calendar.YEAR));
    }

    /**
     * 根据年 月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public static String getMonthOfNow() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        if (month < 10) {
            return "0".concat(String.valueOf(month));
        }
        return String.valueOf(month);
    }

    /**
     * HH 时 mm 分 ss 秒 时长
     *
     * @param timestamp 时长时间戳
     * @return HH 时 mm 分 ss 秒 时长
     */
    public static String normalFormatDuration(Long timestamp) {
        if (timestamp == 0L) {
            return "0 秒";
        }
        if (timestamp < 0L) {    //小于0不合法
            return "";
        }
        if (timestamp < org.apache.commons.lang.time.DateUtils.MILLIS_PER_HOUR) {
            return DurationFormatUtils.formatDuration(timestamp, "mm 分 ss 秒");
        }
        return DurationFormatUtils.formatDuration(timestamp, "HH 时 mm 分 ss 秒");
    }

    /**
     * 获取当前时间
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getNowDateTimeFormat() {
        LocalDateTime now = LocalDateTime.now();
        return now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static Long getTimestampMillisecond(String datetimeStr) {
        LocalDateTime datetime = LocalDateTime.parse(datetimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return datetime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
    }

    /**
     * 获取之后的时间
     *
     * @param dateTime1 yyyy-MM-dd HH:mm:ss
     * @param dateTime2 yyyy-MM-dd HH:mm:ss
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getAfterDateTimeStr(String dateTime1, String dateTime2) {
        if (StringUtils.isEmpty(dateTime1) && StringUtils.isEmpty(dateTime1)) {
            return "";
        }
        if (StringUtils.isEmpty(dateTime1)) {
            return dateTime2;
        }
        if (StringUtils.isEmpty(dateTime2)) {
            return dateTime1;
        }
        LocalDateTime localDateTime1 = LocalDateTime.parse(dateTime1, DateTimeFormatter.ofPattern(YMDHMS));
        LocalDateTime localDateTime2 = LocalDateTime.parse(dateTime2, DateTimeFormatter.ofPattern(YMDHMS));
        if (localDateTime1.isAfter(localDateTime2)) {
            return dateTime1;
        }
        return dateTime2;
    }


    public static Integer getMaxWeekOfYear(Integer year) {
        if (year == null) {
            return 0;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setMinimalDaysInFirstWeek(4);
        calendar.clear();
        calendar.set(year, Calendar.DECEMBER, 31); // 将calendar设置到该年的最后一天
        int days = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);//一年有多少天
        int weekCount = calendar.getActualMaximum(Calendar.WEEK_OF_YEAR);//一年有多少周
        //按需求,如果 weekCount * 7小于 days, weekCount+1
        if (new BigDecimal(weekCount).multiply(new BigDecimal(7)).compareTo(new BigDecimal(days)) < 0) {
            weekCount = weekCount + 1;
        }
        return weekCount; // 获取该年共有多少周
    }

    /**
     * 比较返回最大的日期
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Date getMaxDate(Date date1, Date date2) {
        if (date1 == null) {
            return date2;
        }
        if (date2 == null) {
            return date1;
        }
        return date1.after(date2) ? date1 : date2;
    }

    /**
     * 判断两个日期是否是在同一周内
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameWeek(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("Both dates must not be null");
        }

        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        int week1 = c1.get(Calendar.WEEK_OF_YEAR);
        int year1 = c1.get(Calendar.YEAR);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        int week2 = c2.get(Calendar.WEEK_OF_YEAR);
        int year2 = c2.get(Calendar.YEAR);

        return year1 == year2 && week1 == week2;
    }

    public static Date getFirstDateOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 将周的开始设置为周一
        calendar.setFirstDayOfWeek(Calendar.MONDAY);

        // 获取周一日期
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }

    public static Date getLastDateOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 将周的开始设置为周一
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.add(Calendar.DAY_OF_MONTH, 6);
        return calendar.getTime();
    }

    public static void main(String[] args) {
        // System.out.println(formatDate(getStartDateOfCurrentDay(new Date())));
        System.out.println(getMaxDayOfCurrentMonth());
        System.out.println(formatDateChineseYmdh(new Date()));
        System.out.println(getYearAndMonthYesterday());
        System.out.println(getYearOfNow());
        System.out.println(getDaysByYearMonth(2021, 5));
        System.out.println("当前年份有 " + getMaxWeekOfYear(2020) + " 个周");
        System.out.println("当前年份有 " + getMaxWeekOfYear(2021) + " 个周");
        System.out.println("当前年份有 " + getMaxWeekOfYear(2022) + " 个周");
        System.out.println("当前年份有 " + getMaxWeekOfYear(2023) + " 个周");
        System.out.println("当前年份有 " + getMaxWeekOfYear(2024) + " 个周");
    }
}