TimeUtil.java 23.8 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
package com.cnlive.goldenline.util;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 时间类工具
 */
@SuppressLint("SimpleDateFormat")
public class TimeUtil {

    private volatile static TimeUtil timeUtil;

    private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
    private static final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    private static final String TAG = TimeUtil.class.getSimpleName();

    private TimeUtil() {

    }

    public static TimeUtil getInstance() {
        if (timeUtil == null) {
            synchronized (TimeUtil.class) {
                timeUtil = new TimeUtil();
            }
        }
        return timeUtil;
    }

    public int getCurrentYear() {
        return getTime(Calendar.YEAR);
    }

    public int getCurrentMonth() {
        return getTime(Calendar.MONTH) + 1;
    }

    public int getCurrentDay() {
        return getTime(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取周几 注意顺序:周日、一、二、三、四、五、六
     *
     * @return
     */
    public int getCurrentDayOfWeek() {
        return getTime(Calendar.DAY_OF_WEEK);
    }

    public int getCurrentHour() {
        return getTime(Calendar.HOUR_OF_DAY);
    }

    public int getCurrentMinute() {
        return getTime(Calendar.MINUTE);
    }

    /**
     * 依据类型获取时间
     *
     * @param type
     * @return
     */
    private int getTime(int type) {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(type);
    }

    /**
     * long 毫秒值 格式化  HH:mm:ss  不足小时的则 返回 mm:ss
     *
     * @param second
     * @return
     */
    public static String stringForTime(long second) {
        int totalSeconds = (int) second;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        if (hours > 0) {
            return String.format(Locale.getDefault(), "%d:%02d:%02d", hours, minutes, seconds);
        } else {
            return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
        }
    }

    /**
     * 比较两个日期的时间差
     * 结束时间  -  开始时间
     *
     * @param fromDate 开始日期
     * @param endDate  结束日期 如果为空代表 比较指定日期 与今天的时间差 毫秒值
     * @param module   日期格式 可以为空
     * @return 如果endDate 在fromDate之后 返回正值 否则返回负数
     */
    public long compareTime(@NonNull String fromDate, String endDate, String module) {
        if (TextUtils.isEmpty(fromDate)) {
            return -1;
        }
        if (TextUtils.isEmpty(module)) {
            switch (fromDate.length()) {
                case 10:
                    module = "yyyy-MM-dd";
                    break;
                case 16: {
                    module = "yyyy-MM-dd HH:mm";
                }
                break;
                default:
                    module = "yyyy-MM-dd HH:mm:ss";
                    break;
            }
        }
        if (fromDate.length() > module.length()) {
            return -1;
        }
        if (TextUtils.isEmpty(endDate)) {
            endDate = getStandardDate(module);
        }
        long dateStr2Long = dateStr2Long(fromDate, module);
        long todaylong = dateStr2Long(endDate, module);
        long time = todaylong - dateStr2Long;
        return time;
    }

    public boolean afterToday(int year, int month, int day, int hour, int minute, String pattern) {
        StringBuffer sb = new StringBuffer();
        sb.append(year);
        sb.append("-");
        sb.append(StringUtils.formatnum(month, "00") + "-");
        sb.append(StringUtils.formatnum(day, "00") + " ");
        sb.append(StringUtils.formatnum(hour, "00") + ":");
        sb.append(StringUtils.formatnum(minute, "00"));

        return afterToday(sb.toString(), pattern);
    }

    /**
     * 检测的日期是否在今天之后
     *
     * @param checkDate
     * @param pattern
     * @return
     */
    public boolean afterToday(@NonNull String checkDate, String pattern) {
        return judgeDateTime(checkDate, null, pattern);
    }

    /**
     * 比较日期先后 fromDate 与 endDate 先后顺序
     *
     * @param fromDate
     * @param endDate  如果为空 默认为今天
     * @return
     */
    public boolean judgeDateTime(@NonNull String fromDate, String endDate, String pattern) {
        if (TextUtils.isEmpty(pattern)) {
            int length = fromDate.length();
            switch (length) {
                case 7:
                    pattern = "yyyy-MM";
                    break;
                case 10:
                    pattern = "yyyy-MM-dd";
                    break;
                case 16:
                    pattern = "yyyy-MM-dd HH:mm";
                    break;
                case 19:
                    pattern = "yyyy-MM-dd HH:mm:ss";
                    break;
                default:
                    pattern = "yyyy-MM-dd";
                    break;
            }
        }
        if (TextUtils.isEmpty(endDate)) {
            endDate = getStandardDate(pattern);
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(fromDate).after(sdf.parse(endDate));
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 比较日期先后 fromDate 与 endDate 先后顺序 int < 0 if this Date is less than the
     * specified Date, 0 if they are equal, and an int > 0 if this Date is
     * greater.
     *
     * @param fromDate
     * @param endDate  如果为空 默认为今天
     * @return
     */
    public int judgeDate(@NonNull String fromDate, String endDate, String pattern) {
        if (TextUtils.isEmpty(pattern)) {
            int length = fromDate.length();
            switch (length) {
                case 7:
                    pattern = "yyyy-MM";
                    break;
                case 10:
                    pattern = "yyyy-MM-dd";
                    break;
                case 16:
                    pattern = "yyyy-MM-dd HH:mm";
                    break;
                case 19:
                    pattern = "yyyy-MM-dd HH:mm:ss";
                    break;
                default:
                    pattern = "yyyy-MM-dd";
                    break;
            }
        }
        if (TextUtils.isEmpty(endDate)) {
            endDate = getStandardDate(pattern);
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(fromDate).compareTo(sdf.parse(endDate));
        } catch (ParseException e) {
            e.printStackTrace();
            return 1;
        }
    }

    public boolean isSameDay(@NonNull String day1, @NonNull String day2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = sdf.parse(day1);
            Date date2 = sdf.parse(day2);
            return date1.compareTo(date2) == 0 ? true : false;
        } catch (ParseException e) {
            if (day1.length() >= 10 && day2.length() >= 10) {
                return day1.substring(0, 10).compareTo(day1.substring(0, 10)) == 0 ? true : false;
            } else {
                return day1.compareTo(day2) == 0 ? true : false;
            }
        }
    }

    /**
     * @param date         日期时间字符串
     * @param sourceFormat 原始日期格式
     * @param destFormat   目标日期格式
     * @return
     */
    public String changeDateFormat(String date, @NonNull String sourceFormat, @NonNull String destFormat) {
        if (StringUtils.isEmpty(date)) return "";
        if (TextUtils.isEmpty(sourceFormat) || TextUtils.isEmpty(destFormat)) {
            return date;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(sourceFormat);

        try {
            Date date2 = sdf.parse(date);
            sdf = new SimpleDateFormat(destFormat);
            return sdf.format(date2);
        } catch (ParseException e) {

        }
        return date;

    }

    /**
     * 获取时间日期字符串的年月日时分
     *
     * @param dateStr
     * @param pattern 格式如果不确定 可传空null 会根据dateStr的长度默认设置一个
     * @return
     */
    @Nullable
    public int[] getTimeCalendarType(@NonNull String dateStr, String pattern) {
        if (TextUtils.isEmpty(dateStr)) {
            return null;
        }
        if (TextUtils.isEmpty(pattern)) {
            switch (dateStr.length()) {
                case 10:
                    pattern = "yyyy-MM-dd";
                    break;
                case 16:
                    pattern = "yyyy-MM-dd HH:mm";
                    break;
                case 19:
                    pattern = "yyyy-MM-dd HH:mm:ss";
                    break;

                default:
                    pattern = "yyyy-MM-dd";
                    break;
            }

        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            Date date = sdf.parse(dateStr);
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            int[] time = new int[5];
            time[0] = ca.get(Calendar.YEAR);
            time[1] = ca.get(Calendar.MONTH);
            time[2] = ca.get(Calendar.DATE);
            time[3] = ca.get(Calendar.HOUR_OF_DAY);
            time[4] = ca.get(Calendar.MINUTE);
            return time;
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 取出日期 月份 格式化成 MM/dd
     *
     * @param time
     * @param module
     * @return
     */
    @NonNull
    public String getFormatDate(@NonNull String time, String module) {
        String date = time.substring(8, 10);
        String month = time.substring(5, 7);
        return month + "/" + date;
        // return getFormatDate(date, month, module);
    }

    /**
     * 格式化时间串
     *
     * @param date
     * @return
     */
    public String getFormateTime(@Nullable Date date, @Nullable String format) {
        if (format == null || format.equals("")) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        try {
            if (date == null) {
                return "";
            }
            DateFormat df = new SimpleDateFormat(format);
            return df.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 时间日期字符串格式化
     *
     * @param date   纯数字
     * @param month  纯数字 格式有误 直接返回当前时间
     * @param module 格式有误 即为默认格式
     * @return
     */
    public String getFormatDate(String date, String month, String module) {
        if (TextUtils.isEmpty(module)) {
            module = "MM.dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(module);
        Calendar ca = Calendar.getInstance();
        if (StringUtils.pureNum(date) && StringUtils.pureNum(month)) {
            ca.set(Calendar.MONTH, Integer.valueOf(month) - 1);
            ca.set(Calendar.DAY_OF_MONTH, Integer.valueOf(date));
            return sdf.format(ca.getTime());
        } else {
            return sdf.format(ca.getTime());
        }

    }

    /**
     * 将 日期字符串 转换成 long毫秒值
     *
     * @param dateStr
     * @param pattern
     * @return
     */
    public long dateStr2Long(@NonNull String dateStr, String pattern) {
        int result = 0;

        if (TextUtils.isEmpty(dateStr)) {
            return result;
        }
        if (TextUtils.isEmpty(pattern)) {
            switch (dateStr.length()) {
                case 10:
                    pattern = "yyyy-MM-dd";
                    break;
                case 16: {
                    pattern = "yyyy-MM-dd HH:mm";
                }
                break;
                case 19: {
                    pattern = "yyyy-MM-dd HH:mm:ss";
                }
                break;
                default:
                    pattern = "yyyy-MM-dd HH:mm:ss";
                    break;
            }
        }
        if (dateStr.length() > pattern.length()) {
            return result;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date;
        try {
            date = sdf.parse(dateStr);
        } catch (Exception e) {
            date = new Date();
        }
        return date.getTime();
    }

    /**
     * 判断时间 是否今天 时间格式 yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public boolean isToday(@NonNull String date) {
        return getStandardDate("yyyy-MM-dd").compareTo(date) == 0;
    }

    public String hourAdd(@NonNull String hour) {
        String[] split = hour.split(":");
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(split[0]));
        canlendar.set(Calendar.MINUTE, Integer.valueOf(split[1]));
        canlendar.add(Calendar.HOUR_OF_DAY, 1); // 日期减 如果不够减会将月变动
        SimpleDateFormat sdfd = new SimpleDateFormat("HH:mm");
        return sdfd.format(canlendar.getTime());
    }

    public String dateAddFrom(int days, String fromDate, String module, int type) {
        return dateAdd(days, fromDate, module, type);
    }

    /**
     * 将从指定日期加减n天数。 如传入整型-5 意为将当前日期减去5天的日期 如传入整型5 意为将当前日期加上5天后的日期 返回字串 默认格式
     * yyyy-MM-dd 默认日期 当前日期
     *
     * @param days
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public String dateAdd(int days) {
        return dateAdd(days, null, null, Calendar.DATE);
    }

    public String minuteAdd(int minutes, String fromTime) {
        return dateAdd(minutes, fromTime, "HH:mm", Calendar.MINUTE);
    }

    public String minuteAdd(int minutes, String time, String pattern) {
        return dateAdd(minutes, time, pattern, Calendar.MINUTE);
    }

    /**
     * 将当前日期加减n天。 如传入整型-5 意为将当前日期减去5天的日期 如传入整型5 意为将当前日期加上5天后的日期 返回字串 默认格式
     * yyyy-MM-dd
     *
     * @param days
     * @param module 时间格式)可以没有
     * @return
     * @type 要操作的时间类型 如 Calendar.Day
     */
    @SuppressLint("SimpleDateFormat")
    public String dateAdd(int days, String fromDate, String module, int type) {
        if (type == 0) {
            type = Calendar.DAY_OF_MONTH;
        }
        if (TextUtils.isEmpty(module)) {
            module = "yyyy-MM-dd";
        }
        // 日期处理模块 (将日期加上某些天或减去天数)返回字符串
        Calendar canlendar = Calendar.getInstance(); // java.util包
        if (!TextUtils.isEmpty(fromDate)) {
            canlendar.setTime(strToDate(fromDate, module));
        }
        canlendar.add(type, days); // 日期减 如果不够减会将月变动
        SimpleDateFormat sdfd = new SimpleDateFormat(module);
        return sdfd.format(canlendar.getTime());
    }

    /**
     * 将Date转换为指定格式的字符串
     *
     * @param dateDate
     * @return
     */
    public String dateToStr(Date dateDate, String module) {
        if (TextUtils.isEmpty(module)) {
            module = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(module);
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 将时间字符串转换成指定格式的 util.Date
     *
     * @param dateStr
     * @param module  默认格式yyyy-MM-dd
     * @return
     */
    @Nullable
    public Date strToDate(String dateStr, String module) {
        if (TextUtils.isEmpty(module)) {
            module = "yyyy-MM-dd";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(module);
        Date date = null;
        try {
            date = formatter.parse(dateStr);
        } catch (ParseException e) {
            date = new Date();
        }
        return date;
    }

    /**
     * 返回标准时间日期 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public String getStandardDate(@NonNull String module) {
        return new SimpleDateFormat(module).format(new Date());
    }

    /**
     * 格式化 回复时间只取到分钟数
     *
     * @param replyTime
     * @return
     */
    @NonNull
    public String formatReplyTime2(@NonNull String replyTime) {
        if (TextUtils.isEmpty(replyTime)) {
            return "";
        }
        if (replyTime.length() > 16) {
            return replyTime.substring(0, 16);
        }
        return replyTime;
    }

    /**
     * 格式化时间 取得小时:分钟 HH:mm
     *
     * @param time
     * @return
     */
    @NonNull
    public String formatTime(@NonNull String time) {
        if (TextUtils.isEmpty(time)) {
            return "";
        }
        if (timeMatcher(time, "HH:mm")) {
            return time;
        }

        if (time.length() >= 16) {
            return time.substring(11, 16);
        }
        return time;
    }

    /**
     * 时间格式化 只取年月日
     *
     * @param date
     * @return
     */
    @NonNull
    public String formatDate2(@NonNull String date) {
        if (TextUtils.isEmpty(date)) {
            return "";
        }
        if (date.length() > 10) {
            return date.substring(0, 10);
        }
        return date;
    }

    private boolean timeMatcher(@NonNull String time, @NonNull String pattern) {
        Pattern p = Pattern.compile(pattern);
        Matcher matcher = p.matcher(time);
        return matcher.matches();
    }

    /**
     * 获取指定类型的日期数据
     *
     * @param date   时间日期
     * @param type   年、月、日、时、分、秒 类型 Calendar.YEAR Calendar.MONTH。。。。。
     * @param module 时间日期格式
     * @return
     */
    public int getTimeValueByCalendarType(@NonNull String date, int type, String module) {
        int result = 0;

        if (TextUtils.isEmpty(date)) {
            return result;
        }
        if (TextUtils.isEmpty(module)) {
            switch (date.length()) {
                case 10:
                    module = "yyyy-MM-dd";
                    break;
                case 16: {
                    module = "yyyy-MM-dd HH:mm";
                }
                break;
                case 19: {
                    module = "yyyy-MM-dd HH:mm:ss";
                }
                break;
                default:
                    module = "yyyy-MM-dd HH:mm:ss";
                    break;
            }
        }
        if (date.length() > module.length()) {
            return result;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(module);
        try {
            Date date2 = sdf.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date2);
            if (type == Calendar.MONTH) {
                return calendar.get(type) + 1;
            } else {
                return calendar.get(type);
            }

        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 将 时长 转化成 对应 格式的日期字符串
     *
     * @param recordTimeLength 时长
     * @param calendarType     日期类型
     * @param pattern          欲转换的格式
     */
    public String getTimeByIntValue(long recordTimeLength, int calendarType, @NonNull String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            long time = recordTimeLength;
            switch (calendarType) {
                case Calendar.HOUR_OF_DAY:
                    time = recordTimeLength * 60 * 60 * 1000;
                    break;
                case Calendar.MINUTE:
                    time = recordTimeLength * 60 * 1000;
                    break;
                case Calendar.SECOND:
                    time = recordTimeLength * 1000;
                    break;
                default:
                    break;
            }
            Date date = new Date(time);
            return sdf.format(date);
        } catch (Exception e) {

        }
        return "";
    }

    /**
     * 将毫秒转化成日期 最多只能到天
     *
     * @param millis
     * @param type   只能是 1到4 分别对应 4种不同时间格式 ss、mm:ss、HH:mm:ss、dd HH:mm:ss
     * @return
     */
    public static String setMilltoTime(long millis, int type) {
        String format = null;
        long hourDiference = 28800000;
        long dayDiference = 86800000;
        if (millis > dayDiference) {
            type = 4;
        }
        switch (type) {
            case 1:
                format = "ss";
                break;
            case 2:
                format = "mm:ss";
                break;
            case 3:
                format = "HH:mm:ss";
                break;
            case 4:
                format = "dd HH:mm:ss";
                break;
            default:
                break;
        }
        if (format == null) {
            throw new IllegalArgumentException("the value of type is illegal");
        }

        millis -= hourDiference;
        if (type == 4) {
            millis -= dayDiference;
        }
        DateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return formatter.format(calendar.getTime());
    }

    /**
     * 查询 date 日期是周几
     * 注意顺序 日 一 二 。。。六
     * 分别对应 1 ---- 7
     *
     * @param date
     * @return
     */
    public int getDayOfWeek(String date) {
        if (date == null || date.length() == 0) return 0;
        try {
            String pattern = "yyyy-MM-dd";
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
            Date date1 = sdf.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            return calendar.get(Calendar.DAY_OF_WEEK);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 判断日期对应星期几
     *
     * @param date
     * @return
     */
    public String getWeekDay(String date) {
        String result = "";
        int dayOfWeek = getDayOfWeek(date);
        switch (dayOfWeek) {
            case 1:
                result = "周日";
                break;
            case 2:
                result = "周一";
                break;
            case 3:
                result = "周二";
                break;
            case 4:
                result = "周三";
                break;
            case 5:
                result = "周四";
                break;
            case 6:
                result = "周五";
                break;
            case 7:
                result = "周六";
                break;
        }
        return result;
    }

    /**
     * 日期与今天对比
     * 返回 昨天 今天 明天
     *
     * @return
     */
    public String formatTime3(String time, boolean isShowToday) {
        int i = judgeDate(time, null, "yyyy-MM-dd");
        String result;
        if (i == 0) {
            result = isShowToday ? "今天" : "";
        } else if (i == 1) {
            result = "明天";
        } else if (i == -1) {
            result = "昨天";
        } else {
            if (i < 0) {
                result = String.format("%d天前", Math.abs(i));
            } else result = String.format("%d天后", i);
        }
        return result;
    }

}