DateUtil.java 27.6 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
package com.viontech.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil extends DateUtils {

    private static final String[] weekDays = {"(周日)", "(周一)", "(周二)", "(周三)", "(周四)", "(周五)", "(周六)"};


    private final static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    private DateUtil() {
        throw new IllegalStateException("请勿实例化工具类");
    }

    private static ThreadLocal<Map<String, SimpleDateFormat>> local = new ThreadLocal<>();

    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_MONTH_FORMAT = "yyyy-MM";
    public static final String DATE_FORMAT_NO_DELIMITER = "yyyyMMdd";
    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final String TIME_FORMAT_NO_SEC = "HH:mm";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_TIME_FORMAT_NO_SEC = "yyyy-MM-dd HH:mm";
    public static final String DATE_TIME_FORMAT_NO_DELIMITER = "yyyyMMddHHmmss";
    public static final String TIMESTAMP_NO_DELIMITER = "yyyyMMddHHmmssSSS";
    public static final String DATE_PRECISE_TO_MINUTE = "yyyyMMddHHmm";
    public static final String[] WEEKS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    public static final String[] DEFAULT_FORMATS = {TIMESTAMP_FORMAT, DATE_TIME_FORMAT, DATE_FORMAT, TIME_FORMAT, DATE_PRECISE_TO_MINUTE};

    public static final String DAY_BEGIN_TIME = " 00:00:00";
    public static final String DAY_END_TIME = " 23:59:59";

    /**
     * 精确到毫秒的完整UTC时间   如:2017-07-04T15:50:18.223
     */
    public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS_S_UTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    /**
     * 精确到毫秒的完整时间    如:2010-12-01 23:15:06.999
     */
    public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS_S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    /**
     * 英文全称  如:2010-12-01 23:15:06
     */
    public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 英文全称  如:2010-12-01 23:15
     */
    public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    /**
     * 英文简写(默认)如:2010-12-01
     */
    public static SimpleDateFormat FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");

    private static SimpleDateFormat getDateFormat(String format) {
        Map<String, SimpleDateFormat> map = local.get();
        if (map == null) {
            map = new HashMap<>();
            local.set(map);
        }
        SimpleDateFormat sdf = map.get(format);
        if (sdf == null) {
            sdf = new SimpleDateFormat(format);
            map.put(format, sdf);
        }
        return sdf;
    }

    private static SimpleDateFormat getDateFormatDefault() {
        return getDateFormat(DATE_TIME_FORMAT);
    }

    private static SimpleDateFormat getDateFormatDate() {
        return getDateFormat(DATE_FORMAT);
    }

    private static SimpleDateFormat getDateFormatTime() {
        return getDateFormat(TIME_FORMAT);
    }

    /**
     * 自动判断日期字符串的格式,返回Date对象
     *
     * @param dateString 日期字符串
     * @param dateFormat 格式字符串数组。为空时使用<code>DateUtil.DEFAULT_FORMATS</code>
     * @return 日期Date对象
     * @throws ParseException
     * @see DateUtils#parseDate
     */
    public static Date parse(String dateString, String... dateFormat) throws ParseException {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }

        if (dateFormat == null || dateFormat.length == 0) {
            return DateUtils.parseDate(dateString, DEFAULT_FORMATS);
        } else {
            return DateUtils.parseDate(dateString, dateFormat);
        }
    }

    /**
     * 取指定格式的当前时间字符串
     *
     * @param dateFormat
     * @return
     */
    public static String getCurrentTime(String dateFormat) {
        Date date = new Date();
        return format(date, dateFormat);
    }

    /**
     * 将字符串转换成Date类型
     *
     * @param dateString
     * @param dateFormat
     * @return
     */
    public static Date parse(String dateString, String dateFormat) {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        try {
            return DateUtils.parseDate(dateString, dateFormat);
        } catch (ParseException e) {
            logger.error("日期格式化异常", e);
            return null;
        }
    }

    /**
     * 将Date类型转化成字符串
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String format(Date date, String dateFormat) {
        if (date == null) {
            return "";
        } else {
            return DateFormatUtils.format(date, dateFormat);
        }
    }

    /**
     * 在传入的日期基础上往后加n天
     *
     * @param date
     * @param n    要加的天数
     * @return
     */
    public static Date addDay(Date date, int n) {
        return DateUtils.addDays(date, n);
    }


    /**
     * 在传入的日期基础上往后加n天
     *
     * @param dateStr
     * @param n       要加的天数
     * @return
     */
    public static String addDay(String dateStr, String dateFormat, int n) {
        if (StringUtils.isBlank(dateStr) || StringUtils.isBlank(dateFormat)) {
            return "";
        }
        Date date = parse(dateStr, dateFormat);
        Date newDate = addDay(date, n);
        return format(newDate, dateFormat);
    }

    /**
     * 判断当前时间是否在开始时间与结束时间之间
     *
     * @param time  当前时间
     * @param begin 开始时间
     * @param end   结束时间
     * @return boolen类型,true表示在两者间,false表示不在两者之间
     */
    public static boolean isTimeIn(Date time, Date begin, Date end) {
        return time.getTime() >= begin.getTime() && time.getTime() <= end.getTime();
    }

    /**
     * 判断当前时间是否在开始时间与结束时间之间
     *
     * @param time  当前时间
     * @param begin 开始时间
     * @param end   结束时间
     * @return boolen类型,true表示在两者间,false表示不在两者之间(time和end相同时,为false)
     */
    public static boolean isTimeIn1(Date time, Date begin, Date end) {
        return time.getTime() >= begin.getTime() && time.getTime() < end.getTime();
    }

    /**
     * 判断指定日期是星期几
     *
     * @param time   要判断的日期
     * @param format 输入的日期格式
     * @return 返回数字[1:星期一,2:星期二,....,7:星期日]
     * @throws ParseException
     */
    public static int getWeek(String time, String format) throws ParseException {
        return getWeek(DateUtils.parseDate(time, format));
    }

    /**
     * 判断指定日期是星期几
     *
     * @param date 要判断的日期
     * @return 返回数字[1:星期一,2:星期二,....,7:星期日]
     * @throws ParseException
     */
    public static int getWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int week = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            week = 7;
        } else {
            week = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return week;
    }

    /**
     * 获取一年的第几周,默认周一是第一天(按中国习惯)
     *
     * @param date            -
     * @param isInternational - 是否国际化,true:周日为第一天,false:周一为第一天,即周日会比国际通用的weekofyear值少1
     * @return -
     */
    public static int getWeekOfYear(Date date, Boolean isInternational) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int i = c.get(Calendar.WEEK_OF_YEAR);
        int week = getWeek(date);
        if (isInternational != null && !isInternational && week == 7) {
            i = i - 1;
        }
        return i;
    }

    /**
     * 获取一年的第几周
     *
     * @param time            -
     * @param format          -
     * @param isInternational - 是否国际化,true:周日为第一天,false:周一为第一天
     * @return -
     */
    public static int getWeekOfYear(String time, String format, Boolean isInternational) {
        return getWeekOfYear(parse(time, format), isInternational);
    }


    /**
     * 判断是否为有效的身份证日期
     *
     * @param date
     * @return
     */
    public static boolean isIdDate(String date) {
        return isDateFormat(date, "yyyyMMdd");
    }

    /**
     * 判断传入的字符串dateStr是否是日期格式patternStr的字符串 @author yejg
     *
     * @param dateStr
     * @param patternStr
     * @return
     */
    public static boolean isDateFormat(String dateStr, String patternStr) {
        Date date = null;
        try {
            date = parse(dateStr, patternStr);
        } catch (Exception e) {
        }

        return date == null ? false : true;
    }

    /**
     * 将字符串日期转成Timestamp类型
     *
     * @param dateString 字符串类型的时间
     * @param format     字符串类型的时间要转换的格式
     * @return Timestamp类型的时间戳
     * @throws ParseException
     */
    public static java.sql.Timestamp parse2Timestamp(String dateString, String format) throws ParseException {
        return new java.sql.Timestamp(DateUtils.parseDate(dateString, format).getTime());
    }

    /**
     * 获取两个时间的间隔,字符串表示
     *
     * @param start
     * @param end
     * @return
     * @author huadi
     */
    public static String getDiffTimeStr(Date start, Date end) {
        String time = "";
        if (start != null && end != null) {
            int t = (int) (end.getTime() - start.getTime()) / 1000;
            String h = "";
            String m = "";
            String s = "";
            h = (int) t / 3600 + "";
            m = (int) (t % 3600) / 60 + "";
            s = t % 60 + "";
            if (h.length() <= 1) {
                h = "0" + h;
            }
            if (m.length() <= 1) {
                m = "0" + m;
            }
            if (s.length() <= 1) {
                s = "0" + s;
            }
            time = h + ":" + m + ":" + s;
        }
        return time;
    }

    /**
     * 获取两个日期之间间隔的分钟数
     *
     * @param startDate
     * @param endDate
     * @return
     * @author zhougz
     */
    public static int getIntervalMinute(Date startDate, Date endDate) {
        int min = 0;
        if (null != startDate && null != endDate) {
            long end = endDate.getTime();
            long start = startDate.getTime();
            long betweenDate = (end - start) / (60 * 1000);
            min = Long.valueOf(betweenDate).intValue();
        }
        return min;
    }

    /**
     * 获取两个日期之间间隔的天数
     *
     * @param start_date
     * @param end_date
     * @return
     * @author sunyy
     */
    public static int getIntervalDay(Date start_date, Date end_date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            start_date = sdf.parse(sdf.format(start_date));
            end_date = sdf.parse(sdf.format(end_date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(start_date);
        long time1 = cal.getTimeInMillis();
        cal.setTime(end_date);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }


    /**
     * 星期转换为星期索引
     *
     * @param week
     * @return
     */
    public static int weekToNum(String week) {
        int weekNum = -1;
        for (int i = 0, j = WEEKS.length; i < j; i++) {
            if (week != null && WEEKS[i].toLowerCase().contains(week.toLowerCase())) {
                weekNum = i + 1;
                break;
            }
        }
        return weekNum;
    }

    /**
     * 获取日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);//时
        cal.set(Calendar.MINUTE, 0);//分
        cal.set(Calendar.SECOND, 0);//秒
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        Date resultDate = cal.getTime();
        return resultDate;
    }

    /**
     * 获取日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);//时
        cal.set(Calendar.MINUTE, 59);//分
        cal.set(Calendar.SECOND, 59);//秒
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, 6);
        cal.add(Calendar.DAY_OF_WEEK, d);
        Date resultDate = cal.getTime();
        return resultDate;
    }

    /**
     * 获取日期当月第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);//时
        c.set(Calendar.MINUTE, 0);//分
        c.set(Calendar.SECOND, 0);//秒
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        Date resultDate = c.getTime();
        return resultDate;
    }

    /**
     * 获取日期当月最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);//时
        c.set(Calendar.MINUTE, 59);//分
        c.set(Calendar.SECOND, 59);//秒
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date resultDate = c.getTime();
        return resultDate;
    }

    /**
     * 获取今天剩余秒数
     *
     * @return
     */
    public static int getTodaySeconds() {
        Calendar curDate = Calendar.getInstance();
        Calendar tommorowDate = new GregorianCalendar(curDate
                .get(Calendar.YEAR), curDate.get(Calendar.MONTH), curDate
                .get(Calendar.DATE) + 1, 0, 0, 0);
        return (int) (tommorowDate.getTimeInMillis() - curDate.getTimeInMillis()) / 1000;
    }

    /**
     * 获取当前时间字符串(yyyy-MM-dd HH:mm:ss)
     *
     * @return 当前时间字符串
     */
    public static String getNowStr() {
        return getDatetimeStr(new Date());
    }

    /**
     * Date转String
     *
     * @param date 时间
     * @return 字符串(yyyy-MM-dd HH:mm:ss)
     */
    public static String getDatetimeStr(Date date) {
        return getDateFormatDefault().format(date);
    }

    /**
     * 获取当前时间字符串(yyyy-MM-dd)
     *
     * @return 当前时间字符串
     */
    public static String getNowDateStr() {
        return getDateFormatDate().format(new Date());
    }

    /**
     * 获取当前时间字符串(HH:mm:ss)
     *
     * @return 当前时间字符串
     */
    public static String getNowTimeStr() {
        return getDateFormatTime().format(new Date());
    }

    /**
     * 计算今天到指定时间的剩余天数
     * <p>1.不足一天按一天算;</p>
     * <p>2.指定时间是过去时间,返回0;</p>
     *
     * @param date 指定时间
     * @return 剩余天数
     */
    public static Integer decorateDays(Date date) {
        Long d = date.getTime() - new Date().getTime();
        if (d < 0L) {
            return 0;
        }
        d = d / 3600_000L / 24L;
        return d.intValue() + 1;
    }

    /**
     * Date转String
     *
     * @param date   时间
     * @param format 格式
     * @return 时间字符串
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat dateFormat = getDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return dateFormat.format(date);
    }

    /**
     * Date转String
     *
     * @param date 时间
     * @return 时间字符串(yyyy-MM-dd HH:mm:ss)
     */
    public static String formatDate(Date date) {
        return formatDate(date, DATE_TIME_FORMAT);
    }

    /**
     * String转Date
     *
     * @param dateStr 时间字符串
     * @param format  格式
     * @return Date 时间
     * @throws ParseException
     */
    public static Date parseDate(String dateStr, String format) throws ParseException {
        return getDateFormat(format).parse(dateStr);
    }

    /**
     * String转Date
     *
     * @param dateStr 时间字符串(yyyy-MM-dd HH:mm:ss)
     * @return Date 时间
     * @throws ParseException
     */
    public static Date parseDate(String dateStr) throws ParseException {
        return parseDate(dateStr, DATE_TIME_FORMAT);
    }

    /**
     * 计算时间差(毫秒)
     *
     * @param d1 第一个时间
     * @param d2 第二个时间
     * @return d1.getTime() - d2.getTime()
     */
    public static long calcTimeDifference(Date d1, Date d2) {
        return d1.getTime() - d2.getTime();
    }

    /**
     * 计算传入时间和当前时间相比已经过去了多少分钟
     *
     * @param date 传入时间
     * @return 传入时间和当前时间相比已经过去了多少分钟
     */
    public static long calcPastMinute(Date date) {
        long diffMillisecond = calcTimeDifference(new Date(), date);
        return diffMillisecond / 60_000L;
    }

    /**
     * 计算传入时间和当前时间相比已经过去了多少分钟
     *
     * @param dateStr 传入时间字符串(yyyy-MM-dd HH:mm:ss)
     * @return 传入时间和当前时间相比已经过去了多少分钟
     */
    public static long calcPastMinute(String dateStr) throws ParseException {
        long diffMillisecond = calcTimeDifference(new Date(), parseDate(dateStr));
        return diffMillisecond / 60_000L;
    }

    /**
     * 获取N分钟之前的时间
     *
     * @param minute 单位分钟
     * @return N分钟之前的时间
     */
    public static Date getMinuteAgoDate(long minute) {
        long agoTimestamp = System.currentTimeMillis() - minute * 60_000L;
        return new Date(agoTimestamp);
    }

    /**
     * 获取之前的时间
     *
     * @param day    N天
     * @param hour   N小时
     * @param minute N分钟
     * @param second N秒
     * @return 之前的时间
     */
    public static Date getAgoDate(long day, long hour, long minute, long second) {
        long agoTimestamp = System.currentTimeMillis() - (((day * 24 + hour) * 60 + minute) * 60 + second) * 1000L;
        return new Date(agoTimestamp);
    }

    /**
     * 获取基准时间之前的时间
     *
     * @param baseDate 基准时间
     * @param day      N天
     * @param hour     N小时
     * @param minute   N分钟
     * @param second   N秒
     * @return 之后的时间
     */
    public static Date getAfterDate(Date baseDate, long day, long hour, long minute, long second) {
        long currTimestamp = baseDate.getTime();
        long afterTimestamp = currTimestamp + (((day * 24 + hour) * 60 + minute) * 60 + second) * 1000L;
        return new Date(afterTimestamp);
    }

    /**
     * 获取指定月份第一天Date
     *
     * @param dateStr yyyy-MM
     * @return Date类型时间
     */
    public static Date getFirstDayByMonth(String dateStr) throws Exception {
        dateStr = dateStr + "-01";
        return parseDate(dateStr, "yyyy-MM-dd");
    }

    /**
     * 获取指定月份下个月第一天Date
     *
     * @param dateStr
     * @return
     */
    public static Date getNextFirstDayByMonth(String dateStr) throws Exception {
        Date date = getFirstDayByMonth(dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获取指定月份最后一天Date
     *
     * @param dateStr yyyy-MM
     * @return Date类型时间
     */
    public static Date getLastDayByMonth(String dateStr) throws Exception {
        Date date = getFirstDayByMonth(dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * <p>功能描述: 获取当前时间距离一天结束的剩余秒数</p>
     *
     * @param currentDate -
     * @return <p>修改履历:</p>
     * @date 2019/9/5 19:28
     */
    public static Integer getRemainSecondsOneDay(Date currentDate) {
        Calendar midnight = Calendar.getInstance();
        midnight.setTime(currentDate);
        midnight.add(Calendar.DAY_OF_MONTH, 1);
        midnight.set(Calendar.HOUR_OF_DAY, 0);
        midnight.set(Calendar.MINUTE, 0);
        midnight.set(Calendar.SECOND, 0);
        midnight.set(Calendar.MILLISECOND, 0);
        return Long.valueOf((midnight.getTime().getTime() - currentDate.getTime()) / 1000).intValue();
    }

    /**
     * <p>功能描述: 转换两个时间的时间差为天时分秒</p>
     *
     * @param beginDate -计算开始时间
     * @param endDate   -计算结束时间
     * @return <p>修改履历:</p>
     * @author jishu
     * @date 2019/9/24 10:18
     */
    public static Map<String, Integer> parseDayHourMinuteSecond(Date beginDate, Date endDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - beginDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("day", Long.valueOf(day).intValue());
        resultMap.put("hour", Long.valueOf(hour).intValue());
        resultMap.put("minute", Long.valueOf(min).intValue());
        resultMap.put("second", Long.valueOf(sec).intValue());
        return resultMap;
    }

    /**
     * 通过日期获取周几,格式如:7-08(周三)
     *
     * @param date -
     * @return -
     */
    public static String dateToDayOfWeekStr(Date date) {
        Calendar calendar = Calendar.getInstance();
        StringBuilder sb = new StringBuilder();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        if (month < 10) {
            sb.append("0");
        }
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        sb.append(month).append("-").append(day);

        int i = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (i < 0) {
            i = 0;
        }
        sb.append(weekDays[i]);
        return sb.toString();
    }

    /**
     * 通过日期获取一年中的第几周
     *
     * @param date -
     * @return -
     */
    public static String dateToWeekOfYearStr(Date date) {
        Calendar calendar = Calendar.getInstance();
        StringBuilder sb = new StringBuilder();
        calendar.setTime(date);
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        sb.append("第").append(weekOfYear).append("周");
        return sb.toString();
    }

    /**
     * 通过日期获取几月
     *
     * @param date -
     * @return -
     */
    public static String dateToMonthOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        StringBuilder sb = new StringBuilder();
        calendar.setTime(date);
        int month1 = calendar.get(Calendar.MONTH) + 1;
        sb.append("第").append(month1).append("月");
        return sb.toString();
    }

    /**
     * 转yyyy-MM-dd 为yyyy-MM-dd HH:mm:ss
     *
     * @param date -
     * @param type - 1.每天的开始 2.每天的结束
     * @return -
     */
    public static String transDateToDateTime(String date, Integer type) {
        if (StringUtils.length(date) == 10) {
            if (type == 1) {
                return date + DAY_BEGIN_TIME;
            } else {
                return date + DAY_END_TIME;
            }
        }else{
            return date;
        }
    }

    public static Date getFirstDateOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_YEAR, c.getActualMinimum(Calendar.DAY_OF_YEAR));
        return setDayMinTime(c.getTime());
    }
    public static Date setDayMinTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, c.getActualMinimum(Calendar.HOUR_OF_DAY));
        c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
        c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
        c.set(Calendar.MILLISECOND, c.getActualMinimum(Calendar.MILLISECOND));
        return c.getTime();
    }
    public static List<Date> getDaysBetweenDates(Date startDate,Date endDate) {
        List<Date> dates = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        while (c.getTime().before(endDate)) {
            dates.add(c.getTime());
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
        dates.add(endDate);
        return dates;
    }
    public static List<String> getDaysStrBetweenDates(Date startDate,Date endDate) {
        List<String> dates = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        while (c.getTime().before(endDate)) {
            dates.add(formatDate(c.getTime(),DATE_FORMAT));
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
        dates.add(formatDate(endDate,DATE_FORMAT));
        return dates;
    }

    /**
     * String转Date
     * 支持:2010-12-01 23:15:06.999
     * 2010-12-01 23:15:06
     * 2010-12-01 23:15
     * 2010-12-01
     *
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static Date dateFormat(String dateStr) throws ParseException {
        if (dateStr.length() == 23||dateStr.length() ==22 || dateStr.length() ==21){
            if(dateStr.contains("T")){
                return DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS_S_UTC.parse(dateStr);
            }
            return DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS_S.parse(dateStr);
        }
        if (dateStr.length() == 19)
            return DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS.parse(dateStr);
        else if (dateStr.length() == 16)
            return DateUtil.FORMAT_YYYY_MM_DD_HH_MM.parse(dateStr);
        else if (dateStr.length() == 10)
            return DateUtil.FORMAT_YYYY_MM_DD.parse(dateStr);
        else
            return null;
    }
}