admin
2020-06-20 0ab8a2ea521a838124f517daf4e61dee971a6d4c
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
package com.ks.tool.bkz.util;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
 
public class TimeUtil {
 
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
    public static String getMonthDay(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd");
        String date = sdf.format(new Date(time));
        return date;
    }
 
    public static String getHistoryTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        String date = sdf.format(new Date(time));
        return date;
    }
 
    public static String getAllTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = sdf.format(new Date(time));
        return date;
    }
 
    public static String getWholeTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date(time));
        return date;
    }
 
    public static String getAllNoTime(long time) {
        // yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        String date = sdf.format(new Date(time));
        return date;
    }
 
    public static String getyyyyMMdd(long time) {
        // yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(new Date(time));
        return date;
    }
 
    public static String getCommonTime(long time) {
        // yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date(time));
        sdf = new SimpleDateFormat("HH:mm");
        String hm = sdf.format(new Date(time));
        return date + "\n" + hm;
    }
 
    public static String getGernalTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date(time));
        return date;
    }
 
    public static String getGernalTime(long time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String date = sdf.format(new Date(time));
        return date;
    }
 
    public static String getExchangeAdTime(long time) {
        // yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        String date = sdf.format(new Date(time));
        return date;
    }
 
    public static long convertDateToTemp(String st) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(st);
            return date.getTime();
        } catch (Exception e) {
             e.printStackTrace();
            return 0;
        }
    }
    
    public static Date parseYYYYMM(String st) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        try {
            return sdf.parse(st);
        } catch (Exception e) {
             e.printStackTrace();
            return null;
        }
    }
    
    public static Date parseYYYYMMDD_HHMMSS(String st) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(st);
        } catch (Exception e) {
             e.printStackTrace();
            return null;
        }
    }
    
    
    public static Date parseYYYYMMDD_HHMM(String st) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            return sdf.parse(st);
        } catch (Exception e) {
             e.printStackTrace();
            return null;
        }
    }
    
    public static Date parseYYYYMMDD(String st) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(st);
        } catch (Exception e) {
             e.printStackTrace();
            return null;
        }
    }
    
    public static Date parseDotYYYYMMDD(String st) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        try {
            return sdf.parse(st);
        } catch (Exception e) {
             e.printStackTrace();
            return null;
        }
    }
    
    public static Date parseDotCommon(String st) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        try {
            return sdf.parse(st);
        } catch (Exception e) {
             e.printStackTrace();
            return null;
        }
    }
    
    public static Date parseDotCommon2(String st) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        try {
            return sdf.parse(st);
        } catch (Exception e) {
             e.printStackTrace();
            return null;
        }
    }
    
    public static long convertAllTimeToTemp(String st) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            date = sdf.parse(st);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    public static String formatDate(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        String date = sdf.format(new Date(time));
        return date;
    }
    
    public static String formatDate(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        String date = sdf.format(time);
        return date;
    }
 
    public static String formatDateDot(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        String date = sdf.format(time);
        return date;
    }
    
    public static String formatHHMM_12H(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
        String date = sdf.format(time);
        return date;
    }
    
    public static String formatYMDHHMM(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    public static String formatDateAddT(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static long convertToTimeTemp(String st, String format) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(st);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
 
    public static long convertDateToTemp1(String st) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            date = sdf.parse(st);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    
 
    public static long convertDateToTemp2(String st) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(st);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
 
    public static boolean isSameDay(String time1, String time2) {
        if (getGernalTime(Long.parseLong(time1)).equalsIgnoreCase(getGernalTime(Long.parseLong(time2))))
            return true;
        else
            return false;
    }
 
    public static Date getNextDay(int day) {
        long current = System.currentTimeMillis();
        long date = current / 86400000 * 86400000 - TimeZone.getDefault().getRawOffset();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(date));
        calendar.add(Calendar.DATE, day);
        int y = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH);
        int d = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(y, m, d);
        return calendar.getTime();
    }
 
    public static Date getNextDay(int day, long curtime) {
        long current = curtime;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(current));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DATE, day);
        int y = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH);
        int d = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(y, m, d);
        return calendar.getTime();
    }
 
    public static List<String> getEmupDate(int days) {
        days = -days + 1;
        List<String> list = new ArrayList<String>();
        for (int ii = days; ii <= 0; ii++) {
            list.add(dateFormat.format(getNextDay(ii)));
        }
        return list;
    }
 
    public static List<String> getEmupDate(int days, long endtime) {
        days = -days;
        List<String> list = new ArrayList<String>();
        for (int ii = days; ii <= 0; ii++) {
            list.add(dateFormat.format(getNextDay(ii, endtime)));
        }
        return list;
    }
 
    public static String getSimpleDate(Date date) {
        String format = dateFormat.format(date);
        return format;
    }
 
    public static String getSimpleDateMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(date);
        return format;
    }
 
    public static String getMonthOnlyMM(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        String format = sdf.format(date);
        return format;
    }
    
    public static String getYearOnlyYYYY(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String format = sdf.format(date);
        return format;
    }
    
    public static int getDayOfMonth() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        return day;
    }
 
    public static Date parse(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(time);
    }
 
    public static String yyyyMMddHHmmss(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = sdf.format(new Date(time));
        return date;
    }
 
    /**
     * 获取两个时间的天数差值
     * 
     * @param from
     * @param to
     * @return
     */
    public static int getDayDifferenceCount(Date from, Date to) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(from);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(to);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) { // 同一年
            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 timeDistance + (day2 - day1);
        } else { // 不同年
            return day2 - day1;
        }
 
    }
}