1010
1111import java .time .DayOfWeek ;
1212import java .time .Duration ;
13- import java .time .LocalDateTime ;
1413import java .time .Month ;
1514import java .time .Period ;
15+ import java .time .ZonedDateTime ;
1616import java .time .temporal .ChronoUnit ;
1717import java .util .HashMap ;
1818import java .util .Map ;
@@ -81,7 +81,7 @@ public class TimeUtils {
8181 Map .entry ("w6" , DayOfWeek .SATURDAY ));
8282
8383 /**
84- * Returns the relative {@link LocalDateTime } corresponding to the given relative string and local date time.
84+ * Returns the relative {@link ZonedDateTime } corresponding to the given relative string and zoned date time.
8585 * <p>
8686 * The relative time string has syntax {@code [+|-]<offset_time_integer><offset_time_unit>@<snap_time_unit>}, and
8787 * is made up of two optional components:
@@ -219,12 +219,12 @@ public class TimeUtils {
219219 * </body>
220220 * </table>
221221 */
222- public static LocalDateTime getRelativeLocalDateTime (String relativeString , LocalDateTime localDateTime ) {
222+ public static ZonedDateTime getRelativeZonedDateTime (String relativeString , ZonedDateTime zonedDateTime ) {
223223
224- LocalDateTime relativeLocalDateTime = localDateTime ;
224+ ZonedDateTime relativeZonedDateTime = zonedDateTime ;
225225
226226 if (relativeString .equalsIgnoreCase (NOW )) {
227- return localDateTime ;
227+ return zonedDateTime ;
228228 }
229229
230230 Matcher matcher = RELATIVE_PATTERN .matcher (relativeString );
@@ -235,27 +235,27 @@ public static LocalDateTime getRelativeLocalDateTime(String relativeString, Loca
235235
236236
237237 if (matcher .group ("offset" ) != null ) {
238- relativeLocalDateTime = applyOffset (
239- relativeLocalDateTime ,
238+ relativeZonedDateTime = applyOffset (
239+ relativeZonedDateTime ,
240240 matcher .group ("offsetSign" ),
241241 matcher .group ("offsetValue" ),
242242 matcher .group ("offsetUnit" ));
243243 }
244244
245245 if (matcher .group ("snap" ) != null ) {
246- relativeLocalDateTime = applySnap (
247- relativeLocalDateTime ,
246+ relativeZonedDateTime = applySnap (
247+ relativeZonedDateTime ,
248248 matcher .group ("snapUnit" ));
249249 }
250250
251- return relativeLocalDateTime ;
251+ return relativeZonedDateTime ;
252252 }
253253
254254 /**
255255 * Applies the offset specified by the offset sign, value,
256- * and unit to the given local date time, and returns the result.
256+ * and unit to the given zoned date time, and returns the result.
257257 */
258- private LocalDateTime applyOffset (LocalDateTime localDateTime , String offsetSign , String offsetValue , String offsetUnit ) {
258+ private ZonedDateTime applyOffset (ZonedDateTime zonedDateTime , String offsetSign , String offsetValue , String offsetUnit ) {
259259
260260 int offsetValueInt = Optional .ofNullable (offsetValue ).map (Integer ::parseInt ).orElse (1 );
261261 if (offsetSign .equals (NEGATIVE_SIGN )) {
@@ -271,46 +271,46 @@ private LocalDateTime applyOffset(LocalDateTime localDateTime, String offsetSign
271271
272272 if (DURATION_FOR_TIME_UNIT_MAP .containsKey (offsetUnitLowerCase )) {
273273 Duration offsetDuration = DURATION_FOR_TIME_UNIT_MAP .get (offsetUnitLowerCase ).multipliedBy (offsetValueInt );
274- return localDateTime .plus (offsetDuration );
274+ return zonedDateTime .plus (offsetDuration );
275275 }
276276
277277 if (PERIOD_FOR_TIME_UNIT_MAP .containsKey (offsetUnitLowerCase )) {
278278 Period offsetPeriod = PERIOD_FOR_TIME_UNIT_MAP .get (offsetUnitLowerCase ).multipliedBy (offsetValueInt );
279- return localDateTime .plus (offsetPeriod );
279+ return zonedDateTime .plus (offsetPeriod );
280280 }
281281
282282 String message = String .format ("The relative date time unit '%s' is not supported." , offsetUnit );
283283 throw new IllegalArgumentException (message );
284284 }
285285
286286 /**
287- * Snaps the given local date time to the start of the previous time
287+ * Snaps the given zoned date time to the start of the previous time
288288 * period specified by the given snap unit, and returns the result.
289289 */
290- private LocalDateTime applySnap (LocalDateTime localDateTime , String snapUnit ) {
290+ private ZonedDateTime applySnap (ZonedDateTime zonedDateTime , String snapUnit ) {
291291
292292 // Convert to lower case to make case-insensitive.
293293 String snapUnitLowerCase = snapUnit .toLowerCase ();
294294
295295 if (SECOND_UNITS_SET .contains (snapUnitLowerCase )) {
296- return localDateTime .truncatedTo (ChronoUnit .SECONDS );
296+ return zonedDateTime .truncatedTo (ChronoUnit .SECONDS );
297297 } else if (MINUTE_UNITS_SET .contains (snapUnitLowerCase )) {
298- return localDateTime .truncatedTo (ChronoUnit .MINUTES );
298+ return zonedDateTime .truncatedTo (ChronoUnit .MINUTES );
299299 } else if (HOUR_UNITS_SET .contains (snapUnitLowerCase )) {
300- return localDateTime .truncatedTo (ChronoUnit .HOURS );
300+ return zonedDateTime .truncatedTo (ChronoUnit .HOURS );
301301 } else if (DAY_UNITS_SET .contains (snapUnitLowerCase )) {
302- return localDateTime .truncatedTo (ChronoUnit .DAYS );
302+ return zonedDateTime .truncatedTo (ChronoUnit .DAYS );
303303 } else if (WEEK_UNITS_SET .contains (snapUnitLowerCase )) {
304- return applySnapToDayOfWeek (localDateTime , DayOfWeek .SUNDAY );
304+ return applySnapToDayOfWeek (zonedDateTime , DayOfWeek .SUNDAY );
305305 } else if (MONTH_UNITS_SET .contains (snapUnitLowerCase )) {
306- return localDateTime .truncatedTo (ChronoUnit .DAYS ).withDayOfMonth (1 );
306+ return zonedDateTime .truncatedTo (ChronoUnit .DAYS ).withDayOfMonth (1 );
307307 } else if (QUARTER_UNITS_SET .contains (snapUnitLowerCase )) {
308- Month snapMonth = localDateTime .getMonth ().firstMonthOfQuarter ();
309- return localDateTime .truncatedTo (ChronoUnit .DAYS ).withDayOfMonth (1 ).withMonth (snapMonth .getValue ());
308+ Month snapMonth = zonedDateTime .getMonth ().firstMonthOfQuarter ();
309+ return zonedDateTime .truncatedTo (ChronoUnit .DAYS ).withDayOfMonth (1 ).withMonth (snapMonth .getValue ());
310310 } else if (YEAR_UNITS_SET .contains (snapUnitLowerCase )) {
311- return localDateTime .truncatedTo (ChronoUnit .DAYS ).withDayOfYear (1 );
311+ return zonedDateTime .truncatedTo (ChronoUnit .DAYS ).withDayOfYear (1 );
312312 } else if (DAY_OF_THE_WEEK_FOR_SNAP_UNIT_MAP .containsKey (snapUnitLowerCase )) {
313- return applySnapToDayOfWeek (localDateTime , DAY_OF_THE_WEEK_FOR_SNAP_UNIT_MAP .get (snapUnitLowerCase ));
313+ return applySnapToDayOfWeek (zonedDateTime , DAY_OF_THE_WEEK_FOR_SNAP_UNIT_MAP .get (snapUnitLowerCase ));
314314 }
315315
316316 String message = String .format ("The relative date time unit '%s' is not supported." , snapUnit );
@@ -321,10 +321,10 @@ private LocalDateTime applySnap(LocalDateTime localDateTime, String snapUnit) {
321321 * Snaps the given date time to the start of the previous
322322 * specified day of the week, and returns the result.
323323 */
324- private LocalDateTime applySnapToDayOfWeek (LocalDateTime dateTime , DayOfWeek snapDayOfWeek ) {
325- LocalDateTime snappedDateTime = dateTime .truncatedTo (ChronoUnit .DAYS );
324+ private ZonedDateTime applySnapToDayOfWeek (ZonedDateTime zonedDateTime , DayOfWeek snapDayOfWeek ) {
325+ ZonedDateTime snappedDateTime = zonedDateTime .truncatedTo (ChronoUnit .DAYS );
326326
327- int daysToSnap = dateTime .getDayOfWeek ().getValue () - snapDayOfWeek .getValue ();
327+ int daysToSnap = zonedDateTime .getDayOfWeek ().getValue () - snapDayOfWeek .getValue ();
328328 if (daysToSnap < 0 ) daysToSnap += DayOfWeek .values ().length ;
329329
330330 return snappedDateTime .minusDays (daysToSnap );
0 commit comments