Class UtilDateTime

java.lang.Object
org.apache.ofbiz.base.util.UtilDateTime

public final class UtilDateTime extends Object
Utility class for handling java.util.Date, the java.sql data/time classes and related
  • Method Details

    • getInterval

      public static double getInterval(Date from, Date thru)
    • getIntervalInDays

      public static int getIntervalInDays(Timestamp from, Timestamp thru)
    • addDaysToTimestamp

      public static Timestamp addDaysToTimestamp(Timestamp start, int days)
    • addDaysToTimestamp

      public static Timestamp addDaysToTimestamp(Timestamp start, Double days)
    • getInterval

      public static double getInterval(Timestamp from, Timestamp thru)
    • formatInterval

      public static String formatInterval(Date from, Date thru, int count, Locale locale)
    • formatInterval

      public static String formatInterval(Date from, Date thru, Locale locale)
    • formatInterval

      public static String formatInterval(Timestamp from, Timestamp thru, int count, Locale locale)
    • formatInterval

      public static String formatInterval(Timestamp from, Timestamp thru, Locale locale)
    • formatInterval

      public static String formatInterval(long interval, int count, Locale locale)
    • formatInterval

      public static String formatInterval(long interval, Locale locale)
    • formatInterval

      public static String formatInterval(double interval, Locale locale)
    • formatInterval

      public static String formatInterval(double interval, int count, Locale locale)
    • nowTimestamp

      public static Timestamp nowTimestamp()
      Return a Timestamp for right now
      Returns:
      Timestamp for right now
    • getTimestamp

      public static Timestamp getTimestamp(long time)
      Convert a millisecond value to a Timestamp.
      Parameters:
      time - millsecond value
      Returns:
      Timestamp
    • getTimestamp

      public static Timestamp getTimestamp(String milliSecs) throws NumberFormatException
      Convert a millisecond value to a Timestamp.
      Parameters:
      milliSecs - millsecond value
      Returns:
      Timestamp
      Throws:
      NumberFormatException
    • nowAsString

      public static String nowAsString()
      Returns currentTimeMillis as String
      Returns:
      String(currentTimeMillis)
    • nowDateString

      public static String nowDateString()
      Return a string formatted as yyyyMMddHHmmss
      Returns:
      String formatted for right now
    • nowDateString

      public static String nowDateString(String format)
      Return a string formatted as format
      Returns:
      String formatted for right now
    • nowDate

      public static Date nowDate()
      Return a Date for right now
      Returns:
      Date for right now
    • getDayStart

      public static Timestamp getDayStart(Timestamp stamp)
    • getDayStart

      public static Timestamp getDayStart(Timestamp stamp, int daysLater)
    • getNextDayStart

      public static Timestamp getNextDayStart(Timestamp stamp)
    • getDayEnd

      public static Timestamp getDayEnd(Timestamp stamp)
    • getDayEnd

      public static Timestamp getDayEnd(Timestamp stamp, Long daysLater)
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp)
      Return the date for the first day of the year
      Parameters:
      stamp -
      Returns:
      java.sql.Timestamp
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp, int daysLater)
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp, int daysLater, int yearsLater)
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp, int daysLater, int monthsLater, int yearsLater)
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp, Number daysLater, Number monthsLater, Number yearsLater)
    • getMonthStart

      public static Timestamp getMonthStart(Timestamp stamp)
      Return the date for the first day of the month
      Parameters:
      stamp -
      Returns:
      java.sql.Timestamp
    • getMonthStart

      public static Timestamp getMonthStart(Timestamp stamp, int daysLater)
    • getMonthStart

      public static Timestamp getMonthStart(Timestamp stamp, int daysLater, int monthsLater)
    • getWeekStart

      public static Timestamp getWeekStart(Timestamp stamp)
      Return the date for the first day of the week
      Parameters:
      stamp -
      Returns:
      java.sql.Timestamp
    • getWeekStart

      public static Timestamp getWeekStart(Timestamp stamp, int daysLater)
    • getWeekStart

      public static Timestamp getWeekStart(Timestamp stamp, int daysLater, int weeksLater)
    • getWeekEnd

      public static Timestamp getWeekEnd(Timestamp stamp)
    • toCalendar

      public static com.ibm.icu.util.Calendar toCalendar(Timestamp stamp)
    • toSqlDate

      public static Date toSqlDate(String date)
      Converts a date String into a java.sql.Date
      Parameters:
      date - The date String: MM/DD/YYYY
      Returns:
      A java.sql.Date made from the date String
    • toSqlDate

      public static Date toSqlDate(String monthStr, String dayStr, String yearStr)
      Makes a java.sql.Date from separate Strings for month, day, year
      Parameters:
      monthStr - The month String
      dayStr - The day String
      yearStr - The year String
      Returns:
      A java.sql.Date made from separate Strings for month, day, year
    • toSqlDate

      public static Date toSqlDate(int month, int day, int year)
      Makes a java.sql.Date from separate ints for month, day, year
      Parameters:
      month - The month int
      day - The day int
      year - The year int
      Returns:
      A java.sql.Date made from separate ints for month, day, year
    • toSqlTime

      public static Time toSqlTime(String time)
      Converts a time String into a java.sql.Time
      Parameters:
      time - The time String: either HH:MM or HH:MM:SS
      Returns:
      A java.sql.Time made from the time String
    • toSqlTime

      public static Time toSqlTime(String hourStr, String minuteStr, String secondStr)
      Makes a java.sql.Time from separate Strings for hour, minute, and second.
      Parameters:
      hourStr - The hour String
      minuteStr - The minute String
      secondStr - The second String
      Returns:
      A java.sql.Time made from separate Strings for hour, minute, and second.
    • toSqlTime

      public static Time toSqlTime(int hour, int minute, int second)
      Makes a java.sql.Time from separate ints for hour, minute, and second.
      Parameters:
      hour - The hour int
      minute - The minute int
      second - The second int
      Returns:
      A java.sql.Time made from separate ints for hour, minute, and second.
    • toTimestamp

      public static Timestamp toTimestamp(String dateTime)
      Converts a date and time String into a Timestamp
      Parameters:
      dateTime - A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
      Returns:
      The corresponding Timestamp
    • toTimestamp

      public static Timestamp toTimestamp(String date, String time)
      Converts a date String and a time String into a Timestamp
      Parameters:
      date - The date String: MM/DD/YYYY
      time - The time String: either HH:MM or HH:MM:SS
      Returns:
      A Timestamp made from the date and time Strings
    • toTimestamp

      public static Timestamp toTimestamp(String monthStr, String dayStr, String yearStr, String hourStr, String minuteStr, String secondStr)
      Makes a Timestamp from separate Strings for month, day, year, hour, minute, and second.
      Parameters:
      monthStr - The month String
      dayStr - The day String
      yearStr - The year String
      hourStr - The hour String
      minuteStr - The minute String
      secondStr - The second String
      Returns:
      A Timestamp made from separate Strings for month, day, year, hour, minute, and second.
    • toTimestamp

      public static Timestamp toTimestamp(int month, int day, int year, int hour, int minute, int second)
      Makes a Timestamp from separate ints for month, day, year, hour, minute, and second.
      Parameters:
      month - The month int
      day - The day int
      year - The year int
      hour - The hour int
      minute - The minute int
      second - The second int
      Returns:
      A Timestamp made from separate ints for month, day, year, hour, minute, and second.
    • toTimestamp

      public static Timestamp toTimestamp(Date date)
    • toDate

      public static Date toDate(String dateTime)
      Converts a date and time String into a Date
      Parameters:
      dateTime - A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
      Returns:
      The corresponding Date
    • toDate

      public static Date toDate(String date, String time)
      Converts a date String and a time String into a Date
      Parameters:
      date - The date String: MM/DD/YYYY
      time - The time String: either HH:MM or HH:MM:SS
      Returns:
      A Date made from the date and time Strings
    • toDate

      public static Date toDate(String monthStr, String dayStr, String yearStr, String hourStr, String minuteStr, String secondStr)
      Makes a Date from separate Strings for month, day, year, hour, minute, and second.
      Parameters:
      monthStr - The month String
      dayStr - The day String
      yearStr - The year String
      hourStr - The hour String
      minuteStr - The minute String
      secondStr - The second String
      Returns:
      A Date made from separate Strings for month, day, year, hour, minute, and second.
    • toDate

      public static Date toDate(int month, int day, int year, int hour, int minute, int second)
      Makes a Date from separate ints for month, day, year, hour, minute, and second.
      Parameters:
      month - The month int
      day - The day int
      year - The year int
      hour - The hour int
      minute - The minute int
      second - The second int
      Returns:
      A Date made from separate ints for month, day, year, hour, minute, and second.
    • toDateString

      public static String toDateString(Date date, String format)
      Makes a date String in the given from a Date
      Parameters:
      date - The Date
      Returns:
      A date String in the given format
    • toDateString

      public static String toDateString(Date date)
      Makes a date String in the format MM/DD/YYYY from a Date
      Parameters:
      date - The Date
      Returns:
      A date String in the format MM/DD/YYYY
    • toTimeString

      public static String toTimeString(Date date)
      Makes a time String in the format HH:MM:SS from a Date. If the seconds are 0, then the output is in HH:MM.
      Parameters:
      date - The Date
      Returns:
      A time String in the format HH:MM:SS or HH:MM
    • toTimeString

      public static String toTimeString(int hour, int minute, int second)
      Makes a time String in the format HH:MM:SS from a separate ints for hour, minute, and second. If the seconds are 0, then the output is in HH:MM.
      Parameters:
      hour - The hour int
      minute - The minute int
      second - The second int
      Returns:
      A time String in the format HH:MM:SS or HH:MM
    • toDateTimeString

      public static String toDateTimeString(Date date)
      Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS" from a Date. If the seconds are 0 they are left off.
      Parameters:
      date - The Date
      Returns:
      A combined data and time string in the format "MM/DD/YYYY HH:MM:SS" where the seconds are left off if they are 0.
    • toGmtTimestampString

      public static String toGmtTimestampString(Timestamp timestamp)
    • monthBegin

      public static Timestamp monthBegin()
      Makes a Timestamp for the beginning of the month
      Returns:
      A Timestamp of the beginning of the month
    • weekNumber

      public static int weekNumber(Timestamp input)
      returns a week number in a year for a Timestamp input
      Parameters:
      input - Timestamp date
      Returns:
      A int containing the week number
    • dayNumber

      public static int dayNumber(Timestamp stamp)
      returns a day number in a week for a Timestamp input
      Parameters:
      stamp - Timestamp date
      Returns:
      A int containing the day number (sunday = 1, saturday = 7)
    • weekNumber

      public static int weekNumber(Timestamp input, int startOfWeek)
    • getCalendarInstance

      public static com.ibm.icu.util.Calendar getCalendarInstance(TimeZone timeZone, Locale locale)
    • toCalendar

      public static com.ibm.icu.util.Calendar toCalendar(Date date, TimeZone timeZone, Locale locale)
      Returns a Calendar object initialized to the specified date/time, time zone, and locale.
      Parameters:
      date - date/time to use
      timeZone -
      locale -
      Returns:
      Calendar object
      See Also:
    • adjustTimestamp

      public static Timestamp adjustTimestamp(Timestamp stamp, int adjType, int adjQuantity, TimeZone timeZone, Locale locale)
      Perform date/time arithmetic on a Timestamp. This is the only accurate way to perform date/time arithmetic across locales and time zones.
      Parameters:
      stamp - date/time to perform arithmetic on
      adjType - the adjustment type to perform. Use one of the java.util.Calendar fields.
      adjQuantity - the adjustment quantity.
      timeZone -
      locale -
      Returns:
      adjusted Timestamp
      See Also:
    • adjustTimestamp

      public static Timestamp adjustTimestamp(Timestamp stamp, Integer adjType, Integer adjQuantity)
    • getDayStart

      public static Timestamp getDayStart(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getDayStart

      public static Timestamp getDayStart(Timestamp stamp, int daysLater, TimeZone timeZone, Locale locale)
    • getDayEnd

      public static Timestamp getDayEnd(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getDayEnd

      public static Timestamp getDayEnd(Timestamp stamp, Long daysLater, TimeZone timeZone, Locale locale)
    • getWeekStart

      public static Timestamp getWeekStart(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getWeekStart

      public static Timestamp getWeekStart(Timestamp stamp, int daysLater, TimeZone timeZone, Locale locale)
    • getWeekStart

      public static Timestamp getWeekStart(Timestamp stamp, int daysLater, int weeksLater, TimeZone timeZone, Locale locale)
    • getWeekEnd

      public static Timestamp getWeekEnd(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getMonthStart

      public static Timestamp getMonthStart(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getMonthStart

      public static Timestamp getMonthStart(Timestamp stamp, int daysLater, TimeZone timeZone, Locale locale)
    • getMonthStart

      public static Timestamp getMonthStart(Timestamp stamp, int daysLater, int monthsLater, TimeZone timeZone, Locale locale)
    • getMonthEnd

      public static Timestamp getMonthEnd(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp, int daysLater, TimeZone timeZone, Locale locale)
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp, int daysLater, int yearsLater, TimeZone timeZone, Locale locale)
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp, Number daysLater, Number monthsLater, Number yearsLater, TimeZone timeZone, Locale locale)
    • getYearStart

      public static Timestamp getYearStart(Timestamp stamp, int daysLater, int monthsLater, int yearsLater, TimeZone timeZone, Locale locale)
    • getYearEnd

      public static Timestamp getYearEnd(Timestamp stamp, TimeZone timeZone, Locale locale)
    • weekNumber

      public static int weekNumber(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getDayNames

      public static List<String> getDayNames(Locale locale)
      Returns a List of day name Strings - suitable for calendar headings.
      Parameters:
      locale -
      Returns:
      List of day name Strings
    • getMonthNames

      public static List<String> getMonthNames(Locale locale)
      Returns a List of month name Strings - suitable for calendar headings.
      Parameters:
      locale -
      Returns:
      List of month name Strings
    • toDateFormat

      public static DateFormat toDateFormat(String dateFormat, TimeZone tz, Locale locale)
      Returns an initialized DateFormat object.
      Parameters:
      dateFormat - optional format string
      tz -
      locale - can be null if dateFormat is not null
      Returns:
      DateFormat object
    • toDateTimeFormat

      public static DateFormat toDateTimeFormat(String dateTimeFormat, TimeZone tz, Locale locale)
      Returns an initialized DateFormat object.
      Parameters:
      dateTimeFormat - optional format string
      tz -
      locale - can be null if dateTimeFormat is not null
      Returns:
      DateFormat object
    • toTimeFormat

      public static DateFormat toTimeFormat(String timeFormat, TimeZone tz, Locale locale)
      Returns an initialized DateFormat object.
      Parameters:
      timeFormat - optional format string
      tz -
      locale - can be null if timeFormat is not null
      Returns:
      DateFormat object
    • stringToTimeStamp

      public static Timestamp stringToTimeStamp(String dateTimeString, TimeZone tz, Locale locale) throws ParseException
      Localized String to Timestamp conversion. To be used in tandem with timeStampToString().
      Throws:
      ParseException
    • stringToTimeStamp

      public static Timestamp stringToTimeStamp(String dateTimeString, String dateTimeFormat, TimeZone tz, Locale locale) throws ParseException
      Localized String to Timestamp conversion. To be used in tandem with timeStampToString().
      Throws:
      ParseException
    • timeStampToString

      public static String timeStampToString(Timestamp stamp, TimeZone tz, Locale locale)
      Localized Timestamp to String conversion. To be used in tandem with stringToTimeStamp().
    • timeStampToString

      public static String timeStampToString(Timestamp stamp, String dateTimeFormat, TimeZone tz, Locale locale)
      Localized Timestamp to String conversion. To be used in tandem with stringToTimeStamp().
    • availableTimeZones

      public static List<TimeZone> availableTimeZones()
      Returns a List of available TimeZone objects.
      See Also:
    • toTimeZone

      public static TimeZone toTimeZone(String tzId)
      Returns a TimeZone object based upon a time zone ID. Method defaults to server's time zone if tzID is null or empty.
      See Also:
    • toTimeZone

      public static TimeZone toTimeZone(int gmtOffset)
      Returns a TimeZone object based upon an hour offset from GMT.
      See Also:
    • getSecond

      public static int getSecond(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getMinute

      public static int getMinute(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getHour

      public static int getHour(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getDayOfWeek

      public static int getDayOfWeek(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getDayOfMonth

      public static int getDayOfMonth(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getDayOfYear

      public static int getDayOfYear(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getWeek

      public static int getWeek(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getMonth

      public static int getMonth(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getYear

      public static int getYear(Timestamp stamp, TimeZone timeZone, Locale locale)
    • getEarliestDate

      public static Date getEarliestDate()
    • getLatestDate

      public static Date getLatestDate()
    • unmodifiableDate

      public static Date unmodifiableDate(Date date)
      Returns a copy of date that cannot be modified. Attempts to modify the returned date will result in an UnsupportedOperationException.
      Parameters:
      date - the date to copy
      Returns:
      an immutable copy of date.
    • getDateFormat

      public static String getDateFormat()
    • getDateTimeFormat

      public static String getDateTimeFormat()
    • getTimeFormat

      public static String getTimeFormat()