kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
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
package com.nanometer.smartlab.util;
 
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
 
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 
/**
 * Created by johnny on 15/9/9.
 */
@Component
@Scope("singleton")
public class Utils {
 
    public final static String[] DATE_FORMAT = new String[]{"yyyyMMdd", "yyyy-MM-dd", "yyyy/MM/dd","yyyyMMddHHmmss","yyyy-MM-dd HH:mm:ss","yyyy/MM/dd HH:mm:ss"};
 
    public static Timestamp now() {
        return new Timestamp(System.currentTimeMillis());
    }
 
    public static String now(String format) {
        try {
            if (StringUtils.isBlank(format)) {
                format = Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS;
            }
 
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(new Timestamp(System.currentTimeMillis()));
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public static String formatDateToString(Date date, String pattern) {
        if (date != null) {
            return formatTimestampToString(new Timestamp(date.getTime()), pattern);
        }
 
        return null;
    }
 
    public static String formatTimestampToString(Timestamp time, String pattern) {
        try {
            if (time == null) {
                return null;
            }
            if (StringUtils.isBlank(pattern)) {
                pattern = Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS;
            }
 
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public static Timestamp formatStringToTimestamp(String time, String pattern) {
        try {
            if (StringUtils.isBlank(time)) {
                return null;
            }
            if (StringUtils.isBlank(pattern)) {
                pattern = Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS;
            }
 
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return new Timestamp(sdf.parse(time).getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public static Timestamp formatStartTime(Timestamp time) {
        try {
            if (time == null) {
                return null;
            }
 
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(time);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return new Timestamp(calendar.getTimeInMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
    public static Timestamp formatEndTime(Timestamp time) {
        try {
            if (time == null) {
                return null;
            }
 
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(time);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            return new Timestamp(calendar.getTimeInMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public static int getBetweenDay(Timestamp time1, Timestamp time2) {
        Calendar d1 = new GregorianCalendar();
        d1.setTime(time1);
        Calendar d2 = new GregorianCalendar();
        d2.setTime(time2);
        if (d1.after(d2)) {
            d1.setTime(time2);
            d2.setTime(time1);
        }
        int days = d2.get(Calendar.DAY_OF_YEAR)- d1.get(Calendar.DAY_OF_YEAR);
        int y2 = d2.get(Calendar.YEAR);
        if (d1.get(Calendar.YEAR) != y2) {
            do {
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        }
        return days;
    }
 
    public static int getBetweenMonth(Timestamp time1, Timestamp time2) {
        Calendar d1 = new GregorianCalendar();
        d1.setTime(time1);
        Calendar d2 = new GregorianCalendar();
        d2.setTime(time2);
        if (d1.after(d2)) {
            d1.setTime(time2);
            d2.setTime(time1);
        }
 
        int months = d2.get(Calendar.MONTH) - d1.get(Calendar.MONTH);
        int y2 = d2.get(Calendar.YEAR);
        if (d1.get(Calendar.YEAR) != y2) {
            do {
                months += d1.getActualMaximum(Calendar.MONTH) + 1;
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        }
        return months;
    }
 
    public static Timestamp addTimeBySecond(Timestamp time, int second) {
        return addTime(time, Calendar.SECOND, second);
    }
 
    public static Timestamp subTimeBySecond(Timestamp time, int second) {
        return subTime(time, Calendar.SECOND, second);
    }
 
    public static Timestamp subTimeByMonth(Timestamp time, int month) {
        return subTime(time, Calendar.MONTH, month);
    }
 
    public static Timestamp addTimeByYear(Timestamp time, int year) {
        return addTime(time, Calendar.YEAR, year);
    }
    public static Timestamp subTimeByYear(Timestamp time, int year) {
        return subTime(time, Calendar.YEAR, year);
    }
 
    private static Timestamp addTime(Timestamp time, int type, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time.getTime());
        calendar.add(type, value);
 
        return new Timestamp(calendar.getTimeInMillis());
    }
 
    private static Timestamp subTime(Timestamp time, int type, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time.getTime());
        calendar.add(type, -value);
 
        return new Timestamp(calendar.getTimeInMillis());
    }
 
    public static String getWeekMonday(String format){
        SimpleDateFormat formater=new SimpleDateFormat(format);
        Calendar cal=new GregorianCalendar();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        Date first=cal.getTime();
        return formater.format(first);
    }
 
}