#
Junjie
1 天以前 536e17a446c170a8b214aaf188b98817ee6258c1
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
package com.core.common;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
 
public class DateUtils {
 
    public static final String yyyyMMdd_C = "yyyy年MM月dd日";
    public static final String yyyyMM_F = "yyyy-MM";
    public static final String yyyyMMdd_F = "yyyy-MM-dd";
    public static final String yyyyMMddHHmmss_F = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyyMMddHHmmsssss_F = "yyyy-MM-dd HH:mm:ss:SSS";
    public static final String yyyy = "yyyy";
    public static final String yyyyMM = "yyyyMM";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String yyyyMMddHH = "yyyyMMddHH";
    public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String YYMMDDHHMMSS = "yyMMddHHmmss";
    public static final String yyyyMMddHHmmsssss = "yyyyMMddHHmmssSSS";
 
    public static String convert(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }
 
    public static String convert(Date date) {
        return convert(date, yyyyMMddHHmmss_F);
    }
 
    public static Date convert(String text, String format) {
        if (text.length() < format.length()) {
            throw new RuntimeException("时间解析失败 ==>>" + text);
        }
        if (text.length() > format.length()) {
            text = text.substring(0, format.length());
        }
        try {
            return new SimpleDateFormat(format).parse(text);
        } catch (ParseException e) {
            throw new RuntimeException("时间解析失败 ==>>" + text);
        }
    }
 
    public static Date convert(String text) {
        return convert(text, yyyyMMddHHmmss_F);
    }
 
    public static int diff(Date start, Date end) {
        return getDaysByTimestamp(Math.abs(end.getTime() - start.getTime()));
    }
 
    public static long diffToMinute(Date start, Date end) {
        return Math.abs(end.getTime() - start.getTime()) / 1000L / 60L;
    }
 
    public static long diffToSeconds(Date start, Date end) {
        return Math.abs(end.getTime() - start.getTime()) / 1000L;
    }
 
    private static int getDaysByTimestamp(long timestamp) {
        double days = Arith.divides(2, timestamp, 86400000);
        int value = (int) days;
        if (days > value) {
            return value + 1;
        }
        return value;
    }
 
    public static int diffToNow(Date date) {
        long val = new Date().getTime() - date.getTime();
        return (int) (Math.abs(val) / 1000L);
    }
 
    public static String createTimeStamp() {
        return Long.toString(System.currentTimeMillis() / 1000L);
    }
 
    public static Date calculate(Date date, Long amount, TimeUnit unit, boolean before) {
        if (Objects.isNull(date) || Objects.isNull(amount) || Objects.isNull(unit)) {
            return null;
        }
        return new Date(before ? date.getTime() - unit.toMillis(amount) : date.getTime() + unit.toMillis(amount));
    }
 
    public static Date calculate(Date date, Long amount, TimeUnit unit) {
        return calculate(date, amount, unit, false);
    }
 
    public static DateEntity getDateEntity(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        DateEntity entity = new DateEntity();
        entity.setYear(calendar.get(Calendar.YEAR));
        entity.setMonth(calendar.get(Calendar.MONTH));
        entity.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        entity.setHour(calendar.get(Calendar.HOUR_OF_DAY));
        entity.setMinute(calendar.get(Calendar.MINUTE));
        entity.setSecond(calendar.get(Calendar.SECOND));
        return entity;
    }
 
    public static class DateEntity {
        private int year;
        private int month;
        private int day;
        private int hour;
        private int minute;
        private int second;
 
        public int getYear() {
            return year;
        }
 
        public void setYear(int year) {
            this.year = year;
        }
 
        public int getMonth() {
            return month;
        }
 
        public void setMonth(int month) {
            this.month = month;
        }
 
        public int getDay() {
            return day;
        }
 
        public void setDay(int day) {
            this.day = day;
        }
 
        public int getHour() {
            return hour;
        }
 
        public void setHour(int hour) {
            this.hour = hour;
        }
 
        public int getMinute() {
            return minute;
        }
 
        public void setMinute(int minute) {
            this.minute = minute;
        }
 
        public int getSecond() {
            return second;
        }
 
        public void setSecond(int second) {
            this.second = second;
        }
    }
}