喻健
2018-12-25 95654e5a5a1d0c4384cf44a1510c670ec804abc7
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
package org.yeshi.utils;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class DateUtil {
 
    public static String dateDiff(String startTime, String endTime) {
 
        String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟";
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 
        try {
            long nm = 1000 * 60;// 一分钟的毫秒数
            long nh = 1000 * 60 * 60;// 一小时的毫秒数
            long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
 
            // 获得两个时间的毫秒时间差异
            long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
 
            long day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
 
            datatime = day + "天" + hour + "小时" + min + "分钟";
 
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
        return datatime;
    }
 
    public static String dateDiff2(Date startTime, Date endTime) throws Exception {
 
        String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟";
 
        long nm = 1000 * 60;// 一分钟的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
 
        // 获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
 
        long day = diff / nd;// 计算差多少天
        long hour = diff % nd / nh;// 计算差多少小时
        long min = diff % nd % nh / nm;// 计算差多少分钟
 
        datatime = day + "天" + hour + "小时" + min + "分钟";
 
        return datatime;
    }
 
    public String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(millSec);
        return sdf.format(date);
    }
 
    /**
     * 验证是否属于同一天
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            return isSameDay(cal1, cal2);
        } else {
            return false;
        }
    }
 
    /**
     * 验证是否属于同一天
     * 
     * @param cal1
     * @param cal2
     * @return
     */
    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if (cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            return false;
        }
    }
 
    /**
     * 指定日期加上天数后的日期
     * 
     * @param num
     *            为增加的天数
     * @param nowDate
     *            创建时间
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num, String nowDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date currdate = format.parse(nowDate);
    
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, num);
        
        return format.format(ca.getTime());
    }
}