Class 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 Detail

      • convertDateValue

        public static java.util.Calendar convertDateValue​(DateValue aValue)
                                                   throws java.text.ParseException
        Converts a given date value to a Calendar.
        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 a Calendar.
        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 a Calendar.
        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 given Date (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 given Date (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 with SimpleDateFormat, but as far as I know there's no better method to achieve this result.
        Parameters:
        aFormat - the base format
        aDate - 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 nanoseconds
        aShortFormat - if true, a short format for the units is chosen
        aLongFormat - 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 a TimeDifference to a list of Pair with value plus TemporalUnit. This is not directly converting to a Duration 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 object
        aValueConverter - the value converter instance (necessary in case of calculated time difference values)
        Returns:
        the converted duration