public class DateUtil extends Object
Modifier and Type | Field and Description |
---|---|
private static SimpleDateFormat |
DATE_FORMAT |
private static SimpleDateFormat |
TIMESTAMP_FORMAT |
Constructor and Description |
---|
DateUtil() |
Modifier and Type | Method and Description |
---|---|
static Date |
copy(Date value)
Get a copy of the date.
|
static String |
formatDate(Date d)
Formats a date in yyyy-MM-dd format.
|
static String |
formatTimestamp(Date d)
Formats a timestamp in yyyy-MM-dd HH:mm:ss format.
|
static Date |
parseString(String value)
Parses a string to create a
Date without time. |
static Date |
parseTimestamp(String value)
Parses a string to create a
Date 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 |
setNotNullDate(Date value,
String name)
Make a copy of a
Date object. |
static Date |
setNullableDate(Date value,
String name)
Make a copy of a
Date object. |
static Date |
truncate(Date value)
Truncate the given date so that only the date part
remains.
|
private static final SimpleDateFormat DATE_FORMAT
private static final SimpleDateFormat TIMESTAMP_FORMAT
public static Date setNullableDate(Date value, String name)
Date
object. Null values are allowed.value
- The date to checkname
- The name of the attribute to use if an exception is thrownpublic static Date setNotNullDate(Date value, String name) throws InvalidUseOfNullException
Date
object. Null values are not allowed.value
- The date to check. Can not be null.name
- The name of the attribute to use if an exception is thrownInvalidUseOfNullException
- If the value parameter is null.public static Date copy(Date value)
value
- Date value to copypublic static Date truncate(Date value)
value
- The date to truncate, can be nullpublic static Date parseString(String value) throws InvalidDataException
Date
without time. This method supports
date in yyyy-MM-dd format or as long timevalues.value
- the String
to be parsedDate
objectDateFormatException
- if value
isn't a valid date.InvalidDataException
public static String formatDate(Date d)
d
- The date to format.public static Date parseTimestamp(String value) throws InvalidDataException
Date
including time. This method
supports date in yyyy-MM-dd HH:mm:ss format or as long timevalues.value
- the String
to be parsedDate
objectDateFormatException
- if value
isn't a valid timestampInvalidDataException
public static String formatTimestamp(Date d)
d
- The date to format.public static boolean safeEquals(Date d1, Date d2, boolean time)
time
- TRUE to compare the date+time part, FALSE to only compare the
date partpublic static boolean safeInvariantEquals(List<Date> l1, List<Date> l2, boolean time)
time
- TRUE to compare the date+time part, FALSE to only compare the
date part