Class Timestamp


  • public class Timestamp
    extends java.lang.Object
    This class provides static utility methods for parsing and formatting timestamps (date/time strings), time, and date strings. Formatting uses W3C / ISO 8601 timestamps, in the form "2013-10-02T15:30:00.00Z" format (timestamps) "yyyy-MM-ddZ" (dates) and "HH:mm:ss.SSSZ" (time). When parsing, the fractional part of the seconds value is optional, as is the time zone value.

    Use the parse*As*(String) methods to parse such strings into a Date or Java 8 ZonedDateTime value.

    Use the timestamp(*), timeString(*), and dateString(*) methods to format a Date or Java 8 ZonedDateTime or long in this format.

    All the methods use or result in UTC (GMT) time zone dates.

    Also includes methods for parsing and formatting HTTP Timestamps in the form "EEE, dd MMM yyyy HH:mm:ss zzz", as described in RFC-7231. This format requires 2-digit day-of-month and 4-digit years and the GMT time zone. Any milliseconds will be truncated and ignored when formatting.

    Use the parseHttpTimestampAs*(String) methods to parse such strings into a Date or Java 8 ZonedDateTime value.

    Use the httpTimestamp(*) methods to format a Date or Java 8 ZonedDateTime or long in this format. This format will always output "GMT" time zone dates.

    Use truncateDateToSeconds(Date) to truncate a Date that might include milliseconds into a corresponding Date truncated to the previous whole second value.

    See Also:
    Section 7.1.1.1 of RFC 7231
    • Constructor Summary

      Constructors 
      Constructor Description
      Timestamp()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String dateString​(long millisSinceJavaEpoch)
      Format a datetime value millisSinceJavaEpoch as a W3C/ISO 8601 compliant time string using "yyyy-MM-dd" format
      static java.lang.String dateString​(java.time.ZonedDateTime date)
      Format a ZonedDateTime as a W3C/ISO 8601 compliant time string using "yyyy-MM-dd" format
      static java.lang.String dateString​(java.util.Date date)
      Format a Date as a W3C/ISO 8601 compliant time string using "yyyy-MM-dd" format
      static java.lang.String httpTimestamp​(long millisSinceJavaEpoch)
      Format a datetime value millisSinceJavaEpoch as a RFC 7231 compliant HTTP Timestamp string using "EEE, dd MMM yyyy HH:mm:ss GMT" format
      static java.lang.String httpTimestamp​(java.time.ZonedDateTime dateTime)
      Format a DateTime as an RFC 7231 compliant HTTP Timestamp string using "EEE, dd MMM yyyy HH:mm:ss GMT" format.
      static java.lang.String httpTimestamp​(java.util.Date date)
      Format a Date as a RFC 7231 compliant HTTP Timestamp string using "EEE, dd MMM yyyy HH:mm:ss GMT" format
      static java.time.ZonedDateTime parseDateAsDateTime​(java.lang.String date)
      Parse a timestamp string using "yyyy-MM-ddZ" The the time zone are.
      static java.util.Date parseHttpTimestampAsDate​(java.lang.String httpTimestamp)
      Parse an httpTimestamp string using RFC 7231 "EEE, dd MMM yyyy HH:mm:ss GMT" format (must be in GMT timezone).
      static java.time.ZonedDateTime parseHttpTimestampAsDateTime​(java.lang.String httpTimestamp)
      Parse an httpTimestamp string using RFC 7231 "EEE, dd MMM yyyy HH:mm:ss GMT" format (must be in GMT timezone).
      static java.util.Date parseStringAsDate​(java.lang.String string)
      Parse a string that may represent a date, a time or a timestamp and return a Date.
      static java.time.ZonedDateTime parseTimeAsDateTime​(java.lang.String time)
      Parse a time string using "HH:mm:ss.SSSZ"; the fractional part of the seconds and time zone are.
      static java.util.Date parseTimestampAsDate​(java.lang.String timestamp)
      Parse a timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format; the fractional part of the seconds and the time zone are optional.
      static java.time.ZonedDateTime parseTimestampAsDateTime​(java.lang.String timestamp)
      Parse a timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" or "yyyy-MM-dd'T'HH:mm:ssZ" format.
      static java.time.Instant parseTimestampAsInstant​(java.lang.String timestamp)
      Parse a timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format; the fractional part of the seconds and the time zone are optional.
      static java.lang.String timestamp​(long millisSinceJavaEpoch)
      Format datetime value millisSinceJavaEpoch as a W3C/ISO 8601 compliant timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format
      static java.lang.String timestamp​(java.time.Instant instant)
      Format datetime value millisSinceJavaEpoch as a W3C/ISO 8601 compliant timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format
      static java.lang.String timestamp​(java.time.ZonedDateTime timestamp)
      Format a DateTime timestamp as a W3C/ISO 8601 compliant timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format
      static java.lang.String timestamp​(java.util.Date timestamp)
      Format a Date timestamp value as a W3C/ISO 8601 compliant timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format
      static java.lang.String timeString​(long millisSinceJavaEpoch)
      Format datetime value millisSinceJavaEpoch as a W3C/ISO 8601 compliant timestamp string using "HH:mm:ss.SSSZ" format
      static java.lang.String timeString​(java.time.ZonedDateTime time)
      Format a ZonedDateTime value as a W3C/ISO 8601 compliant timestamp string using "HH:mm:ss.SSSZ" format
      static java.lang.String timeString​(java.util.Date time)
      Format a Date value as a W3C/ISO 8601 compliant timestamp string using "HH:mm:ss.SSSZ" format
      static java.util.Date truncateDateToSeconds​(java.util.Date preciseDate)
      Produces a new java.util.Date object with the same value as the input preciseDate, but with any milliseconds (or nanoseconds) truncated off, always rounding down to the previous whole second value.
      • Methods inherited from class java.lang.Object

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

      • EXPECTED_HTTP_TIMESTAMP_FORMAT

        public static final java.lang.String EXPECTED_HTTP_TIMESTAMP_FORMAT
        See Also:
        Constant Field Values
      • EXPECTED_TIMESTAMP_FORMAT

        public static final java.lang.String EXPECTED_TIMESTAMP_FORMAT
        See Also:
        Constant Field Values
    • Constructor Detail

      • Timestamp

        public Timestamp()
    • Method Detail

      • parseStringAsDate

        public static java.util.Date parseStringAsDate​(java.lang.String string)
                                                throws java.lang.IllegalArgumentException
        Parse a string that may represent a date, a time or a timestamp and return a Date. If no parsing is successful, throw an IllegalArgumentException.
        Parameters:
        string - The input string. This should be in ISO8601 format and represent a valid date, time or timestamp.
        Returns:
        The Date value that the string represents
        Throws:
        java.lang.IllegalArgumentException - if the string cannot be parsed.
      • parseTimestampAsDate

        public static java.util.Date parseTimestampAsDate​(java.lang.String timestamp)
                                                   throws java.lang.IllegalArgumentException
        Parse a timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format; the fractional part of the seconds and the time zone are optional. Use the token Z to denote UTC (GMT).
        Parameters:
        timestamp - a timestamp string
        Returns:
        the Date corresponding to the timestamp, in UTC time zone.
        Throws:
        java.lang.IllegalArgumentException - if the timestamp string is not properly formatted
      • parseTimestampAsInstant

        public static java.time.Instant parseTimestampAsInstant​(java.lang.String timestamp)
        Parse a timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format; the fractional part of the seconds and the time zone are optional. Use the token Z to denote UTC (GMT).
        Parameters:
        timestamp - a timestamp string
        Returns:
        the Instant corresponding to the timestamp, in UTC time zone.
        Throws:
        java.lang.IllegalArgumentException - if the timestamp string is not properly formatted
      • parseDateAsDateTime

        public static java.time.ZonedDateTime parseDateAsDateTime​(java.lang.String date)
                                                           throws java.lang.IllegalArgumentException
        Parse a timestamp string using "yyyy-MM-ddZ" The the time zone are. Use the token Z to denote UTC (GMT).
        Parameters:
        date - a timestamp string
        Returns:
        the DateTime corresponding to the date string, in UTC time zone.
        Throws:
        java.lang.IllegalArgumentException - if the date string is not properly formatted
      • parseTimeAsDateTime

        public static java.time.ZonedDateTime parseTimeAsDateTime​(java.lang.String time)
                                                           throws java.lang.IllegalArgumentException
        Parse a time string using "HH:mm:ss.SSSZ"; the fractional part of the seconds and time zone are. Use the token Z to denote UTC (GMT).
        Parameters:
        time - a timestamp string
        Returns:
        the DateTime corresponding to the time string, in UTC time zone.
        Throws:
        java.lang.IllegalArgumentException - if the time string is not properly formatted
      • parseTimestampAsDateTime

        public static java.time.ZonedDateTime parseTimestampAsDateTime​(java.lang.String timestamp)
                                                                throws java.lang.IllegalArgumentException
        Parse a timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" or "yyyy-MM-dd'T'HH:mm:ssZ" format. The fractional part of the seconds is optional, but the time zone is required. Use the token Z to denote UTC (GMT).
        Parameters:
        timestamp - a timestamp string
        Returns:
        the DateTime corresponding to the timestamp, in UTC time zone.
        Throws:
        java.lang.IllegalArgumentException - if the timestamp string is not properly formatted
      • parseHttpTimestampAsDate

        public static java.util.Date parseHttpTimestampAsDate​(java.lang.String httpTimestamp)
                                                       throws java.lang.IllegalArgumentException
        Parse an httpTimestamp string using RFC 7231 "EEE, dd MMM yyyy HH:mm:ss GMT" format (must be in GMT timezone). Negative years (before 0000) are not supported.
        Parameters:
        httpTimestamp - a string using HTTP format in GMT timezone
        Returns:
        the Date corresponding to the httpTimestamp, in GMT/UTC time zone.
        Throws:
        java.lang.IllegalArgumentException - if the httpTimestamp string is not properly formatted
      • parseHttpTimestampAsDateTime

        public static java.time.ZonedDateTime parseHttpTimestampAsDateTime​(java.lang.String httpTimestamp)
                                                                    throws java.lang.IllegalArgumentException
        Parse an httpTimestamp string using RFC 7231 "EEE, dd MMM yyyy HH:mm:ss GMT" format (must be in GMT timezone). Negative years (before 0000) are not supported.
        Parameters:
        httpTimestamp - a string using HTTP format in GMT timezone
        Returns:
        the ZonedDateTime corresponding to the httpTimestamp, in UTC time zone.
        Throws:
        java.lang.IllegalArgumentException - if the httpTimestamp string is not properly formatted
      • timestamp

        public static java.lang.String timestamp​(java.time.ZonedDateTime timestamp)
        Format a DateTime timestamp as a W3C/ISO 8601 compliant timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format
        Parameters:
        timestamp - a timestamp string
        Returns:
        the Date corresponding to the timestamp, in UTC time zone.
      • timestamp

        public static java.lang.String timestamp​(java.util.Date timestamp)
        Format a Date timestamp value as a W3C/ISO 8601 compliant timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format
        Parameters:
        timestamp - a date/time value
        Returns:
        the formatted date/time string, in UTC time zone
      • timestamp

        public static java.lang.String timestamp​(long millisSinceJavaEpoch)
        Format datetime value millisSinceJavaEpoch as a W3C/ISO 8601 compliant timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format
        Parameters:
        millisSinceJavaEpoch - milliseconds since the Java epoch (12:00AM, Jan 1, 1970 UTC)
        Returns:
        the formatted date/time string, in UTC.
      • timestamp

        public static java.lang.String timestamp​(java.time.Instant instant)
        Format datetime value millisSinceJavaEpoch as a W3C/ISO 8601 compliant timestamp string using "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format
        Parameters:
        instant - an Instant value
        Returns:
        the formatted date/time string, in UTC.
      • timeString

        public static java.lang.String timeString​(java.time.ZonedDateTime time)
        Format a ZonedDateTime value as a W3C/ISO 8601 compliant timestamp string using "HH:mm:ss.SSSZ" format
        Parameters:
        time - a timestamp string
        Returns:
        the formatted time string, in UTC time zone
      • timeString

        public static java.lang.String timeString​(java.util.Date time)
        Format a Date value as a W3C/ISO 8601 compliant timestamp string using "HH:mm:ss.SSSZ" format
        Parameters:
        time - a time value
        Returns:
        the formatted time string, in UTC time zone
      • timeString

        public static java.lang.String timeString​(long millisSinceJavaEpoch)
        Format datetime value millisSinceJavaEpoch as a W3C/ISO 8601 compliant timestamp string using "HH:mm:ss.SSSZ" format
        Parameters:
        millisSinceJavaEpoch - milliseconds since the Java epoch (12:00AM, Jan 1, 1970 UTC)
        Returns:
        the formatted time string, in UTC.
      • dateString

        public static java.lang.String dateString​(java.time.ZonedDateTime date)
        Format a ZonedDateTime as a W3C/ISO 8601 compliant time string using "yyyy-MM-dd" format
        Parameters:
        date - a date value
        Returns:
        the formatted time string, in UTC time zone
      • dateString

        public static java.lang.String dateString​(java.util.Date date)
        Format a Date as a W3C/ISO 8601 compliant time string using "yyyy-MM-dd" format
        Parameters:
        date - a time value
        Returns:
        the formatted time string, in UTC time zone
      • dateString

        public static java.lang.String dateString​(long millisSinceJavaEpoch)
        Format a datetime value millisSinceJavaEpoch as a W3C/ISO 8601 compliant time string using "yyyy-MM-dd" format
        Parameters:
        millisSinceJavaEpoch - milliseconds since the Java epoch (12:00AM, Jan 1, 1970 UTC)
        Returns:
        the formatted time string, in UTC.
      • httpTimestamp

        public static java.lang.String httpTimestamp​(java.time.ZonedDateTime dateTime)
        Format a DateTime as an RFC 7231 compliant HTTP Timestamp string using "EEE, dd MMM yyyy HH:mm:ss GMT" format. Negative years (before 0000) are not supported.
        Parameters:
        dateTime - ZonedDateTime to format
        Returns:
        the String corresponding to the HTTP timestamp, in GMT time zone.
      • httpTimestamp

        public static java.lang.String httpTimestamp​(java.util.Date date)
        Format a Date as a RFC 7231 compliant HTTP Timestamp string using "EEE, dd MMM yyyy HH:mm:ss GMT" format
        Parameters:
        date - a date value including time
        Returns:
        the formatted HTTP Timestamp string, in GMT time zone
      • httpTimestamp

        public static java.lang.String httpTimestamp​(long millisSinceJavaEpoch)
        Format a datetime value millisSinceJavaEpoch as a RFC 7231 compliant HTTP Timestamp string using "EEE, dd MMM yyyy HH:mm:ss GMT" format
        Parameters:
        millisSinceJavaEpoch - milliseconds since the Java epoch (12:00:00AM, Jan 01, 1970 GMT)
        Returns:
        the formatted HTTP Timestamp string, in GMT.
      • truncateDateToSeconds

        public static java.util.Date truncateDateToSeconds​(java.util.Date preciseDate)
        Produces a new java.util.Date object with the same value as the input preciseDate, but with any milliseconds (or nanoseconds) truncated off, always rounding down to the previous whole second value. A null input value will result in a null output value.
        Parameters:
        preciseDate - a Date value (possibly including milliseconds)
        Returns:
        a new Date truncated to the previous whole second value or null if input is null