Hi guys,
Hope this helps to solve many problems using dates. Expect your esteemed
suggestions and comments for my improvement.
i could not send more than 100 lines. so i am sending the file as 2 parts.
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
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");
public static Date addDays(Date date, int days){
Calendar cal = Calendar.getInstance();cal.setTime(date);cal.add(cal.DATE,
days);return cal.getTime();
}
public static Date addMonths(Date date, int months){
Calendar cal = Calendar.getInstance();cal.setTime(date);
cal.add(cal.MONTH, months);
return cal.getTime();
}
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());
}
public static String format(Date date, String simpleFormat) throws
InvalidDateException{
try{
return new SimpleDateFormat(simpleFormat).format(date);
} catch (Exception e){ }
}
public final static Date getCurrentTime() {
return Calendar.getInstance().getTime();
}
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)
;
}
}
public static Date getDate(String date) throws InvalidDateException {
try {
return dateFormatter.parse(date);
} catch (java.text.ParseException pe) {
throw new InvalidDateException(pe.getMessage(),pe);
}
}
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)
;
}
}
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) {}
}
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) { }
}
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) { }
}
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){}
}
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".