org.conqat.lib.commons.date.DateUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-lib-commons Show documentation
Show all versions of teamscale-lib-commons Show documentation
Provides common utility functions
The newest version!
/*
* Copyright (c) CQSE GmbH
*
* 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 org.conqat.lib.commons.date;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
/**
* Utility methods for working on date objects.
*
* @deprecated Use {@link DateTimeUtils} or {@link DurationUtils}.
*/
@Deprecated
public class DateUtils {
/**
* Returns the current time as a {@link Date} object. This is preferred to directly calling the
* {@link Date#Date()} constructor, as this method provides a central entry point that allows the
* notion of time to be tweaked (e.g. for testing, when we want a certain date to be returned).
*
* The behavior of this method can be affected either by calling the
* {@link DateTimeUtils#setClock(Clock)} method, or by providing a fixed date in format
* {@value DateTimeUtils#NOW_SYSTEM_PROPERTY_PATTERN} as the system property
* {@value DateTimeUtils#NOW_SYSTEM_PROPERTY_NAME}.
*
* @see DateTimeUtils#now()
* @deprecated Use {@link DateTimeUtils#now()}
*/
public static synchronized Date getNow() {
return Date.from(DateTimeUtils.now());
}
/**
* {@link #getNow()} {@link #truncateToBeginOfDay(Date) truncated to the begin of day}.
*
* @deprecated This method implicitly used the system time zone, to truncate the time. Use
* {@link DateTimeUtils#withClock(Function)} with {@link LocalDate#now(Clock) LocalDate}
* if only the date is needed. If the actual timestamp is needed, it depends on the
* timezone of the requested date.
*/
public static Date getNowWithoutTime() {
return truncateToBeginOfDay(getNow());
}
/**
* Returns a normalized version of the given date. Normalization is done by removing all time-of-day
* information (using the current {@link DateTimeUtils#getZone() zone}).
*
* @deprecated This method implicitly used the system time zone, to truncate the time. Use
* {@link ZonedDateTime#truncatedTo(TemporalUnit)} or similar to be aware of the correct
* timezone.
*/
public static Date truncateToBeginOfDay(Date date) {
if (date == null) {
return null;
}
return Date.from(DateTimeUtils.atZone(date.toInstant()).truncatedTo(ChronoUnit.DAYS).toInstant());
}
/**
* Returns the difference between two dates in days.
*
* @deprecated Use {@link DateTimeUtils#diff(Instant, Instant, TimeUnit)}
*/
public static long diffDays(Date earlier, Date later) {
return DateTimeUtils.diff(earlier.toInstant(), later.toInstant(), TimeUnit.DAYS);
}
/**
* Create a new date and add days
to it.
*
* @deprecated This method uses the implicit system timezone to add date-based days. Use
* {@link ZonedDateTime#plusDays(long)} for date-based addition or
* {@link Duration#ofDays(long) Duration.ofDays(days)}{@link Duration#addTo(Temporal)
* .addTo(...)} for time-based addition (24 hours)
*/
public static Date addDaysToDate(Date date, int days) {
return Date.from(DateTimeUtils.atZone(date.toInstant()).plusDays(days).toInstant());
}
/**
* Create a new date and subtracts days
from it.
*
* @deprecated This method uses the implicit system timezone to subtract date-based days. Use
* {@link ZonedDateTime#minusDays(long)} for date-based subtraction or
* {@link Duration#ofDays(long)
* Duration.ofDays(days)}{@link Duration#subtractFrom(Temporal) .subtractFrom(...)} for
* time-based addition (24 hours)
*/
public static Date subtractDaysFromDate(Date date, int days) {
return addDaysToDate(date, -days);
}
/**
* Create a date.
*
* @param month
* zero-based index
*
* @deprecated This method implicitly uses the system time zone for which the date is created. Use
* {@link LocalDate} or similar.
*/
public static Date createDate(int day, int month, int year) {
if (month == 12) {
throw new IllegalArgumentException("month is zero-based, use the Calendar instances");
}
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
return DateUtils.truncateToBeginOfDay(calendar.getTime());
}
}