Hi all,
Hope this helps you to solve many issues on dates.do let me know your
esteemed comments and suggestions please.
==================================================================
package com.eds.fi.ebca.ngbs.util;
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
/**
* This class contains all basic functionalities to access dates in a
standard way.
* Months will start from 1 to 12.
* Standard format for dates is YYYY/MM/DD
* Standard format for times is HH:MM
*
*/
public class DateService
{
public final static SimpleDateFormat dateFormatter = new
SimpleDateFormat("yyyy-M-d");
public final static SimpleDateFormat timeFormatter = new
SimpleDateFormat("HH:mm");
public final static SimpleDateFormat dateTimeFormatter = new
SimpleDateFormat("yyyy-M-d HH:mm");
/**
* Adds a number of days to the given date (or subtract if days is negative)
*/
public static Date addDays(Date date, int days)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(cal.DATE, days);
return cal.getTime();
}
/**
* Adds a number of months to the given date (or subtract if months is
negative)
*/
public static Date addMonths(Date date, int months)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(cal.MONTH, months);
return cal.getTime();
}
/**
* Adds a number of years to the given date (or subtract if years is
negative)
*/
public static Date addYears(Date date, int years)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(cal.YEAR, years);
return cal.getTime();
}
public static Date convertFromSqlDate(java.sql.Date date)
{
return new Date(date.getTime());
}
public static java.sql.Date convertToSqlDate(Date date)
{
if (date == null)
return null;
return new java.sql.Date(date.getTime());
}
/**
* Gives a string representation of the given date using a custom format,
* that follows the standards of java.text.SimpleDateFormat
*
* @param date Date
* @param simpleFormat String
* @return String
* @exception InvalidDateException
*/
public static String format(Date date, String simpleFormat) throws
InvalidDateException
{
try
{
return new SimpleDateFormat(simpleFormat).format(date);
} catch (Exception e)
{
throw new InvalidDateException( (date==null)?
"null":date.toString(), "Invalid date" ,
"DateService", "parse()", e);
}
}
/**
* get current time
*/
public final static Date getCurrentTime()
{
return Calendar.getInstance().getTime();
}
/**
* Gets a java.util.Date representation of the given year, month and day
*
* @param yy int The year (if year is smaller then 100, we add 1900).
* @param mm int The month (value between 1 and 12)
* @param dd int The day
* @return java.util.Date
* @exception InvalidDateException
*/
public static Date getDate(int yy, int mm, int dd) throws
InvalidDateException
{
if (yy < 100)
yy += 1900;
try {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.DATE, dd);
cal.set(Calendar.MONTH, mm - 1);
cal.set(Calendar.YEAR, yy);
return cal.getTime();
}catch (Exception e){
StringBuffer msg = new StringBuffer(10);
msg.append(yy);
msg.append('/');
msg.append(mm);
msg.append('/');
msg.append(dd);
throw new InvalidDateException(msg.toString(), "Invalid
date" , "DateService", "getDate()", e);
}
}
/**
* Gets a java.util.Date representation of the given year, month and day.
* Parses the string values and then calls the <CODE>getDate(int, int,
int)</CODE> method.
*
* @param yy String The year (if year is smaller then 100, we add 1900).
* @param mm String The month (value between 1 and 12)
* @param dd String The day
* @return java.util.Date
* @exception InvalidDateException
*/
public static Date getDate(String date) throws InvalidDateException
{
try {
return dateFormatter.parse(date);
} catch (java.text.ParseException pe) {
throw new InvalidDateException(pe.getMessage(),pe);
}
}
/**
* Gets a java.util.Date representation of the given year, month and day.
* Parses the string values and then calls the <CODE>getDate(int, int,
int)</CODE> method.
*
* @param yy String The year (if year is smaller then 100, we add 1900).
* @param mm String The month (value between 1 and 12)
* @param dd String The day
* @return java.util.Date
* @exception InvalidDateException
*/
public static Date getDate(String yy, String mm, String dd) throws
InvalidDateException{
try {
int y = Integer.parseInt(yy);
int m = Integer.parseInt(mm);
int d = Integer.parseInt(dd);
return getDate(y,m,d);
}
catch (NumberFormatException e) {
StringBuffer msg = new StringBuffer(10);
msg.append(yy);
msg.append('/');
msg.append(mm);
msg.append('/');
msg.append(dd);
throw new InvalidDateException(msg.toString(), "Invalid
date" , "DateService", "getDate()", e);
}
}
/**
* Gets the day from a given date (1 - 31) using the Calendar class.
*
* @param date Date
* @return int
* @exception InvalidDateException
*/
public static int getDay(Date date) throws InvalidDateException{
try {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTime(date);
return cal.get(Calendar.DATE);
} catch (Exception e) {
throw new InvalidDateException( (date==null)?
"null":date.toString(), "Invalid date" , "DateService", "getDay()", e);
}
}
/**
* Gets the hour from a given date (0 - 23) using the Calendar class.
*
* @param date Date
* @return int
* @exception InvalidDateException
*/
public static int getHour(Date date) throws InvalidDateException{
try {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTime(date);
return cal.get(Calendar.HOUR_OF_DAY);
} catch (Exception e) {
throw new InvalidDateException( (date==null)?
"null":date.toString(), "Invalid date" , "DateService", "getDay()", e);
}
}
/**
* Gets the minutes from a given date (0 - 59) using the Calendar class.
*
* @param date Date
* @return int
* @exception InvalidDateException
*/
public static int getMinutes(Date date) throws InvalidDateException{
try {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTime(date);
return cal.get(Calendar.MINUTE);
} catch (Exception e) {
throw new InvalidDateException( (date==null)?
"null":date.toString(), "Invalid date" , "DateService", "getDay()", e);
}
}
/**
* Gets the month from a given date (1 - 12) using the Calendar class.
*
* @param date Date
* @return int
* @exception InvalidDateException
*/
public static int getMonth(Date date) throws InvalidDateException {
try {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
} catch (Exception e){
throw new InvalidDateException( (date==null)?
"null":date.toString(), "Invalid date" , "DateService", "getMonth()", e);
}
}
public final static java.util.Date getTime(int hours, int min, int sec)
throws InvalidDateException {
try {
Calendar cal = Calendar.getInstance();
java.util.Date today = cal.getTime();
int year = getYear(today);
int month = getMonth(today) - 1;
int date = getDay(today);
cal.clear();
cal.set(year, month, date, hours, min, sec);
return cal.getTime();
} catch (Exception e) {
if (e instanceof InvalidDateException)
throw (InvalidDateException) e;
else {
String input = "" + hours + ':' + min + ':'+ sec;
throw new InvalidDateException(
input,
"invalid time : " + input,
"DateService",
"getTime()",
e);
}
}
}
public final static java.util.Date getTime(
int year,
int month,
int day,
int hours,
int min,
int sec)
throws InvalidDateException {
try {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(year, month-1, day, hours, min, sec);
return cal.getTime();
} catch (Exception e) {
if (e instanceof InvalidDateException)
throw (InvalidDateException) e;
else {
String input = "" + hours + ':' + min + ':' + sec;
throw new InvalidDateException(
input,
"invalid time : " + input,
"DateService",
"getTime()",
e);
}
}
}
public static String getXML(Date date) throws InvalidDateException{
return getXML(date, "Year", "Month", "Day");
}
public static String getXML(Date date, String yearTag, String monthTag,
String dayTag) throws InvalidDateException{
try {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTime(date);
StringBuffer buffer = new StringBuffer(41);
buffer.append("<");
buffer.append(yearTag);
buffer.append(">");
buffer.append(cal.get(cal.YEAR));
buffer.append("</");
buffer.append(yearTag);
buffer.append(">");
buffer.append("<");
buffer.append(monthTag);
buffer.append(">");
buffer.append(cal.get(cal.MONTH) + 1);
buffer.append("</");
buffer.append(monthTag);
buffer.append(">");
buffer.append("<");
buffer.append(dayTag);
buffer.append(">");
buffer.append(cal.get(cal.DATE));
buffer.append("</");
buffer.append(dayTag);
buffer.append(">");
return buffer.toString();
} catch (Exception e) {
throw new InvalidDateException( (date==null)?
"null":date.toString(), "Invalid date" , "DateService", "getXML()", e);
}
}
/**
* Gets the year from a given date (1900 - 9999) using the Calendar class.
*
* @param date Date
* @return int
* @exception InvalidDateException
*/
public static int getYear(Date date) throws InvalidDateException{
try {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTime(date);
int yy = cal.get(Calendar.YEAR);
if (yy < 100)
yy += 1900;
return yy;
} catch (Exception e) {
throw new InvalidDateException( (date==null)?
"null":date.toString(), "Invalid date" , "DateService", "getYear()", e);
}
}
/**
* Checks whether 2 dates are on the same day
* @return boolean
* @param date1 java.util.Date
* @param date2 java.util.Date
*/
public static boolean isSameDay(Date date1, Date date2) {
if ( date1 == null && date2 == null ){
return true;
}
if ( date1 == null || date2 == null ){
return false;
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
return (cal1.get(Calendar.DAY_OF_MONTH) ==
cal2.get(Calendar.DAY_OF_MONTH)
&& cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
&& cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR));
}
/**
* Gives a date using a custom format,
* that follows the standards of java.text.SimpleDateFormat
* Avoid using this method. Try to use the standard format throughout
the whole application.
*
* @param date String
* @param simpleFormat String
* @return Date
* @exception InvalidDateException
*/
public static Date parse( String date, String simpleFormat ) throws
InvalidDateException
{
try
{
return new SimpleDateFormat(simpleFormat).parse(date);
}
catch ( Exception e )
{
throw new InvalidDateException( ( date == null ) ? "null" :
date.toString(), "Invalid date", "DateService", "parse()", e );
}
}
/**
* Gives a Date object from the standard representation generated by
toStandardString(Date).
*
* @param date Date
* @return String
* @exception InvalidDateException
*/
public static Date toStandardDate(String date) throws InvalidDateException{
try{
return dateFormatter.parse(date);
} catch (Exception e){
throw new InvalidDateException( (date==null)? "null" : date,
"Invalid date" , "DateService", "toStandardDate()", e);
}
}
/**
* Gives a Date object from the standard representation generated by
toStandardString(Date).
*
* @param date Date
* @return String
* @exception InvalidDateException
*/
public static Date toStandardDateTime(String date) throws
InvalidDateException{
try{
return dateTimeFormatter.parse(date);
} catch (Exception e){
throw new InvalidDateException( (date==null)? "null" : date,
"Invalid date" , "DateService", "toStandardDate()", e);
}
}
/**
* Gives a standard string representation of the given date.
*
* @param date Date
* @return String
* @exception InvalidDateException
*/
public static String toStandardString(Date date) throws
InvalidDateException{
try{
return dateFormatter.format(date);
} catch (Exception e){
throw new InvalidDateException( (date==null)?
"null":date.toString(), "Invalid date" , "DateService", "toStandardDate()",
e);
}
}
/**
* Gives a standard string representation of the given date.
*
* @param date Date
* @return String
* @exception InvalidDateException
*/
public static String toStandardTimeString(Date date) throws
InvalidDateException{
try{
return dateTimeFormatter.format(date);
} catch (Exception e){
throw new InvalidDateException( (date==null)?
"null":date.toString(), "Invalid date" , "DateService", "toStandardDate()",
e);
}
}
}
==================================================================
Regards
Ramesh Kesavanarayanan
[EMAIL PROTECTED]
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".