Package de.gebit.integrity.utils
Class DateUtil
- java.lang.Object
-
- de.gebit.integrity.utils.DateUtil
-
public final class DateUtil extends java.lang.Object
A utility class to handle date/time parsing and other date stuff. Sorry for the uglyness, but date/time handling just IS ugly crap, especially when using Java's built-in functionality :-(.- Author:
- Rene Schneider - initial API and implementation
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.Calendar
convertDateAndTimeValue(DateAndTimeValue aValue)
Converts a given date and time to aCalendar
.static java.util.Calendar
convertDateValue(DateValue aValue)
Converts a given date value to aCalendar
.static java.lang.String
convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat)
Converts a duration in nanoseconds to a human-readable, nicely formatted string.static java.util.List<org.eclipse.xtext.util.Pair<java.lang.Long,java.time.temporal.TemporalUnit>>
convertTimeDifference(TimeDifference aDifference, ValueConverter aValueConverter)
static java.math.BigDecimal
convertTimeSetProgressionFactor(TimeSet aTimeSet, ValueConverter aValueConverter, ConversionContext aConversionContext)
Converts the timeset commands' progression factor.static java.util.Calendar
convertTimeValue(TimeValue aValue)
Converts a given time value to aCalendar
.static java.lang.String
formatDateWithMilliseconds(java.text.DateFormat aFormat, java.util.Date aDate)
Performs date formatting using the provided format, but injects millisecond precision if the millisecond value is not "000".static java.util.Date
stripDateFromTime(java.util.Date aTime)
Strips the date information from a givenDate
(sets all time fields to zero).static java.util.Date
stripTimeFromDate(java.util.Date aDate)
Strips the time information from a givenDate
(sets all time fields to zero).
-
-
-
Method Detail
-
convertDateValue
public static java.util.Calendar convertDateValue(DateValue aValue) throws java.text.ParseException
Converts a given date value to aCalendar
.- Parameters:
aValue
- the date value- Returns:
- the calendar set to the specified date
- Throws:
java.text.ParseException
- if the value cannot be parsed because it is of wrong format or depicts an illegal date/time
-
convertTimeValue
public static java.util.Calendar convertTimeValue(TimeValue aValue) throws java.text.ParseException
Converts a given time value to aCalendar
.- Parameters:
aValue
- the time value- Returns:
- the calendar set to the specified date
- Throws:
java.text.ParseException
- if the value cannot be parsed because it is of wrong format or depicts an illegal date/time
-
convertDateAndTimeValue
public static java.util.Calendar convertDateAndTimeValue(DateAndTimeValue aValue) throws java.text.ParseException
Converts a given date and time to aCalendar
.- Parameters:
aValue
- the date value- Returns:
- the calendar set to the specified date
- Throws:
java.text.ParseException
- if the value cannot be parsed because it is of wrong format or depicts an illegal date/time
-
stripTimeFromDate
public static java.util.Date stripTimeFromDate(java.util.Date aDate)
Strips the time information from a givenDate
(sets all time fields to zero).- Parameters:
aDate
- the date- Returns:
- a new Date without time information
-
stripDateFromTime
public static java.util.Date stripDateFromTime(java.util.Date aTime)
Strips the date information from a givenDate
(sets all time fields to zero).- Parameters:
aTime
- the time- Returns:
- a new Time without the date information
-
formatDateWithMilliseconds
public static java.lang.String formatDateWithMilliseconds(java.text.DateFormat aFormat, java.util.Date aDate)
Performs date formatting using the provided format, but injects millisecond precision if the millisecond value is not "000". This is intended to be used with the standard date/time formats returned by DateFormat factory methods, which don't include milliseconds in most cases unfortunately. It employs a rather ugly hack to enhance the pattern which only works withSimpleDateFormat
, but as far as I know there's no better method to achieve this result.- Parameters:
aFormat
- the base formataDate
- the date to format- Returns:
- the formatted string
-
convertNanosecondTimespanToHumanReadableFormat
public static java.lang.String convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat)
Converts a duration in nanoseconds to a human-readable, nicely formatted string.- Parameters:
aTimespan
- the timespan in nanosecondsaShortFormat
- if true, a short format for the units is chosenaLongFormat
- if true, a long format for the units is chosen- Returns:
- the formatted string
-
convertTimeDifference
public static java.util.List<org.eclipse.xtext.util.Pair<java.lang.Long,java.time.temporal.TemporalUnit>> convertTimeDifference(TimeDifference aDifference, ValueConverter aValueConverter)
Converts aTimeDifference
to a list ofPair
with value plusTemporalUnit
. This is not directly converting to aDuration
because certain units' absolute length depends on the actual date/time (like months and years). Their length can only be evaluated when we know the "current" date/time.In case of an overall negative difference, all values are negated on conversion.
- Parameters:
aDifference
- the time difference objectaValueConverter
- the value converter instance (necessary in case of calculated time difference values)- Returns:
- the converted duration
-
convertTimeSetProgressionFactor
public static java.math.BigDecimal convertTimeSetProgressionFactor(TimeSet aTimeSet, ValueConverter aValueConverter, ConversionContext aConversionContext)
Converts the timeset commands' progression factor.- Parameters:
aTimeSet
-aValueConverter
-- Returns:
- Throws:
ParameterUtil.UnresolvableVariableException
UnexecutableException
-
-