Package org.apache.ofbiz.base.util
Class UtilDateTime
java.lang.Object
org.apache.ofbiz.base.util.UtilDateTime
Utility class for handling java.util.Date, the java.sql data/time classes and related
-
Method Summary
Modifier and TypeMethodDescriptionstatic Timestamp
addDaysToTimestamp
(Timestamp start, int days) static Timestamp
addDaysToTimestamp
(Timestamp start, Double days) static Timestamp
adjustTimestamp
(Timestamp stamp, int adjType, int adjQuantity, TimeZone timeZone, Locale locale) Perform date/time arithmetic on a Timestamp.static Timestamp
adjustTimestamp
(Timestamp stamp, Integer adjType, Integer adjQuantity) Returns a List of available TimeZone objects.static int
returns a day number in a week for a Timestamp inputstatic String
formatInterval
(double interval, int count, Locale locale) static String
formatInterval
(double interval, Locale locale) static String
formatInterval
(long interval, int count, Locale locale) static String
formatInterval
(long interval, Locale locale) static String
formatInterval
(Timestamp from, Timestamp thru, int count, Locale locale) static String
formatInterval
(Timestamp from, Timestamp thru, Locale locale) static String
formatInterval
(Date from, Date thru, int count, Locale locale) static String
formatInterval
(Date from, Date thru, Locale locale) static com.ibm.icu.util.Calendar
getCalendarInstance
(TimeZone timeZone, Locale locale) static String
static String
static Timestamp
static Timestamp
static Timestamp
static Timestamp
getDayNames
(Locale locale) Returns a List of day name Strings - suitable for calendar headings.static int
getDayOfMonth
(Timestamp stamp, TimeZone timeZone, Locale locale) static int
getDayOfWeek
(Timestamp stamp, TimeZone timeZone, Locale locale) static int
getDayOfYear
(Timestamp stamp, TimeZone timeZone, Locale locale) static Timestamp
getDayStart
(Timestamp stamp) static Timestamp
getDayStart
(Timestamp stamp, int daysLater) static Timestamp
getDayStart
(Timestamp stamp, int daysLater, TimeZone timeZone, Locale locale) static Timestamp
getDayStart
(Timestamp stamp, TimeZone timeZone, Locale locale) static int
static double
getInterval
(Timestamp from, Timestamp thru) static int
getIntervalInDays
(Timestamp from, Timestamp thru) static int
static int
static Timestamp
getMonthEnd
(Timestamp stamp, TimeZone timeZone, Locale locale) getMonthNames
(Locale locale) Returns a List of month name Strings - suitable for calendar headings.static Timestamp
getMonthStart
(Timestamp stamp) Return the date for the first day of the monthstatic Timestamp
getMonthStart
(Timestamp stamp, int daysLater) static Timestamp
getMonthStart
(Timestamp stamp, int daysLater, int monthsLater) static Timestamp
getMonthStart
(Timestamp stamp, int daysLater, int monthsLater, TimeZone timeZone, Locale locale) static Timestamp
getMonthStart
(Timestamp stamp, int daysLater, TimeZone timeZone, Locale locale) static Timestamp
getMonthStart
(Timestamp stamp, TimeZone timeZone, Locale locale) static Timestamp
getNextDayStart
(Timestamp stamp) static int
static String
static Timestamp
getTimestamp
(long time) Convert a millisecond value to a Timestamp.static Timestamp
getTimestamp
(String milliSecs) Convert a millisecond value to a Timestamp.static int
static Timestamp
getWeekEnd
(Timestamp stamp) static Timestamp
getWeekEnd
(Timestamp stamp, TimeZone timeZone, Locale locale) static Timestamp
getWeekStart
(Timestamp stamp) Return the date for the first day of the weekstatic Timestamp
getWeekStart
(Timestamp stamp, int daysLater) static Timestamp
getWeekStart
(Timestamp stamp, int daysLater, int weeksLater) static Timestamp
getWeekStart
(Timestamp stamp, int daysLater, int weeksLater, TimeZone timeZone, Locale locale) static Timestamp
getWeekStart
(Timestamp stamp, int daysLater, TimeZone timeZone, Locale locale) static Timestamp
getWeekStart
(Timestamp stamp, TimeZone timeZone, Locale locale) static int
static Timestamp
getYearEnd
(Timestamp stamp, TimeZone timeZone, Locale locale) static Timestamp
getYearStart
(Timestamp stamp) Return the date for the first day of the yearstatic Timestamp
getYearStart
(Timestamp stamp, int daysLater) static Timestamp
getYearStart
(Timestamp stamp, int daysLater, int yearsLater) static Timestamp
getYearStart
(Timestamp stamp, int daysLater, int monthsLater, int yearsLater) static Timestamp
getYearStart
(Timestamp stamp, int daysLater, int monthsLater, int yearsLater, TimeZone timeZone, Locale locale) static Timestamp
getYearStart
(Timestamp stamp, int daysLater, int yearsLater, TimeZone timeZone, Locale locale) static Timestamp
getYearStart
(Timestamp stamp, int daysLater, TimeZone timeZone, Locale locale) static Timestamp
getYearStart
(Timestamp stamp, Number daysLater, Number monthsLater, Number yearsLater) static Timestamp
getYearStart
(Timestamp stamp, Number daysLater, Number monthsLater, Number yearsLater, TimeZone timeZone, Locale locale) static Timestamp
getYearStart
(Timestamp stamp, TimeZone timeZone, Locale locale) static Timestamp
Makes a Timestamp for the beginning of the monthstatic String
Returns currentTimeMillis as Stringstatic Date
nowDate()
Return a Date for right nowstatic String
Return a string formatted as yyyyMMddHHmmssstatic String
nowDateString
(String format) Return a string formatted as formatstatic Timestamp
Return a Timestamp for right nowstatic Timestamp
stringToTimeStamp
(String dateTimeString, String dateTimeFormat, TimeZone tz, Locale locale) Localized String to Timestamp conversion.static Timestamp
stringToTimeStamp
(String dateTimeString, TimeZone tz, Locale locale) Localized String to Timestamp conversion.static String
timeStampToString
(Timestamp stamp, String dateTimeFormat, TimeZone tz, Locale locale) Localized Timestamp to String conversion.static String
timeStampToString
(Timestamp stamp, TimeZone tz, Locale locale) Localized Timestamp to String conversion.static com.ibm.icu.util.Calendar
toCalendar
(Timestamp stamp) 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.static Date
Converts a date and time String into a Datestatic DateFormat
toDateFormat
(String dateFormat, TimeZone tz, Locale locale) Returns an initialized DateFormat object.static String
toDateString
(Date date) Makes a date String in the format MM/DD/YYYY from a Datestatic DateFormat
toDateTimeFormat
(String dateTimeFormat, TimeZone tz, Locale locale) Returns an initialized DateFormat object.static String
toDateTimeString
(Date date) Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS" from a Date.static String
toGmtTimestampString
(Timestamp timestamp) static Date
toSqlDate
(int month, int day, int year) Makes a java.sql.Date from separate ints for month, day, yearstatic Date
Converts a date String into a java.sql.Datestatic Date
Makes a java.sql.Date from separate Strings for month, day, yearstatic Time
toSqlTime
(int hour, int minute, int second) Makes a java.sql.Time from separate ints for hour, minute, and second.static Time
Converts a time String into a java.sql.Timestatic Time
Makes a java.sql.Time from separate Strings for hour, minute, and second.static DateFormat
toTimeFormat
(String timeFormat, TimeZone tz, Locale locale) Returns an initialized DateFormat object.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.static Timestamp
toTimestamp
(String dateTime) Converts a date and time String into a Timestampstatic Timestamp
toTimestamp
(String date, String time) Converts a date String and a time String into a Timestampstatic 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.static Timestamp
toTimestamp
(Date date) 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.static String
toTimeString
(Date date) Makes a time String in the format HH:MM:SS from a Date.static TimeZone
toTimeZone
(int gmtOffset) Returns a TimeZone object based upon an hour offset from GMT.static TimeZone
toTimeZone
(String tzId) Returns a TimeZone object based upon a time zone ID.static int
weekNumber
(Timestamp input) returns a week number in a year for a Timestamp inputstatic int
weekNumber
(Timestamp input, int startOfWeek) static int
weekNumber
(Timestamp stamp, TimeZone timeZone, Locale locale)
-
Method Details
-
getIntervalInDays
-
addDaysToTimestamp
-
addDaysToTimestamp
-
getInterval
-
formatInterval
-
formatInterval
-
formatInterval
-
formatInterval
-
formatInterval
-
formatInterval
-
formatInterval
-
formatInterval
-
nowTimestamp
Return a Timestamp for right now- Returns:
- Timestamp for right now
-
getTimestamp
Convert a millisecond value to a Timestamp.- Parameters:
time
- millsecond value- Returns:
- Timestamp
-
getTimestamp
Convert a millisecond value to a Timestamp.- Parameters:
milliSecs
- millsecond value- Returns:
- Timestamp
- Throws:
NumberFormatException
-
nowAsString
Returns currentTimeMillis as String- Returns:
- String(currentTimeMillis)
-
nowDateString
Return a string formatted as yyyyMMddHHmmss- Returns:
- String formatted for right now
-
nowDateString
Return a string formatted as format- Returns:
- String formatted for right now
-
nowDate
Return a Date for right now- Returns:
- Date for right now
-
getDayStart
-
getDayStart
-
getNextDayStart
-
getDayEnd
-
getDayEnd
-
getYearStart
Return the date for the first day of the year- Parameters:
stamp
-- Returns:
- java.sql.Timestamp
-
getYearStart
-
getYearStart
-
getYearStart
-
getYearStart
-
getMonthStart
Return the date for the first day of the month- Parameters:
stamp
-- Returns:
- java.sql.Timestamp
-
getMonthStart
-
getMonthStart
-
getWeekStart
Return the date for the first day of the week- Parameters:
stamp
-- Returns:
- java.sql.Timestamp
-
getWeekStart
-
getWeekStart
-
getWeekEnd
-
toCalendar
-
toSqlDate
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
Makes a java.sql.Date from separate Strings for month, day, year- Parameters:
monthStr
- The month StringdayStr
- The day StringyearStr
- The year String- Returns:
- A java.sql.Date made from separate Strings for month, day, year
-
toSqlDate
Makes a java.sql.Date from separate ints for month, day, year- Parameters:
month
- The month intday
- The day intyear
- The year int- Returns:
- A java.sql.Date made from separate ints for month, day, year
-
toSqlTime
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
Makes a java.sql.Time from separate Strings for hour, minute, and second.- Parameters:
hourStr
- The hour StringminuteStr
- The minute StringsecondStr
- The second String- Returns:
- A java.sql.Time made from separate Strings for hour, minute, and second.
-
toSqlTime
Makes a java.sql.Time from separate ints for hour, minute, and second.- Parameters:
hour
- The hour intminute
- The minute intsecond
- The second int- Returns:
- A java.sql.Time made from separate ints for hour, minute, and second.
-
toTimestamp
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
Converts a date String and a time String into a Timestamp- Parameters:
date
- The date String: MM/DD/YYYYtime
- 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 StringdayStr
- The day StringyearStr
- The year StringhourStr
- The hour StringminuteStr
- The minute StringsecondStr
- The second String- Returns:
- A Timestamp made from separate Strings for month, day, year, hour, minute, and second.
-
toTimestamp
Makes a Timestamp from separate ints for month, day, year, hour, minute, and second.- Parameters:
month
- The month intday
- The day intyear
- The year inthour
- The hour intminute
- The minute intsecond
- The second int- Returns:
- A Timestamp made from separate ints for month, day, year, hour, minute, and second.
-
toTimestamp
-
toDate
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
-
toDateString
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
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
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 intminute
- The minute intsecond
- The second int- Returns:
- A time String in the format HH:MM:SS or HH:MM
-
toDateTimeString
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
-
monthBegin
Makes a Timestamp for the beginning of the month- Returns:
- A Timestamp of the beginning of the month
-
weekNumber
returns a week number in a year for a Timestamp input- Parameters:
input
- Timestamp date- Returns:
- A int containing the week number
-
dayNumber
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
-
getCalendarInstance
-
toCalendar
Returns a Calendar object initialized to the specified date/time, time zone, and locale.- Parameters:
date
- date/time to usetimeZone
-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 onadjType
- 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
-
getDayStart
-
getDayStart
-
getDayEnd
-
getDayEnd
-
getWeekStart
-
getWeekStart
-
getWeekStart
-
getWeekEnd
-
getMonthStart
-
getMonthStart
-
getMonthStart
-
getMonthEnd
-
getYearStart
-
getYearStart
-
getYearStart
-
getYearStart
-
getYearStart
-
getYearEnd
-
weekNumber
-
getDayNames
Returns a List of day name Strings - suitable for calendar headings.- Parameters:
locale
-- Returns:
- List of day name Strings
-
getMonthNames
Returns a List of month name Strings - suitable for calendar headings.- Parameters:
locale
-- Returns:
- List of month name Strings
-
toDateFormat
Returns an initialized DateFormat object.- Parameters:
dateFormat
- optional format stringtz
-locale
- can be null if dateFormat is not null- Returns:
- DateFormat object
-
toDateTimeFormat
Returns an initialized DateFormat object.- Parameters:
dateTimeFormat
- optional format stringtz
-locale
- can be null if dateTimeFormat is not null- Returns:
- DateFormat object
-
toTimeFormat
Returns an initialized DateFormat object.- Parameters:
timeFormat
- optional format stringtz
-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
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
Returns a List of available TimeZone objects.- See Also:
-
toTimeZone
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
Returns a TimeZone object based upon an hour offset from GMT.- See Also:
-
getSecond
-
getMinute
-
getHour
-
getDayOfWeek
-
getDayOfMonth
-
getDayOfYear
-
getWeek
-
getMonth
-
getYear
-
getDateFormat
-
getDateTimeFormat
-
getTimeFormat
-