Package com.helger.commons.datetime
Class PDTWebDateHelper
- java.lang.Object
-
- com.helger.commons.datetime.PDTWebDateHelper
-
@Immutable public final class PDTWebDateHelper extends Object
A helper class that parses Dates out of Strings with date time in RFC822 and W3CDateTime formats plus the variants Atom (0.3) and RSS (0.9, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) specificators added to those formats.
It uses the JDK java.text.SimpleDateFormat class attempting the parse using a mask for each one of the possible formats.
Original work Copyright 2004 Sun Microsystems, Inc.- Author:
- Alejandro Abdelnur (original; mainly the formatting masks), Philip Helger (major modification)
-
-
Field Summary
Fields Modifier and Type Field Description static DateTimeFormatterXSD_DATEstatic DateTimeFormatterXSD_DATE_TIMEstatic DateTimeFormatterXSD_TIME
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static WithZoneIdextractDateTimeZone(String sDate)Extract the time zone from the passed string.static StringgetAsStringRFC822(LocalDateTime aDateTime)create a RFC822 representation of a date time using UTC date time zone.static StringgetAsStringRFC822(OffsetDateTime aDateTime)create a RFC822 representation of a date.static StringgetAsStringRFC822(ZonedDateTime aDateTime)create a RFC822 representation of a date.static StringgetAsStringW3C(LocalDateTime aDateTime)create a W3C Date Time representation of a date.static StringgetAsStringW3C(OffsetDateTime aDateTime)create a W3C Date Time representation of a date time using UTC date time zone.static StringgetAsStringW3C(ZonedDateTime aDateTime)create a W3C Date Time representation of a date time using UTC date time zone.static StringgetAsStringXSD(OffsetDate aOD)static StringgetAsStringXSD(XMLOffsetDate aOD)static StringgetAsStringXSD(XMLOffsetDateTime aODT)static StringgetAsStringXSD(XMLOffsetTime aOT)static StringgetAsStringXSD(LocalDate aLD)static StringgetAsStringXSD(LocalDateTime aLDT)static StringgetAsStringXSD(LocalTime aLT)static StringgetAsStringXSD(OffsetDateTime aODT)static StringgetAsStringXSD(OffsetTime aOT)static StringgetAsStringXSD(ZonedDateTime aZDT)static StringgetAsStringXSD(ZoneId aZoneID, ZonedDateTime aZDT)static StringgetCurrentDateTimeAsStringRFC822()static StringgetCurrentDateTimeAsStringW3C()static ZonedDateTimegetDateTimeFromRFC822(String sDate)Parses a Date out of a String with a date in RFC822 format.static OffsetDateTimegetDateTimeFromW3C(String sDate)Parses a Date out of a String with a date in W3C date-time format.static ZonedDateTimegetDateTimeFromW3COrRFC822(String sDate)Parses a Date out of a String with a date in W3C date-time format or in a RFC822 format.static LocalDategetLocalDateFromXSD(String sValue)static LocalDateTimegetLocalDateTimeFromW3COrRFC822(String sDate)Parses a Date out of a String with a date in W3C date-time format or in a RFC822 format.static LocalDateTimegetLocalDateTimeFromXSD(String sValue)static LocalTimegetLocalTimeFromXSD(String sValue)static OffsetDategetOffsetDateFromXSD(String sValue)static OffsetDateTimegetOffsetDateTimeFromXSD(String sValue)static OffsetTimegetOffsetTimeFromXSD(String sValue)static XMLOffsetDategetXMLOffsetDateFromXSD(String sValue)static XMLOffsetDateTimegetXMLOffsetDateTimeFromXSD(String sValue)static XMLOffsetTimegetXMLOffsetTimeFromXSD(String sValue)static DateTimeFormattergetXSDFormatterDate()static DateTimeFormattergetXSDFormatterDateTime(ZoneId aOverrideZoneID)static DateTimeFormattergetXSDFormatterTime()static ZonedDateTimegetZonedDateTimeFromXSD(String sValue)static ZonedDateTimegetZonedDateTimeFromXSD(String sValue, ZoneId aZoneID)static OffsetDateTimeparseOffsetDateTimeUsingMask(PDTMask<?>[] aMasks, String sDate)Parses a Date out of a string using an array of masks.static ZonedDateTimeparseZonedDateTimeUsingMask(PDTMask<?>[] aMasks, String sDate, ZoneId aDTZ)Parses a Date out of a string using an array of masks.
-
-
-
Field Detail
-
XSD_TIME
public static final DateTimeFormatter XSD_TIME
-
XSD_DATE
public static final DateTimeFormatter XSD_DATE
-
XSD_DATE_TIME
public static final DateTimeFormatter XSD_DATE_TIME
-
-
Method Detail
-
parseOffsetDateTimeUsingMask
@Nullable public static OffsetDateTime parseOffsetDateTimeUsingMask(@Nonnull PDTMask<?>[] aMasks, @Nonnull @Nonempty String sDate)
Parses a Date out of a string using an array of masks. It uses the masks in order until one of them succeeds or all fail.- Parameters:
aMasks- array of masks to use for parsing the stringsDate- string to parse for a date.- Returns:
- the Date represented by the given string using one of the given masks. It returns null if it was not possible to parse the the string with any of the masks.
-
parseZonedDateTimeUsingMask
@Nullable public static ZonedDateTime parseZonedDateTimeUsingMask(@Nonnull PDTMask<?>[] aMasks, @Nonnull @Nonempty String sDate, @Nullable ZoneId aDTZ)
Parses a Date out of a string using an array of masks. It uses the masks in order until one of them succeeds or all fail.- Parameters:
aMasks- array of masks to use for parsing the stringsDate- string to parse for a date.aDTZ- The date/time zone to use. Optional.- Returns:
- the Date represented by the given string using one of the given masks. It returns null if it was not possible to parse the the string with any of the masks.
-
extractDateTimeZone
@Nonnull public static WithZoneId extractDateTimeZone(@Nonnull String sDate)
Extract the time zone from the passed string. UTC and GMT are supported.- Parameters:
sDate- The date string.- Returns:
- A non-
nullWithZoneId, where the remaining string to be parsed (nevernull) and and the extracted time zone (may benull) are contained.
-
getDateTimeFromRFC822
@Nullable public static ZonedDateTime getDateTimeFromRFC822(@Nullable String sDate)
Parses a Date out of a String with a date in RFC822 format.
It parsers the following formats:- "EEE, dd MMM uuuu HH:mm:ss z"
- "EEE, dd MMM uuuu HH:mm z"
- "EEE, dd MMM uu HH:mm:ss z"
- "EEE, dd MMM uu HH:mm z"
- "dd MMM uuuu HH:mm:ss z"
- "dd MMM uuuu HH:mm z"
- "dd MMM uu HH:mm:ss z"
- "dd MMM uu HH:mm z"
Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.
- Parameters:
sDate- string to parse for a date. May benull.- Returns:
- the Date represented by the given RFC822 string. It returns
null if it was not possible to parse the given string into a
ZonedDateTimeor if the passedStringwasnull.
-
getDateTimeFromW3C
@Nullable public static OffsetDateTime getDateTimeFromW3C(@Nullable String sDate)
Parses a Date out of a String with a date in W3C date-time format.
It parsers the following formats:- "uuuu-MM-dd'T'HH:mm:ssz"
- "uuuu-MM-dd'T'HH:mmz"
- "uuuu-MM-dd"
- "uuuu-MM"
- "uuuu"
Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element.
- Parameters:
sDate- string to parse for a date. May benull.- Returns:
- the Date represented by the given W3C date-time string. It returns
null if it was not possible to parse the given string into a
ZonedDateTimeor if the input string wasnull.
-
getDateTimeFromW3COrRFC822
@Nullable public static ZonedDateTime getDateTimeFromW3COrRFC822(@Nullable String sDate)
Parses a Date out of a String with a date in W3C date-time format or in a RFC822 format.- Parameters:
sDate- string to parse for a date.- Returns:
- the Date represented by the given W3C date-time string. It returns null if it was not possible to parse the given string into a Date.
-
getLocalDateTimeFromW3COrRFC822
@Nullable public static LocalDateTime getLocalDateTimeFromW3COrRFC822(@Nullable String sDate)
Parses a Date out of a String with a date in W3C date-time format or in a RFC822 format.- Parameters:
sDate- string to parse for a date.- Returns:
- the Date represented by the given W3C date-time string. It returns null if it was not possible to parse the given string into a Date.
-
getAsStringRFC822
@Nullable public static String getAsStringRFC822(@Nullable ZonedDateTime aDateTime)
create a RFC822 representation of a date.- Parameters:
aDateTime- Date to print. May benull.- Returns:
- the RFC822 represented by the given Date.
nullif the parameter isnull.
-
getAsStringRFC822
@Nullable public static String getAsStringRFC822(@Nullable OffsetDateTime aDateTime)
create a RFC822 representation of a date.- Parameters:
aDateTime- Date to print. May benull.- Returns:
- the RFC822 represented by the given Date.
nullif the parameter isnull.
-
getAsStringRFC822
@Nullable public static String getAsStringRFC822(@Nullable LocalDateTime aDateTime)
create a RFC822 representation of a date time using UTC date time zone.- Parameters:
aDateTime- Date to print. May benull.- Returns:
- the RFC822 represented by the given Date.
nullif the parameter isnull.
-
getAsStringW3C
@Nullable public static String getAsStringW3C(@Nullable ZonedDateTime aDateTime)
create a W3C Date Time representation of a date time using UTC date time zone.- Parameters:
aDateTime- Date to print. May not benull.- Returns:
- the W3C Date Time represented by the given Date.
-
getAsStringW3C
@Nullable public static String getAsStringW3C(@Nullable OffsetDateTime aDateTime)
create a W3C Date Time representation of a date time using UTC date time zone.- Parameters:
aDateTime- Date to print. May not benull.- Returns:
- the W3C Date Time represented by the given Date.
-
getAsStringW3C
@Nullable public static String getAsStringW3C(@Nullable LocalDateTime aDateTime)
create a W3C Date Time representation of a date.- Parameters:
aDateTime- Date to print. May not benull.- Returns:
- the W3C Date Time represented by the given Date.
-
getCurrentDateTimeAsStringRFC822
@Nonnull public static String getCurrentDateTimeAsStringRFC822()
- Returns:
- The current date time formatted using RFC 822
-
getCurrentDateTimeAsStringW3C
@Nonnull public static String getCurrentDateTimeAsStringW3C()
- Returns:
- The current date time formatted using W3C format
-
getXSDFormatterDateTime
@Nonnull public static DateTimeFormatter getXSDFormatterDateTime(@Nullable ZoneId aOverrideZoneID)
-
getLocalDateTimeFromXSD
@Nullable public static LocalDateTime getLocalDateTimeFromXSD(@Nullable String sValue)
-
getAsStringXSD
@Nullable public static String getAsStringXSD(@Nullable LocalDateTime aLDT)
-
getZonedDateTimeFromXSD
@Nullable public static ZonedDateTime getZonedDateTimeFromXSD(@Nullable String sValue)
-
getZonedDateTimeFromXSD
@Nullable public static ZonedDateTime getZonedDateTimeFromXSD(@Nullable String sValue, @Nullable ZoneId aZoneID)
-
getAsStringXSD
@Nullable public static String getAsStringXSD(@Nullable ZonedDateTime aZDT)
-
getAsStringXSD
@Nullable public static String getAsStringXSD(@Nonnull ZoneId aZoneID, @Nullable ZonedDateTime aZDT)
-
getOffsetDateTimeFromXSD
@Nullable public static OffsetDateTime getOffsetDateTimeFromXSD(@Nullable String sValue)
-
getAsStringXSD
@Nullable public static String getAsStringXSD(@Nullable OffsetDateTime aODT)
-
getXMLOffsetDateTimeFromXSD
@Nullable public static XMLOffsetDateTime getXMLOffsetDateTimeFromXSD(@Nullable String sValue)
-
getAsStringXSD
@Nullable public static String getAsStringXSD(@Nullable XMLOffsetDateTime aODT)
-
getXSDFormatterDate
@Nonnull public static DateTimeFormatter getXSDFormatterDate()
-
getOffsetDateFromXSD
@Nullable public static OffsetDate getOffsetDateFromXSD(@Nullable String sValue)
-
getAsStringXSD
@Nullable public static String getAsStringXSD(@Nullable OffsetDate aOD)
-
getXMLOffsetDateFromXSD
@Nullable public static XMLOffsetDate getXMLOffsetDateFromXSD(@Nullable String sValue)
-
getAsStringXSD
@Nullable public static String getAsStringXSD(@Nullable XMLOffsetDate aOD)
-
getXSDFormatterTime
@Nonnull public static DateTimeFormatter getXSDFormatterTime()
-
getOffsetTimeFromXSD
@Nullable public static OffsetTime getOffsetTimeFromXSD(@Nullable String sValue)
-
getAsStringXSD
@Nullable public static String getAsStringXSD(@Nullable OffsetTime aOT)
-
getXMLOffsetTimeFromXSD
@Nullable public static XMLOffsetTime getXMLOffsetTimeFromXSD(@Nullable String sValue)
-
getAsStringXSD
@Nullable public static String getAsStringXSD(@Nullable XMLOffsetTime aOT)
-
-