1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang3.time;
19
20 import java.time.LocalDateTime;
21 import java.time.OffsetDateTime;
22 import java.time.ZoneId;
23 import java.time.ZonedDateTime;
24 import java.util.Calendar;
25 import java.util.Locale;
26 import java.util.Locale.Category;
27 import java.util.Map;
28 import java.util.Objects;
29
30
31
32
33
34
35 public class CalendarUtils {
36
37
38
39
40
41
42
43 public static final CalendarUtils INSTANCE = getInstance();
44
45
46
47
48
49
50
51 public static CalendarUtils getInstance() {
52 return new CalendarUtils(Calendar.getInstance());
53 }
54
55
56
57
58
59
60
61
62 static CalendarUtils getInstance(final Locale locale) {
63 return new CalendarUtils(Calendar.getInstance(locale), locale);
64 }
65
66
67
68
69
70
71
72
73 public static LocalDateTime toLocalDateTime(final Calendar calendar) {
74 return LocalDateTime.ofInstant(calendar.toInstant(), toZoneId(calendar));
75 }
76
77
78
79
80
81
82
83
84 public static OffsetDateTime toOffsetDateTime(final Calendar calendar) {
85 return OffsetDateTime.ofInstant(calendar.toInstant(), toZoneId(calendar));
86 }
87
88
89
90
91
92
93
94
95 public static ZonedDateTime toZonedDateTime(final Calendar calendar) {
96 return ZonedDateTime.ofInstant(calendar.toInstant(), toZoneId(calendar));
97 }
98
99 private static ZoneId toZoneId(final Calendar calendar) {
100 return calendar.getTimeZone().toZoneId();
101 }
102
103 private final Calendar calendar;
104
105 private final Locale locale;
106
107
108
109
110
111
112 public CalendarUtils(final Calendar calendar) {
113 this(calendar, Locale.getDefault());
114 }
115
116
117
118
119
120
121 CalendarUtils(final Calendar calendar, final Locale locale) {
122 this.calendar = Objects.requireNonNull(calendar, "calendar");
123 this.locale = Objects.requireNonNull(locale, "locale");
124 }
125
126
127
128
129
130
131 public int getDayOfMonth() {
132 return calendar.get(Calendar.DAY_OF_MONTH);
133 }
134
135
136
137
138
139
140
141 public int getDayOfYear() {
142 return calendar.get(Calendar.DAY_OF_YEAR);
143 }
144
145
146
147
148
149
150 public int getMonth() {
151 return calendar.get(Calendar.MONTH);
152 }
153
154
155
156
157
158
159 String[] getMonthDisplayNames(final int style) {
160
161
162 final Map<String, Integer> displayNames = calendar.getDisplayNames(Calendar.MONTH, style, locale);
163 if (displayNames == null) {
164 return null;
165 }
166 final String[] monthNames = new String[displayNames.size()];
167 displayNames.forEach((k, v) -> monthNames[v] = k);
168 return monthNames;
169 }
170
171
172
173
174
175 String[] getStandaloneLongMonthNames() {
176 return getMonthDisplayNames(Calendar.LONG_STANDALONE);
177 }
178
179
180
181
182
183 String[] getStandaloneShortMonthNames() {
184 return getMonthDisplayNames(Calendar.SHORT_STANDALONE);
185 }
186
187
188
189
190
191
192 public int getYear() {
193 return calendar.get(Calendar.YEAR);
194 }
195
196
197
198
199
200
201
202 public LocalDateTime toLocalDateTime() {
203 return toLocalDateTime(calendar);
204 }
205
206
207
208
209
210
211
212 public OffsetDateTime toOffsetDateTime() {
213 return toOffsetDateTime(calendar);
214 }
215
216
217
218
219
220
221
222 public ZonedDateTime toZonedDateTime() {
223 return toZonedDateTime(calendar);
224 }
225
226 }