Package net.sf.basedb.core
Class DateUtil
java.lang.Object
net.sf.basedb.core.DateUtil
Utility methods that will make it easier to handle date values.
Especially, dates must be cloned whenver they are returned to
or passed from client applications.
- Version:
- 2.0
- Author:
- Nicklas
-
Field Summary
Modifier and TypeFieldDescriptionprivate static final org.apache.commons.lang3.time.FastDateFormat
private static final org.apache.commons.lang3.time.FastDateFormat
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Date
Get a copy of the date.static String
formatDate
(Date d) Formats a date in yyyy-MM-dd format.static String
Formats a timestamp in yyyy-MM-dd HH:mm:ss format.static Date
parseString
(String value) Parses a string to create aDate
without time.static Date
parseTimestamp
(String value) Parses a string to create aDate
including time.static boolean
safeEquals
(Date d1, Date d2, boolean time) Compare two date instances for equality while protecting from strange implementations in the java.util.Date and java.sql.Timestamp classes.static boolean
safeInvariantEquals
(List<Date> l1, List<Date> l2, boolean time) Checks if two lists contains equal elements disregarding their positions in the array.static Date
setDateOrToday
(Date value) Make a copy of the date.static Date
setNotNullDate
(Date value, String name) Make a copy of aDate
object.static Date
setNullableDate
(Date value, String name) Make a copy of aDate
object.static Date
Truncate the given date so that only the date part remains.
-
Field Details
-
DATE_FORMAT
private static final org.apache.commons.lang3.time.FastDateFormat DATE_FORMAT -
TIMESTAMP_FORMAT
private static final org.apache.commons.lang3.time.FastDateFormat TIMESTAMP_FORMAT
-
-
Constructor Details
-
DateUtil
public DateUtil()
-
-
Method Details
-
setNullableDate
Make a copy of aDate
object. Null values are allowed.- Parameters:
value
- The date to checkname
- The name of the attribute to use if an exception is thrown- Returns:
- A copy of the original date
-
setNotNullDate
Make a copy of aDate
object. Null values are not allowed.- Parameters:
value
- The date to check. Can not be null.name
- The name of the attribute to use if an exception is thrown- Returns:
- A copy of the original date
- Throws:
InvalidUseOfNullException
- If the value parameter is null.
-
setDateOrToday
Make a copy of the date. If null, today's date is used instead.- Since:
- 3.11
-
copy
Get a copy of the date. Null values are allowed.- Parameters:
value
- Date value to copy- Returns:
- A cloned Date object or null if argument is null.
-
truncate
Truncate the given date so that only the date part remains. The time part is set to 00:00:00.0- Parameters:
value
- The date to truncate, can be null- Returns:
- A new date with time part set to 0 or null
- Since:
- 2.16
-
parseString
Parses a string to create aDate
without time. This method supports date in yyyy-MM-dd format or as long timevalues.- Parameters:
value
- theString
to be parsed- Returns:
- a
Date
object - Throws:
DateFormatException
- ifvalue
isn't a valid date.InvalidDataException
-
formatDate
Formats a date in yyyy-MM-dd format.- Parameters:
d
- The date to format.- Returns:
- Formated date as a String.
- Since:
- 2.2
-
parseTimestamp
Parses a string to create aDate
including time. This method supports date in yyyy-MM-dd HH:mm:ss format or as long timevalues.- Parameters:
value
- theString
to be parsed- Returns:
- a
Date
object - Throws:
DateFormatException
- ifvalue
isn't a valid timestampInvalidDataException
- Since:
- 2.16
-
formatTimestamp
Formats a timestamp in yyyy-MM-dd HH:mm:ss format.- Parameters:
d
- The date to format.- Returns:
- Formated date as a String
- Since:
- 2.16
-
safeEquals
Compare two date instances for equality while protecting from strange implementations in the java.util.Date and java.sql.Timestamp classes.- Parameters:
time
- TRUE to compare the date+time part, FALSE to only compare the date part- Since:
- 3.9.1
-
safeInvariantEquals
Checks if two lists contains equal elements disregarding their positions in the array. Eg. [1, 2] is equal to [2, 1]- Parameters:
time
- TRUE to compare the date+time part, FALSE to only compare the date part- Returns:
- TRUE if both list contains the same elements
- Since:
- 3.9.1
-