wl
2022-11-11 e1008dbb1fa76874a28c06913b95c16d18acdfa7
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
package com.ycl.controller.video.common.util;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 
 
public class DateUtils {
 
    /**
     * 获取两日期之间间隔的天数
     * 
     * @param smdate
     *            较小的时间
     * @param bdate
     *            较大的时间
     * @return 相差天数
     */
    public static int daysBetween(Date smdate, Date bdate) {
        long between_days = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            between_days = (time2 - time1) / (1000 * 3600 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
        return Integer.parseInt(String.valueOf(between_days));
    }
 
    public static boolean isBetween(Date date, Date startDate, Date endDate) {
 
        try {
 
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            startDate = sdf.parse(sdf.format(startDate));
            endDate = sdf.parse(sdf.format(endDate));
            date = sdf.parse(sdf.format(date));
 
            Calendar cal = Calendar.getInstance();
 
            cal.setTime(date);
            long time0 = cal.getTimeInMillis();
            cal.setTime(startDate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(endDate);
            long time2 = cal.getTimeInMillis();
 
            if (time0 > time1 && time0 <= time2)
                return true;
 
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
 
    }
 
    public static boolean isToday(Date date) {
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String _date = sdf.format(date);
        String _today = sdf.format(new Date());
 
        return _date.equals(_today);
    }
 
    public static long DifferMillis(Date sDate, Date eDate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sDate = sdf.parse(sdf.format(sDate));
            eDate = sdf.parse(sdf.format(eDate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(sDate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(eDate);
            long time2 = cal.getTimeInMillis();
            return Math.abs(time1 - time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
        return -1L;
    }
 
    public static long DifferSeconds(Date sDate, Date eDate) {
 
        return DifferMillis(sDate, eDate) / 1000;
    }
 
    /**
     * 相差分钟数
     * 
     * @param sDate
     * @param eDate
     * @return
     */
    public static long DifferMinutes(Date sDate, Date eDate) {
 
        return Long.parseLong(String.valueOf(DifferMillis(sDate, eDate)
                / (1000 * 60)));
    }
 
    public static String getNowDate(String format) {
        return new SimpleDateFormat(format).format(new Date());
    }
 
    public static String getTomorrowDate(String format) {
        return new SimpleDateFormat(format).format(addDate(new Date(), 1));
    }
 
    public static String getDateString(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }
 
    public static String getNow() {
        return getDateString(new Date(), DateUtils.DATE_TIME_PATTERN);
    }
    public static Date parse(String date, String format) {
 
        try {
            return new SimpleDateFormat(format).parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
 
    public static boolean nowTimeBetween(String beginTimeStr, String endTimeStr)
            throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
        Long beginTime = dateFormat.parse(beginTimeStr).getTime();
        Long endTime = dateFormat.parse(endTimeStr).getTime();
        Long nowTime = dateFormat.parse(dateFormat.format(new Date()))
                .getTime();
        if (nowTime >= beginTime && nowTime <= endTime) {
            return true;
        }
        return false;
    }
 
    @SuppressWarnings("static-access")
    public static Date addDate(Date date, int days) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, days);// 把日期往后增加一天.整数往后推,负数往前移动
        return calendar.getTime(); // 这个时间就是日期往后推一天的结果
    }
 
    @SuppressWarnings("static-access")
    public static Date addHour(Date date, int hour) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.HOUR, hour);// 把日期往后增加一天.整数往后推,负数往前移动
        return calendar.getTime(); // 这个时间就是日期往后推一天的结果
    }
 
    @SuppressWarnings("static-access")
    public static Date addMinute(Date date, int minute) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MINUTE, minute);// 把日期往后增加一天.整数往后推,负数往前移动
        return calendar.getTime(); // 这个时间就是日期往后推一天的结果
    }
 
    @SuppressWarnings("static-access")
    public static Date addSecond(Date date, int second) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.SECOND, second);// 把日期往后增加一天.整数往后推,负数往前移动
        return calendar.getTime(); // 这个时间就是日期往后推一天的结果
    }
 
    /**
     * 
     * @param timestamp
     *            秒
     * @return
     */
    public static Date getDate(String timestamp) {
        return getDateBySecond(Long.parseLong(timestamp)); // 这个时间就是日期往后推一天的结果
    }
 
    /**
     * 
     * @param timestamp
     *            毫秒
     * @return
     */
    public static Date getDateByMillis(long timestamp) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(timestamp);
        return calendar.getTime(); // 这个时间就是日期往后推一天的结果
    }
 
    /**
     * 
     * @param timestamp
     *            秒
     * @return
     */
    public static Date getDateBySecond(long timestamp) {
        return getDateByMillis(timestamp * 1000); // 这个时间就是日期往后推一天的结果
    }
 
    public static long getTimestamp(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar.getTimeInMillis(); // 这个时间就是日期往后推一天的结果
    }
 
    public static long DifferMillis(String timestamp, String nowTimestamp) {
        Date date1 = parse(timestamp, DATE_TIME_PATTERN);
        Date date2 = parse(nowTimestamp, DATE_TIME_PATTERN);
 
        return DifferMillis(date1, date2);
    }
 
    public static long DifferMillis(String timestamp, Date date) {
        Date date1 = parse(timestamp, DATE_TIME_PATTERN);
 
        return DifferMillis(date1, date);
    }
    public static String dateToStamp(String s) throws ParseException{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }
    public static void main(String[] args) throws ParseException {
        System.out.println(DateUtils.getDateString(new Date(), "yyyy-MM-dd HH:mm:ss"));
    }
    
 
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
}