2.17.2: 2011-06-17

net.sf.basedb.core
Class DateUtil

java.lang.Object
  extended by net.sf.basedb.core.DateUtil

public class DateUtil
extends Object

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
private static SimpleDateFormat DATE_FORMAT
           
private static SimpleDateFormat TIMESTAMP_FORMAT
           
 
Constructor Summary
DateUtil()
           
 
Method Summary
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 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATE_FORMAT

private static final SimpleDateFormat DATE_FORMAT

TIMESTAMP_FORMAT

private static final SimpleDateFormat TIMESTAMP_FORMAT
Constructor Detail

DateUtil

public DateUtil()
Method Detail

setNullableDate

public static Date setNullableDate(Date value,
                                   String name)
Make a copy of a Date object. Null values are allowed.

Parameters:
value - The date to check
name - The name of the attribute to use if an exception is thrown
Returns:
A copy of the original date

setNotNullDate

public static Date setNotNullDate(Date value,
                                  String name)
                           throws InvalidUseOfNullException
Make a copy of a Date 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.

copy

public static Date copy(Date value)
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

public static Date truncate(Date value)
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

public static Date parseString(String value)
                        throws InvalidDataException
Parses a string to create a Date without time. This method supports date in yyyy-MM-dd format or as long timevalues.

Parameters:
value - the String to be parsed
Returns:
a Date object
Throws:
DateFormatException - if value isn't a valid date.
InvalidDataException

formatDate

public static String formatDate(Date d)
Formats a date in yyyy-MM-dd format.

Parameters:
d - The date to format.
Returns:
Formated date as a String.
Since:
2.2

parseTimestamp

public static Date parseTimestamp(String value)
                           throws InvalidDataException
Parses a string to create a Date including time. This method supports date in yyyy-MM-dd HH:mm:ss format or as long timevalues.

Parameters:
value - the String to be parsed
Returns:
a Date object
Throws:
DateFormatException - if value isn't a valid timestamp
InvalidDataException
Since:
2.16

formatTimestamp

public static String formatTimestamp(Date d)
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

2.17.2: 2011-06-17