forked from greenrobot/essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateUtils.java
118 lines (99 loc) · 4.52 KB
/
DateUtils.java
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
/*
* Copyright (C) 2014 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.greenrobot.common;
import java.util.Calendar;
/**
* Simple Date and time utils.
*/
public class DateUtils {
/**
* Calendar objects are rather expensive: for heavy usage it's a good idea to use a single instance per thread
* instead of calling Calendar.getInstance() multiple times. Calendar.getInstance() creates a new instance each
* time.
*/
public static final class DefaultCalendarThreadLocal extends ThreadLocal<Calendar> {
@Override
protected Calendar initialValue() {
return Calendar.getInstance();
}
}
private static ThreadLocal<Calendar> calendarThreadLocal = new DefaultCalendarThreadLocal();
public static long getTimeForDay(int year, int month, int day) {
return getTimeForDay(calendarThreadLocal.get(), year, month, day);
}
/** @param calendar helper object needed for conversion */
public static long getTimeForDay(Calendar calendar, int year, int month, int day) {
calendar.clear();
calendar.set(year, month - 1, day);
return calendar.getTimeInMillis();
}
/** Sets hour, minutes, seconds and milliseconds to the given values. Leaves date info untouched. */
public static void setTime(Calendar calendar, int hourOfDay, int minute, int second, int millisecond) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, millisecond);
}
/** Readable yyyyMMdd int representation of a day, which is also sortable. */
public static int getDayAsReadableInt(long time) {
Calendar cal = calendarThreadLocal.get();
cal.setTimeInMillis(time);
return getDayAsReadableInt(cal);
}
/** Readable yyyyMMdd representation of a day, which is also sortable. */
public static int getDayAsReadableInt(Calendar calendar) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH) + 1;
int year = calendar.get(Calendar.YEAR);
return year * 10000 + month * 100 + day;
}
/** Returns midnight of the given day. */
public static long getTimeFromDayReadableInt(int day) {
return getTimeFromDayReadableInt(calendarThreadLocal.get(), day, 0);
}
/** @param calendar helper object needed for conversion */
public static long getTimeFromDayReadableInt(Calendar calendar, int readableDay, int hour) {
int day = readableDay % 100;
int month = readableDay / 100 % 100;
int year = readableDay / 10000;
calendar.clear(); // We don't set all fields, so we should clear the calendar first
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.YEAR, year);
return calendar.getTimeInMillis();
}
public static int getDayDifferenceOfReadableInts(int dayOfBroadcast1, int dayOfBroadcast2) {
long time1 = getTimeFromDayReadableInt(dayOfBroadcast1);
long time2 = getTimeFromDayReadableInt(dayOfBroadcast2);
// Don't use getDayDifference(time1, time2) here, it's wrong for some days.
// Do float calculation and rounding at the end to cover daylight saving stuff etc.
float daysFloat = (time2 - time1) / 1000 / 60 / 60 / 24f;
return Math.round(daysFloat);
}
public static int getDayDifference(long time1, long time2) {
return (int) ((time2 - time1) / 1000 / 60 / 60 / 24);
}
public static long addDays(long time, int days) {
Calendar calendar = calendarThreadLocal.get();
calendar.setTimeInMillis(time);
calendar.add(Calendar.DAY_OF_YEAR, days);
return calendar.getTimeInMillis();
}
public static void addDays(Calendar calendar, int days) {
calendar.add(Calendar.DAY_OF_YEAR, days);
}
}