DateUtil.java


package com.bush.util;

import java.util.Date;
import java.text.*;

/**
* Useful Date utility methods not found in java.lang.Boolean
*
* @author Daniel Bush
*/
public class DateUtil {

private DateUtil() {}

/**
* Convert the specified {@link java.util.Date} into a {@link java.sql.Date}.
*
* @param date a Date value
* @return a java.sql.Date value
*/
public static java.sql.Date convert(Date date) {
return date != null ? new java.sql.Date(date.getTime()) : null;
}

/**
* Convert the specified {@link java.sql.Date} into a {@link java.util.Date}.
*
* @param timestamp a java.sql.Date value
* @return a Date value
*/
public static Date convert(java.sql.Date timestamp) {
return timestamp != null ? new Date(timestamp.getTime()) : null;
}

/**
* Convert the specified {@link java.util.Date} into a {@link java.sql.Timestamp}.
*
* @param date a Date value
* @return a java.sql.Timestamp value
*/
public static java.sql.Timestamp convertToTimestamp(Date date) {
return date != null ? new java.sql.Timestamp(date.getTime()) : null;
}

/**
* Convert the specified timestamp into a date.
*
* @param timestamp a java.sql.Timestamp value
* @return a Date value
*/
public static Date convertToDate(java.sql.Timestamp timestamp) {
return timestamp != null ? new Date(timestamp.getTime()) : null;
}

/**
* Give me a String and a pattern and I will try to give you the Date representation.
*
* @param date a String value
* @param pattern a String value
* @return a Date value
* @exception ParseException if an error occurs
*/
public static Date parse(String date, String pattern) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(date);
}

/**
* Give me a Date and a pattern and I will try to give you the String representation.
*
* @param date a Date value
* @param pattern a String value
* @return a String value
* @exception ParseException if an error occurs
*/
public static String format(Date date, String pattern) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}

}

this

Archives

November 2005  

This page is powered by Blogger. Isn't yours?