Class DateUtil

java.lang.Object
org.jpos.util.DateUtil

public class DateUtil extends Object
Convenience helpers for parsing and formatting dates and times in a small set of jPOS-specific patterns. Methods that take null return null so call sites do not need to guard themselves.
  • Constructor Details

    • DateUtil

      public DateUtil()
      Utility class; instances carry no state.
  • Method Details

    • parseDate

      public static Date parseDate(String s) throws ParseException
      Parses s using the US short date format.
      Parameters:
      s - date string, or null
      Returns:
      parsed date, or null when s is null
      Throws:
      ParseException - if the string cannot be parsed
    • parseDate_mmddyyyy

      public static Date parseDate_mmddyyyy(String s) throws ParseException
      Parses s using the MM/dd/yyyy pattern.
      Parameters:
      s - date string, or null
      Returns:
      parsed date, or null when s is null
      Throws:
      ParseException - if the string cannot be parsed
    • parseDate_yyyymmdd

      public static Date parseDate_yyyymmdd(String s) throws ParseException
      Parses s using the yyyyMMdd pattern.
      Parameters:
      s - date string, or null
      Returns:
      parsed date, or null when s is null
      Throws:
      ParseException - if the string cannot be parsed
    • parseDate_mmddyy

      public static Date parseDate_mmddyy(String s) throws ParseException
      Parses s using the MM/dd/yy pattern.
      Parameters:
      s - date string, or null
      Returns:
      parsed date, or null when s is null
      Throws:
      ParseException - if the string cannot be parsed
    • parseDateTime

      public static Date parseDateTime(String s) throws ParseException
      Parses s using the US short date / medium time format.
      Parameters:
      s - date-time string, or null
      Returns:
      parsed date, or null when s is null
      Throws:
      ParseException - if the string cannot be parsed
    • parseDateTime_mmddyyyy

      Parses s using the MM/dd/yyyy HH:mm:ss pattern.
      Parameters:
      s - date-time string, or null
      Returns:
      parsed date, or null when s is null
      Throws:
      ParseException - if the string cannot be parsed
    • parseTimestamp

      public static Date parseTimestamp(String s) throws ParseException
      Parses s using the MM/dd/yyyy HH:mm:ss pattern.
      Parameters:
      s - timestamp string, or null
      Returns:
      parsed date, or null when s is null
      Throws:
      ParseException - if the string cannot be parsed
    • parseDateTime_mmddyyyy

      public static Date parseDateTime_mmddyyyy(String s, String tzString) throws ParseException
      Parses s using the MM/dd/yyyy HH:mm:ss pattern in the given time zone.
      Parameters:
      s - date-time string, or null
      tzString - time-zone identifier accepted by TimeZone.getTimeZone(String); null keeps the JVM default zone
      Returns:
      parsed date, or null when s is null
      Throws:
      ParseException - if the string cannot be parsed
    • dateToString

      public static String dateToString(Date d)
      Formats d using the US short date format.
      Parameters:
      d - date, or null
      Returns:
      formatted string, or null when d is null
    • dateToString_mmddyyyy

      public static String dateToString_mmddyyyy(Date d)
      Formats d using the MM/dd/yyyy pattern.
      Parameters:
      d - date, or null
      Returns:
      formatted string, or null when d is null
    • dateTimeToString

      public static String dateTimeToString(Date d)
      Formats d using the US short date / medium time format.
      Parameters:
      d - date, or null
      Returns:
      formatted string, or null when d is null
    • dateTimeToString

      public static String dateTimeToString(Date d, String tzString)
      Formats d using the US short date / medium time format in the given time zone.
      Parameters:
      d - date, or null
      tzString - time-zone identifier; null keeps the JVM default zone
      Returns:
      formatted string, or null when d is null
    • dateTimeToString_mmddyyyy

      Formats d using the MM/dd/yyyy HH:mm:ss pattern.
      Parameters:
      d - date, or null
      Returns:
      formatted string, or null when d is null
    • timestamp

      public static String timestamp(Date d)
      Formats d using the MM/dd/yyyy HH:mm:ss pattern.
      Parameters:
      d - date, or null
      Returns:
      formatted string, or null when d is null
    • postdate

      public static String postdate(Date d)
      Formats d using the MM/dd/yyyy pattern.
      Parameters:
      d - date, or null
      Returns:
      formatted string, or null when d is null
    • parseDateTime

      public static Date parseDateTime(String d, String t)
      Parses an MMDDYY date and HHMMSS time, choosing the century closest to "now" so two-digit years near a century boundary round to the correct year.
      Parameters:
      d - date in MMDDYY
      t - time in HHMMSS
      Returns:
      parsed date
    • parseTime

      public static Date parseTime(String t)
      Parses an HHMM or HHMMSS time using today as the date.
      Parameters:
      t - time string
      Returns:
      parsed date with today's year/month/day
    • parseTime

      public static Date parseTime(String t, Date now)
      Parses an HHMM or HHMMSS time using now as the date portion.
      Parameters:
      t - time string
      now - date supplying the year/month/day
      Returns:
      parsed date with the supplied date and the parsed time
    • dateToString

      public static String dateToString(Date d, String tzString)
      Formats d using the JVM default date format in the given time zone.
      Parameters:
      d - date
      tzString - time-zone identifier; null keeps the JVM default zone
      Returns:
      formatted date string
    • timeToString

      public static String timeToString(Date d, String tzString)
      Formats the time portion of d using the JVM short time format in the given time zone.
      Parameters:
      d - date
      tzString - time-zone identifier; null keeps the JVM default zone
      Returns:
      formatted time string
    • timeToString

      public static String timeToString(Date d)
      Formats the time portion of d using the JVM short time format.
      Parameters:
      d - date
      Returns:
      formatted time string
    • toDays

      public static String toDays(long period)
      Renders a duration in milliseconds as a compact 1h2m3s-style string.
      Parameters:
      period - duration in milliseconds
      Returns:
      human-readable duration; always includes a seconds component, and includes hours/minutes only when non-zero