skyouc
2025-02-17 708a39865b7b0d7a0aae3fad98c282aaa8b93b13
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
package com.vincent.rsf.framework.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;
 
/**
 * 时间工具类
 * Created by vincent on 2019-04-15
 */
public class DateUtils {
 
    public final static String yyyyMMdd_C="yyyy年MM月dd日";
    public final static String yyyyMM_F="yyyy-MM";
    public final static String yyyyMMdd_F="yyyy-MM-dd";
    public final static String yyyyMMddHHmmss_F="yyyy-MM-dd HH:mm:ss";
    public final static String yyyyMMddHHmmsssss_F="yyyy-MM-dd HH:mm:ss,SSS";
    public final static String yyyy="yyyy";
    public final static String yyyyMM="yyyyMM";
    public final static String yyyyMMdd="yyyyMMdd";
    public final static String yyyyMMddHH="yyyyMMddHH";
    public final static String yyyyMMddHHmmss="yyyyMMddHHmmss";
    public final static String YYMMDDHHMMSS="YYMMDDHHMMSS";
    public final static String yyyyMMddHHmmsssss="yyyyMMddHHmmssSSS";
    public final static String yyyyMMddTHHmmsssssZ_F="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
 
    /**
     * date ==>> string
     */
    public static String convert(Date date, String pattern){
        return new SimpleDateFormat(pattern).format(date);
    }
 
    public static String convert(Date date){
        return convert(date, yyyyMMddHHmmss_F);
    }
 
    /**
     * string ==>> date
     */
    public static Date convert(String str, String pattern){
//        if (str.length() < pattern.length()){
//            throw new RuntimeException("时间解析失败 ==>> "+str);
//        }
//        if (str.length() > pattern.length()){
//            str = str.substring(0, pattern.length());
//        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException("时间解析失败 ==>> "+str);
        }
        return date;
    }
 
    public static Date convert(String str){
        return convert(str, yyyyMMddHHmmss_F);
    }
 
    /**
     * 两个date之间相差的天数,不满一天算一天
     */
    public static int diff(Date date1, Date date2){
        return getDaysByTimestamp(Math.abs(date2.getTime() - date1.getTime()));
    }
 
    public static long diffToMinute(Date date1, Date date2){
        return Math.abs(date2.getTime() - date1.getTime())/1000/60;
    }
 
    public static long diffToSeconds(Date date1, Date date2){
        return Math.abs(date2.getTime() - date1.getTime())/1000;
    }
 
    private static int getDaysByTimestamp(long timestamp){
        double daysPoint = Arith.divides(2, timestamp, (1000 * 3600 * 24));
        int daysPoint1 = (int) daysPoint;
        double daysPoint2 = (double) daysPoint1;
        if (daysPoint > daysPoint2){
            return daysPoint1 + 1;
        }
        return daysPoint1;
    }
 
    /**
     * 入参date距离现在的秒数
     */
    public static int diffToNow(Date date){
        long diff = new Date().getTime() - date.getTime();
        return (int) (Math.abs(diff) / 1000);
    }
 
    /**
     * 当前时间戳(单位:秒)
     */
    public static String createTimeStamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
 
    /**
     * 时间计算函数
     * @param date 被计算时间实例
     * @param val 计算值
     * @param timeUnit 计算值单位
     * @param subtraction 减法布尔 true:当前函数为减法计算,false:反之
     * @return 计算结果 Date
     */
    public static Date calculate(Date date, Long val, TimeUnit timeUnit, boolean subtraction){
        if (Objects.isNull(date) || Objects.isNull(val) || Objects.isNull(timeUnit)){
            return null;
        }
        return new Date(subtraction?date.getTime()-timeUnit.toMillis(val):date.getTime()+timeUnit.toMillis(val));
    }
 
    public static Date calculate(Date date, Long val, TimeUnit timeUnit){
        return calculate(date, val, timeUnit, false);
    }
 
    /**
     * 时间对象DateEntity
     */
    public static DateEntity getDateEntity(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        DateEntity dateEntity = new DateEntity();
        dateEntity.setYear(cal.get(Calendar.YEAR));
        dateEntity.setMonth(cal.get(Calendar.MONTH));
        dateEntity.setDay(cal.get(Calendar.DATE));
        dateEntity.setHour(cal.get(Calendar.HOUR_OF_DAY));
        dateEntity.setMinute(cal.get(Calendar.MINUTE));
        dateEntity.setSecond(cal.get(Calendar.SECOND));
        return dateEntity;
    }
 
    static class DateEntity {
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;
 
        public int getYear() {
            return year;
        }
 
        public void setYear(final int year) {
            this.year = year;
        }
 
        public int getMonth() {
            return month;
        }
 
        public void setMonth(final int month) {
            this.month = month + 1;
        }
 
        public int getDay() {
            return day;
        }
 
        public void setDay(final int day) {
            this.day = day;
        }
 
        public int getHour() {
            return hour;
        }
 
        public void setHour(final int hour) {
            this.hour = hour;
        }
 
        public int getMinute() {
            return minute;
        }
 
        public void setMinute(final int minute) {
            this.minute = minute;
        }
 
        public int getSecond() {
            return second;
        }
 
        public void setSecond(final int second) {
            this.second = second;
        }
    }
}