Class DateUtils


  • public final class DateUtils
    extends Object
    A static utility class dealing with:
    • parsing XML date quickly and formatting a date to the XML UTC format regardless of current locale
    • providing a single entry point for formatting dates to be displayed in JOSM GUI, based on user preferences
    • Constructor Detail

      • DateUtils

        private DateUtils()
        Constructs a new DateUtils.
    • Method Detail

      • fromString

        public static Date fromString​(String str)
        Parses XML date quickly, regardless of current locale.
        Parameters:
        str - The XML date as string
        Returns:
        The date
        Throws:
        UncheckedParseException - if the date does not match any of the supported date formats
        DateTimeException - if the value of any field is out of range, or if the day-of-month is invalid for the month-year
      • tsFromString

        public static long tsFromString​(String str)
        Parses XML date quickly, regardless of current locale.
        Parameters:
        str - The XML date as string
        Returns:
        The date in milliseconds since epoch
        Throws:
        UncheckedParseException - if the date does not match any of the supported date formats
        DateTimeException - if the value of any field is out of range, or if the day-of-month is invalid for the month-year
      • parseInstant

        public static Instant parseInstant​(String str)
        Parses the given date string quickly, regardless of current locale.
        Parameters:
        str - the date string
        Returns:
        the parsed instant
        Throws:
        UncheckedParseException - if the date does not match any of the supported date formats
      • fromTimestamp

        public static String fromTimestamp​(long timestamp)
        Formats a date to the XML UTC format regardless of current locale.
        Parameters:
        timestamp - number of seconds since the epoch
        Returns:
        The formatted date
        Since:
        14055
      • fromTimestampInMillis

        public static String fromTimestampInMillis​(long timestamp)
        Formats a date to the XML UTC format regardless of current locale.
        Parameters:
        timestamp - number of milliseconds since the epoch
        Returns:
        The formatted date
        Since:
        14434
      • fromTimestamp

        public static String fromTimestamp​(int timestamp)
        Formats a date to the XML UTC format regardless of current locale.
        Parameters:
        timestamp - number of seconds since the epoch
        Returns:
        The formatted date
      • fromDate

        public static String fromDate​(Date date)
        Formats a date to the XML UTC format regardless of current locale.
        Parameters:
        date - The date to format
        Returns:
        The formatted date
      • cloneDate

        public static Date cloneDate​(Date d)
        Null-safe date cloning method.
        Parameters:
        d - date to clone, or null
        Returns:
        cloned date, or null
        Since:
        11878
      • num

        private static int num​(char c)
      • newIsoDateFormat

        public static SimpleDateFormat newIsoDateFormat()
        Returns a new SimpleDateFormat for date only, according to ISO 8601.
        Returns:
        a new ISO 8601 date format, for date only.
        Since:
        7299
      • getDateFormat

        public static DateFormat getDateFormat​(int dateStyle)
        Returns the date format to be used for current user, based on user preferences.
        Parameters:
        dateStyle - The date style as described in DateFormat.getDateInstance(). Ignored if "ISO dates" option is set
        Returns:
        The date format
        Since:
        7299
      • getDateFormatter

        public static DateTimeFormatter getDateFormatter​(FormatStyle dateStyle)
        Returns the date formatter to be used for current user, based on user preferences.
        Parameters:
        dateStyle - The date style. Ignored if "ISO dates" option is set.
        Returns:
        The date format
      • formatDate

        public static String formatDate​(Date date,
                                        int dateStyle)
        Formats a date to be displayed to current user, based on user preferences.
        Parameters:
        date - The date to display. Must not be null
        dateStyle - The date style as described in DateFormat.getDateInstance(). Ignored if "ISO dates" option is set
        Returns:
        The formatted date
        Since:
        7299
      • getTimeFormat

        public static DateFormat getTimeFormat​(int timeStyle)
        Returns the time format to be used for current user, based on user preferences.
        Parameters:
        timeStyle - The time style as described in DateFormat.getTimeInstance(). Ignored if "ISO dates" option is set
        Returns:
        The time format
        Since:
        7299
      • getTimeFormatter

        public static DateTimeFormatter getTimeFormatter​(FormatStyle timeStyle)
        Returns the time formatter to be used for current user, based on user preferences.
        Parameters:
        timeStyle - The time style. Ignored if "ISO dates" option is set.
        Returns:
        The time format
      • formatTime

        public static String formatTime​(Date time,
                                        int timeStyle)
        Formats a time to be displayed to current user, based on user preferences.
        Parameters:
        time - The time to display. Must not be null
        timeStyle - The time style as described in DateFormat.getTimeInstance(). Ignored if "ISO dates" option is set
        Returns:
        The formatted time
        Since:
        7299
      • getDateTimeFormat

        public static DateFormat getDateTimeFormat​(int dateStyle,
                                                   int timeStyle)
        Returns the date/time format to be used for current user, based on user preferences.
        Parameters:
        dateStyle - The date style as described in DateFormat.getDateTimeInstance(). Ignored if "ISO dates" option is set
        timeStyle - The time style as described in DateFormat.getDateTimeInstance. Ignored if "ISO dates" option is set
        Returns:
        The date/time format
        Since:
        7299
      • getDateTimeFormatter

        public static DateTimeFormatter getDateTimeFormatter​(FormatStyle dateStyle,
                                                             FormatStyle timeStyle)
        Returns the date/time formatter to be used for current user, based on user preferences.
        Parameters:
        dateStyle - The date style. Ignored if "ISO dates" option is set.
        timeStyle - The time style. Ignored if "ISO dates" option is set.
        Returns:
        The date/time format
      • formatDateTime

        public static String formatDateTime​(Date datetime,
                                            int dateStyle,
                                            int timeStyle)
        Formats a date/time to be displayed to current user, based on user preferences.
        Parameters:
        datetime - The date/time to display. Must not be null
        dateStyle - The date style as described in DateFormat.getDateTimeInstance(). Ignored if "ISO dates" option is set
        timeStyle - The time style as described in DateFormat.getDateTimeInstance. Ignored if "ISO dates" option is set
        Returns:
        The formatted date/time
        Since:
        7299