WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
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
package tech.powerjob.server.core.scheduler.auxiliary;
 
import java.io.Serializable;
 
 
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
 
/**
 * Represents a time in hour, minute and second of any given day.
 *
 * <p>The hour is in 24-hour convention, meaning values are from 0 to 23.</p>
 * <a href="https://github.com/quartz-scheduler/quartz">PowerJob learn from quartz</a>
 *
 * @since 2.0.3
 *
 * @author James House
 * @author Zemian Deng <saltnlight5@gmail.com>
 */
public class TimeOfDay implements Serializable {
 
    private static final long serialVersionUID = 2964774315889061771L;
 
    private final int hour;
    private final int minute;
    private final int second;
 
    /**
     * Create a TimeOfDay instance for the given hour, minute and second.
     *
     * @param hour The hour of day, between 0 and 23.
     * @param minute The minute of the hour, between 0 and 59.
     * @param second The second of the minute, between 0 and 59.
     * @throws IllegalArgumentException if one or more of the input values is out of their valid range.
     */
    public TimeOfDay(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
        validate();
    }
 
    /**
     * Create a TimeOfDay instance for the given hour and minute (at the zero second of the minute).
     *
     * @param hour The hour of day, between 0 and 23.
     * @param minute The minute of the hour, between 0 and 59.
     * @throws IllegalArgumentException if one or more of the input values is out of their valid range.
     */
    public TimeOfDay(int hour, int minute) {
        this.hour = hour;
        this.minute = minute;
        this.second = 0;
        validate();
    }
 
    private void validate() {
        if(hour < 0 || hour > 23)
            throw new IllegalArgumentException("Hour must be from 0 to 23");
        if(minute < 0 || minute > 59)
            throw new IllegalArgumentException("Minute must be from 0 to 59");
        if(second < 0 || second > 59)
            throw new IllegalArgumentException("Second must be from 0 to 59");
    }
 
    /**
     * Create a TimeOfDay instance for the given hour, minute and second.
     *
     * @param hour The hour of day, between 0 and 23.
     * @param minute The minute of the hour, between 0 and 59.
     * @param second The second of the minute, between 0 and 59.
     * @throws IllegalArgumentException if one or more of the input values is out of their valid range.
     */
    public static TimeOfDay hourMinuteAndSecondOfDay(int hour, int minute, int second) {
        return new TimeOfDay(hour, minute, second);
    }
 
    /**
     * Create a TimeOfDay instance for the given hour and minute (at the zero second of the minute).
     *
     * @param hour The hour of day, between 0 and 23.
     * @param minute The minute of the hour, between 0 and 59.
     * @throws IllegalArgumentException if one or more of the input values is out of their valid range.
     */
    public static TimeOfDay hourAndMinuteOfDay(int hour, int minute) {
        return new TimeOfDay(hour, minute);
    }
 
    /**
     * The hour of the day (between 0 and 23).
     *
     * @return The hour of the day (between 0 and 23).
     */
    public int getHour() {
        return hour;
    }
 
    /**
     * The minute of the hour.
     *
     * @return The minute of the hour (between 0 and 59).
     */
    public int getMinute() {
        return minute;
    }
 
    /**
     * The second of the minute.
     *
     * @return The second of the minute (between 0 and 59).
     */
    public int getSecond() {
        return second;
    }
 
    /**
     * Determine with this time of day is before the given time of day.
     *
     * @return true this time of day is before the given time of day.
     */
    public boolean before(TimeOfDay timeOfDay) {
 
        if(timeOfDay.hour > hour)
            return true;
        if(timeOfDay.hour < hour)
            return false;
 
        if(timeOfDay.minute > minute)
            return true;
        if(timeOfDay.minute < minute)
            return false;
 
        if(timeOfDay.second > second)
            return true;
        if(timeOfDay.second < second)
            return false;
 
        return false; // must be equal...
    }
 
    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof TimeOfDay))
            return false;
 
        TimeOfDay other = (TimeOfDay)obj;
 
        return (other.hour == hour && other.minute == minute && other.second == second);
    }
 
    @Override
    public int hashCode() {
        return (hour + 1) ^ (minute + 1) ^ (second + 1);
    }
 
    /** Return a date with time of day reset to this object values. The millisecond value will be zero. */
    public Date getTimeOfDayForDate(Date dateTime) {
        if (dateTime == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateTime);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, second);
        cal.clear(Calendar.MILLISECOND);
        return cal.getTime();
    }
 
    /**
     * Create a TimeOfDay from the given date, in the system default TimeZone.
     *
     * @param dateTime The java.util.Date from which to extract Hour, Minute and Second.
     */
    public static TimeOfDay hourAndMinuteAndSecondFromDate(Date dateTime) {
        return hourAndMinuteAndSecondFromDate(dateTime, null);
    }
 
    /**
     * Create a TimeOfDay from the given date, in the given TimeZone.
     *
     * @param dateTime The java.util.Date from which to extract Hour, Minute and Second.
     * @param tz The TimeZone from which relate Hour, Minute and Second for the given date.  If null, system default
     * TimeZone will be used.
     */
    public static TimeOfDay hourAndMinuteAndSecondFromDate(Date dateTime, TimeZone tz) {
        if (dateTime == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateTime);
        if(tz != null)
            cal.setTimeZone(tz);
 
        return new TimeOfDay(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
    }
 
    /**
     * Create a TimeOfDay from the given date (at the zero-second), in the system default TimeZone.
     *
     * @param dateTime The java.util.Date from which to extract Hour and Minute.
     */
    public static TimeOfDay hourAndMinuteFromDate(Date dateTime) {
        return hourAndMinuteFromDate(dateTime, null);
    }
 
    /**
     * Create a TimeOfDay from the given date (at the zero-second), in the system default TimeZone.
     *
     * @param dateTime The java.util.Date from which to extract Hour and Minute.
     * @param tz The TimeZone from which relate Hour and Minute for the given date.  If null, system default
     * TimeZone will be used.
     */
    public static TimeOfDay hourAndMinuteFromDate(Date dateTime, TimeZone tz) {
        if (dateTime == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateTime);
        if(tz != null)
            cal.setTimeZone(tz);
 
        return new TimeOfDay(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
    }
 
    public static TimeOfDay from(String hms) {
        String[] split = hms.split(":");
        if (split.length != 3) {
            throw new IllegalArgumentException("invalid TimeOfDay, make pattern like 15:30:10");
        }
        return new TimeOfDay(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
    }
 
    @Override
    public String toString() {
        return "TimeOfDay[" + hour + ":" + minute + ":" + second + "]";
    }
}