Package eu.woolplatform.utils.datetime
Class DateTimeUtils
- java.lang.Object
-
- eu.woolplatform.utils.datetime.DateTimeUtils
-
public class DateTimeUtils extends Object
This class contains various utility methods related to date and time.
-
-
Constructor Summary
Constructors Constructor Description DateTimeUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TdateTimeToType(org.joda.time.DateTime dateTime, Class<T> clazz)Converts aDateTimeobject to an object of the specified class.static org.joda.time.DateTimelocalToUtcWithGapCorrection(org.joda.time.LocalDateTime localDateTime, org.joda.time.DateTimeZone tz)Converts the specified local date/time to a date/time in the specified time zone.static <T> TparseDateTime(String dateTimeString, Class<T> clazz)Parses a date/time string and returns a date/time object of the specified class.
-
-
-
Method Detail
-
parseDateTime
public static <T> T parseDateTime(String dateTimeString, Class<T> clazz) throws ParseException
Parses a date/time string and returns a date/time object of the specified class. We distinguish three types of date/time classes: UTC time, time with time zone, local date/time. The supported date/time classes are:- long/Long (UNIX timestamp in milliseconds, UTC time)
Date(UTC time)Instant(UTC time)Calendar(with time zone)DateTime(with time zone)LocalDateLocalTimeLocalDateTime
It depends on the string format what class can be returned. This is detailed below. Supported string formats:
-
UNIX timestamp
- long/Long, Date, Instant
- Calendar, DateTime: the timestamp is translated to the default time zone.
- LocalDate, LocalTime, LocalDateTime: the timestamp is translated to the default time zone before creating the local date/time.
-
SQL date: yyyy-MM-dd
- LocalDate
-
SQL time: HH:mm:ss
- LocalTime
-
SQL datetime: yyyy-MM-dd HH:mm:ss
- LocalDateTime
-
any ISO date/time accepted by
ISODateTimeFormat.dateTimeParser()- long/Long, Date, Instant. If no time zone is given in the string, it interprets the date/time with the default time zone. If the date/time does not exist in the time zone (because of a DST change), this method throws an exception. These classes store UTC times, so any specified time zone is eventually lost. Note that a string with only a date is a valid ISO date, but this method considers it an SQL date so the result must be a LocalDate.
- Calendar, DateTime. The same as the UTC times except that any specified time zone is preserved in the result.
- LocalDate, LocalTime, LocalDateTime. Any specified time zone is ignored.
- Type Parameters:
T- the type of date/time to return- Parameters:
dateTimeString- the date/time stringclazz- the result class- Returns:
- the date/time with the specified class
- Throws:
ParseException- if the date/time string is invalid, or a date/time without a time zone is parsed in a time zone where that date/time does not exist
-
dateTimeToType
public static <T> T dateTimeToType(org.joda.time.DateTime dateTime, Class<T> clazz) throws IllegalArgumentExceptionConverts aDateTimeobject to an object of the specified class. It supports the following classes.- long/Long (UNIX timestamp in milliseconds): translated to UTC time, time zone is lost
Date: translated to UTC time, time zone is lostInstant: translated to UTC time, time zone is lostCalendarDateTimeLocalDate: time and time zone is ignoredLocalTime: date and time zone is ignoredLocalDateTime: time zone is ignored
- Type Parameters:
T- the type of date/time to return- Parameters:
dateTime- the date/timeclazz- the result class- Returns:
- the date/time with the specified class
- Throws:
IllegalArgumentException- if the target class is not supported
-
localToUtcWithGapCorrection
public static org.joda.time.DateTime localToUtcWithGapCorrection(org.joda.time.LocalDateTime localDateTime, org.joda.time.DateTimeZone tz)Converts the specified local date/time to a date/time in the specified time zone. If the local time is in a DST gap, it will add one hour. It could therefore occur in the next day.- Parameters:
localDateTime- the local date/timetz- the time zone- Returns:
- the date/time
-
-