Class DateUtils


  • public final class DateUtils
    extends java.lang.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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.time.format.DateTimeFormatter ISO_LOCAL_DATE_TIME
      Differs from DateTimeFormatter.ISO_LOCAL_DATE_TIME by using ' ' instead of 'T' to separate date/time.
      static BooleanProperty PROP_ISO_DATES
      Property to enable display of ISO dates globally.
      static java.util.TimeZone UTC
      The UTC time zone.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private DateUtils()
      Constructs a new DateUtils.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static boolean checkLayout​(java.lang.String text, java.lang.String pattern)
      Check text for a specified layout
      static java.util.Date cloneDate​(java.util.Date d)
      Null-safe date cloning method.
      static java.lang.String formatDate​(java.util.Date date, int dateStyle)
      Formats a date to be displayed to current user, based on user preferences.
      static java.lang.String formatDateTime​(java.util.Date datetime, int dateStyle, int timeStyle)
      Formats a date/time to be displayed to current user, based on user preferences.
      static java.lang.String formatTime​(java.util.Date time, int timeStyle)
      Formats a time to be displayed to current user, based on user preferences.
      static java.lang.String fromDate​(java.util.Date date)
      Formats a date to the XML UTC format regardless of current locale.
      static java.util.Date fromString​(java.lang.String str)
      Parses XML date quickly, regardless of current locale.
      static java.lang.String fromTimestamp​(int timestamp)
      Formats a date to the XML UTC format regardless of current locale.
      static java.lang.String fromTimestamp​(long timestamp)
      Formats a date to the XML UTC format regardless of current locale.
      static java.lang.String fromTimestampInMillis​(long timestamp)
      Formats a date to the XML UTC format regardless of current locale.
      static java.text.DateFormat getDateFormat​(int dateStyle)
      Returns the date format to be used for current user, based on user preferences.
      static java.time.format.DateTimeFormatter getDateFormatter​(java.time.format.FormatStyle dateStyle)
      Returns the date formatter to be used for current user, based on user preferences.
      static java.text.DateFormat getDateTimeFormat​(int dateStyle, int timeStyle)
      Returns the date/time format to be used for current user, based on user preferences.
      static java.time.format.DateTimeFormatter getDateTimeFormatter​(java.time.format.FormatStyle dateStyle, java.time.format.FormatStyle timeStyle)
      Returns the date/time formatter to be used for current user, based on user preferences.
      static java.text.DateFormat getTimeFormat​(int timeStyle)
      Returns the time format to be used for current user, based on user preferences.
      static java.time.format.DateTimeFormatter getTimeFormatter​(java.time.format.FormatStyle timeStyle)
      Returns the time formatter to be used for current user, based on user preferences.
      static java.text.SimpleDateFormat newIsoDateFormat()
      Returns a new SimpleDateFormat for date only, according to ISO 8601.
      private static int num​(char c)  
      static java.time.Instant parseInstant​(java.lang.String str)
      Parses the given date string quickly, regardless of current locale.
      private static int parsePart2​(java.lang.String str, int off)  
      private static int parsePart3​(java.lang.String str, int off)  
      private static int parsePart4​(java.lang.String str, int off)  
      private static int parsePart6​(java.lang.String str, int off)  
      static long tsFromString​(java.lang.String str)
      Parses XML date quickly, regardless of current locale.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UTC

        public static final java.util.TimeZone UTC
        The UTC time zone.
      • ISO_LOCAL_DATE_TIME

        private static final java.time.format.DateTimeFormatter ISO_LOCAL_DATE_TIME
        Differs from DateTimeFormatter.ISO_LOCAL_DATE_TIME by using ' ' instead of 'T' to separate date/time.
    • Constructor Detail

      • DateUtils

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

      • fromString

        public static java.util.Date fromString​(java.lang.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
        java.time.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​(java.lang.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
        java.time.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 java.time.Instant parseInstant​(java.lang.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 java.lang.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 java.lang.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 java.lang.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 java.lang.String fromDate​(java.util.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 java.util.Date cloneDate​(java.util.Date d)
        Null-safe date cloning method.
        Parameters:
        d - date to clone, or null
        Returns:
        cloned date, or null
        Since:
        11878
      • checkLayout

        private static boolean checkLayout​(java.lang.String text,
                                           java.lang.String pattern)
        Check text for a specified layout
        Parameters:
        text - The text to check
        pattern - The pattern to use
        Returns:
        true if the layout matches, otherwise false
      • num

        private static int num​(char c)
      • parsePart2

        private static int parsePart2​(java.lang.String str,
                                      int off)
      • parsePart3

        private static int parsePart3​(java.lang.String str,
                                      int off)
      • parsePart4

        private static int parsePart4​(java.lang.String str,
                                      int off)
      • parsePart6

        private static int parsePart6​(java.lang.String str,
                                      int off)
      • newIsoDateFormat

        public static java.text.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 java.text.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 java.time.format.DateTimeFormatter getDateFormatter​(java.time.format.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 java.lang.String formatDate​(java.util.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 java.text.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 java.time.format.DateTimeFormatter getTimeFormatter​(java.time.format.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 java.lang.String formatTime​(java.util.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 java.text.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 java.time.format.DateTimeFormatter getDateTimeFormatter​(java.time.format.FormatStyle dateStyle,
                                                                              java.time.format.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 java.lang.String formatDateTime​(java.util.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