Package org.n52.shetland.util
Class DateTimeHelper
- java.lang.Object
-
- org.n52.shetland.util.DateTimeHelper
-
public final class DateTimeHelper extends Object
Utility class for Time formatting and parsing. Uses Joda Time.- Since:
- 1.0.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.joda.time.DateTimecalculateExpiresDateTime(org.joda.time.DateTime start)Calculates the expire time for a time objectstatic Stringformat(Time time)Formats the given Time to ISO 8601 string.static StringformatDateTime2FormattedString(org.joda.time.DateTime dateTime, String dateFormat)Formats a DateTime to a String using specified formatstatic StringformatDateTime2FormattedString(org.joda.time.DateTime dateTime, org.joda.time.format.DateTimeFormatter formatter)Formats a DateTime to a DAte using specifiedDateTimeFormatterstatic StringformatDateTime2IsoString(org.joda.time.DateTime dateTime)Formats a DateTime to a ISO-8601 Stringstatic StringformatDateTime2ResponseString(org.joda.time.DateTime dateTime)Formats a DateTime to a String using the response formatstatic StringformatDateTime2String(org.joda.time.DateTime dateTime, Time.TimeFormat timeFormat)Get formatted string fromDateTimeas definedTime.TimeFormatstatic StringformatDateTime2String(TimePosition timePosition)Get formatted string fromTimePositionstatic StringformatDateTime2YearDateString(org.joda.time.DateTime dateTime)formats a DateTime to a string with year.static StringformatDateTime2YearMonthDateString(org.joda.time.DateTime dateTime)formats a DateTime to a string with year-month.static StringformatDateTime2YearMonthDayDateStringYMD(org.joda.time.DateTime dateTime)formats a DateTime to a string with year-month-day.static intgetDaysSince(org.joda.time.DateTime start, org.joda.time.DateTime end)Get days between the givenDateTimesstatic doublegetDaysSinceWithPrecision(org.joda.time.DateTime start, org.joda.time.DateTime end)Get days between the givenDateTimes with precisionstatic intgetMinutesSince(org.joda.time.DateTime start, org.joda.time.DateTime end)Get minutes between the givenDateTimesstatic doublegetSecondsSinceEpoch(org.joda.time.DateTime time)Get seconds since epochstatic intgetTimeLengthBeforeTimeZone(String time)static org.joda.time.DateTimemakeDateTime(Object object)Make a new UTC DateTime from an objectstatic org.joda.time.DateTimemax(org.joda.time.DateTime dt1, org.joda.time.DateTime dt2)Find the max of two dates (null safe)static org.joda.time.PeriodparseDuration(String duration)Parse a duration from a String representationstatic org.joda.time.DateTimeparseIsoString2DateTime(String timeString)Parses a time String to a Joda Time DateTime objectstatic TimeparseIsoString2DateTime2Time(String timeString)static org.joda.time.DateTimeparseString2DateTime(String timeString, String dateFormat)static org.joda.time.DateTimesetDateTime2EndOfMostPreciseUnit4RequestedEndPosition(String time)static org.joda.time.DateTimesetDateTime2EndOfMostPreciseUnit4RequestedEndPosition(org.joda.time.DateTime dateTime, int isoTimeLength)Set the time object to the end values (seconds, minutes, hours, days,..) if the time Object has not all valuesstatic voidsetLease(int lease)Set the lease valuestatic voidsetResponseFormat(String responseFormat)Set the response formatstatic org.joda.time.DateTimetoUTC(org.joda.time.DateTime datetime)Transforms the suppliedDateTimeto UTC.
-
-
-
Method Detail
-
parseIsoString2DateTime
public static org.joda.time.DateTime parseIsoString2DateTime(String timeString) throws DateTimeParseException
Parses a time String to a Joda Time DateTime object- Parameters:
timeString- Time String- Returns:
- DateTime object
- Throws:
DateTimeParseException- If an error occurs.
-
parseString2DateTime
public static org.joda.time.DateTime parseString2DateTime(String timeString, String dateFormat) throws DateTimeParseException
- Throws:
DateTimeParseException
-
parseIsoString2DateTime2Time
public static Time parseIsoString2DateTime2Time(String timeString) throws DateTimeParseException
- Parameters:
timeString- a ISO 8601 formatted time string- Returns:
- a Time object
- Throws:
DateTimeParseException- If an error occurs.
-
format
public static String format(Time time)
Formats the given Time to ISO 8601 string.- Parameters:
time- anTimeobject to be formatted- Returns:
- an ISO 8601 conform
String. - Throws:
IllegalArgumentException- in the case of receiving null or not supported types.- See Also:
formatDateTime2IsoString(DateTime)
-
formatDateTime2IsoString
public static String formatDateTime2IsoString(org.joda.time.DateTime dateTime)
Formats a DateTime to a ISO-8601 String- Parameters:
dateTime- Time object- Returns:
- ISO-8601 formatted time String
-
formatDateTime2ResponseString
public static String formatDateTime2ResponseString(org.joda.time.DateTime dateTime) throws DateTimeFormatException
Formats a DateTime to a String using the response format- Parameters:
dateTime- Time object- Returns:
- Response formatted time String
- Throws:
DateTimeFormatException- If an error occurs.
-
formatDateTime2String
public static String formatDateTime2String(org.joda.time.DateTime dateTime, Time.TimeFormat timeFormat) throws DateTimeFormatException
Get formatted string fromDateTimeas definedTime.TimeFormat- Parameters:
dateTime-DateTimeto formattimeFormat- The responseTime.TimeFormat- Returns:
- The formatted
DateTime - Throws:
DateTimeFormatException- If an error occurs when formatting theDateTime
-
formatDateTime2String
public static String formatDateTime2String(TimePosition timePosition) throws DateTimeFormatException
Get formatted string fromTimePosition- Parameters:
timePosition-TimePositionto format- Returns:
- The formatted
TimePosition - Throws:
DateTimeFormatException- If an error occurs when formatting theTimePosition
-
formatDateTime2FormattedString
public static String formatDateTime2FormattedString(org.joda.time.DateTime dateTime, String dateFormat) throws DateTimeFormatException
Formats a DateTime to a String using specified format- Parameters:
dateTime- Time objectdateFormat- the date time format- Returns:
- Specified formatted time String
- Throws:
DateTimeFormatException- If an error occurs.
-
formatDateTime2FormattedString
public static String formatDateTime2FormattedString(org.joda.time.DateTime dateTime, org.joda.time.format.DateTimeFormatter formatter) throws DateTimeFormatException
Formats a DateTime to a DAte using specifiedDateTimeFormatter- Parameters:
dateTime- Time objectformatter- theDateTimeFormatter- Returns:
- Specified formatted time String
- Throws:
DateTimeFormatException- If an error occurs.
-
formatDateTime2YearMonthDayDateStringYMD
public static String formatDateTime2YearMonthDayDateStringYMD(org.joda.time.DateTime dateTime)
formats a DateTime to a string with year-month-day.- Parameters:
dateTime- The DateTime.- Returns:
- Returns formatted time String.
- Throws:
DateTimeFormatException- If an error occurs.
-
formatDateTime2YearMonthDateString
public static String formatDateTime2YearMonthDateString(org.joda.time.DateTime dateTime) throws DateTimeFormatException
formats a DateTime to a string with year-month.- Parameters:
dateTime- The DateTime.- Returns:
- Returns formatted time String.
- Throws:
DateTimeFormatException- If an error occurs.
-
formatDateTime2YearDateString
public static String formatDateTime2YearDateString(org.joda.time.DateTime dateTime) throws DateTimeFormatException
formats a DateTime to a string with year.- Parameters:
dateTime- The DateTime.- Returns:
- Returns formatted time String.
- Throws:
DateTimeFormatException- If an error occurs.
-
getTimeLengthBeforeTimeZone
public static int getTimeLengthBeforeTimeZone(String time)
-
setDateTime2EndOfMostPreciseUnit4RequestedEndPosition
public static org.joda.time.DateTime setDateTime2EndOfMostPreciseUnit4RequestedEndPosition(org.joda.time.DateTime dateTime, int isoTimeLength)Set the time object to the end values (seconds, minutes, hours, days,..) if the time Object has not all values- Parameters:
dateTime- Time objectisoTimeLength- Length of the time object- Returns:
- Modified time object.
-
setDateTime2EndOfMostPreciseUnit4RequestedEndPosition
public static org.joda.time.DateTime setDateTime2EndOfMostPreciseUnit4RequestedEndPosition(String time) throws DateTimeParseException
- Throws:
DateTimeParseException
-
parseDuration
public static org.joda.time.Period parseDuration(String duration)
Parse a duration from a String representation- Parameters:
duration- Duration as String- Returns:
- Period object of duration
-
calculateExpiresDateTime
public static org.joda.time.DateTime calculateExpiresDateTime(org.joda.time.DateTime start)
Calculates the expire time for a time object- Parameters:
start- Time object- Returns:
- Expire time
-
setResponseFormat
public static void setResponseFormat(String responseFormat)
Set the response format- Parameters:
responseFormat- Defined response format
-
setLease
public static void setLease(int lease)
Set the lease value- Parameters:
lease- Defined lease value
-
makeDateTime
public static org.joda.time.DateTime makeDateTime(Object object)
Make a new UTC DateTime from an object- Parameters:
object- Object to makeDateTime- Returns:
- DateTime, or null if object was null
-
toUTC
public static org.joda.time.DateTime toUTC(org.joda.time.DateTime datetime)
Transforms the suppliedDateTimeto UTC.- Parameters:
datetime- the date time (may benull)- Returns:
- the UTC time (or
null
-
max
public static org.joda.time.DateTime max(org.joda.time.DateTime dt1, org.joda.time.DateTime dt2)Find the max of two dates (null safe)- Parameters:
dt1- firstDateTimedt2- secondDateTime- Returns:
- Max of two dates
-
getDaysSince
public static int getDaysSince(org.joda.time.DateTime start, org.joda.time.DateTime end)Get days between the givenDateTimes- Parameters:
start- StartDateTimeend- EndDateTime- Returns:
- Days between the two
DateTimes
-
getDaysSinceWithPrecision
public static double getDaysSinceWithPrecision(org.joda.time.DateTime start, org.joda.time.DateTime end)Get days between the givenDateTimes with precision- Parameters:
start- StartDateTimeend- EndDateTime- Returns:
- Days with precisions between the two
DateTimes
-
getMinutesSince
public static int getMinutesSince(org.joda.time.DateTime start, org.joda.time.DateTime end)Get minutes between the givenDateTimes- Parameters:
start- StartDateTimeend- EndDateTime- Returns:
- Minutes between the two
DateTimes
-
getSecondsSinceEpoch
public static double getSecondsSinceEpoch(org.joda.time.DateTime time)
Get seconds since epoch- Parameters:
time-DateTimeto get seconds for- Returns:
- Seconds since epoch
-
-