BTW, here is a helper class I wrote a while ago. I never came back to it, and it is not fully tested. But it may point you in a direction.
- Saish
public class DateHelper extends Object {
/////////////////////
// Class Variables //
/////////////////////
final static public String SHORT_DATE = "MM/dd"; // 5 Done
final static public String LONG_DATE = "MM/dd/yy"; // 8
final static public String SHORT_TIME = "hh:mm aa"; // 8
final static public String LONG_TIME = "hh:mm:ss aa"; // 11
final static public String SHORT_TIME_MILITARY = "HH:mm"; // 5 Done
final static public String LONG_TIME_MILITARY = "HH:mm:ss"; // 8
final static public String SHORT_DATE_TIME = SHORT_DATE + " " + SHORT_TIME;
final static public String SHORT_DATE_TIME_MILITARY = SHORT_DATE + " " + SHORT_TIME_MILITARY;
final static public String LONG_DATE_TIME = LONG_DATE + " " + LONG_TIME;
final static public String LONG_DATE_TIME_MILITARY = LONG_DATE + " " + LONG_TIME_MILITARY;
final static public String DATE_DELIMITER = "/";
final static public String TIME_DELIMITER = ":";
final static public String DATE_TIME_DELIMITER = " ";
final static protected String MIDNIGHT = "0:00";
static private Date timezoneOffset;
//////////////////
// Constructors //
//////////////////
public DateHelper() {
super();
}
////////////////////
// Public Methods //
////////////////////
static public Date parse(String text)
throws ParseException {
String target = text.trim();
boolean isDate = target.indexOf(DATE_DELIMITER) >= 0;
boolean isTime = target.indexOf(TIME_DELIMITER) >= 0;
// Process based on the length
StringTokenizer tokenizer = new StringTokenizer(target, DATE_TIME_DELIMITER);
String format = "";
if (isDate) {
format = getDateFormat(tokenizer.nextToken());
if (tokenizer.hasMoreTokens())
format = format + DATE_TIME_DELIMITER;
}
if (isTime) {
String timeString = tokenizer.nextToken();
if (tokenizer.hasMoreTokens())
format = format + getTimeFormat(timeString + " " + tokenizer.nextToken());
else
format = format + getMilitaryTimeFormat(timeString);
}
// Parse the date with the applicable format
DateFormat formatter = new SimpleDateFormat(format);
try {
return formatter.parse(text);
}
catch (java.text.ParseException e) {
throw new ParseException ("Unable to parse '" + target + "'", e);
}
}
static public Date parse(String text, String format)
throws ParseException {
DateFormat formatter = new SimpleDateFormat(format);
try {
return formatter.parse(text.trim());
}
catch (java.text.ParseException e) {
throw new ParseException ("Unable to parse '" + text + "'", e);
}
}
static public String format(Date date, String format) {
DateFormat formatter = new SimpleDateFormat(format);
return formatter.format(date);
}
static public double getDaysBetween(Date first, Date second) {
double milliElapsed = second.getTime() - first.getTime();
double daysElapsed = (milliElapsed / 24F / 3600F / 1000F);
return (Math.round(daysElapsed * 100F) / 100F);
}
static public double getHoursBetween(Date first, Date second) {
double milliElapsed = second.getTime() - first.getTime();
double hoursElapsed = (milliElapsed / 3600F / 1000F);
return (Math.round(hoursElapsed * 100F) / 100F);
}
static final public Date add(Date first, Date second)
throws ParseException {
if (timezoneOffset == null)
timezoneOffset = parse(MIDNIGHT);
return new Date(first.getTime() + second.getTime() - timezoneOffset.getTime());
}
static final public Date add(String first, String second)
throws ParseException {
return add(parse(first), parse(second));
}
static final public Date add(Date first, double days, double hours, double minutes) {
double additionalTime = (days * 24F * 3600F * 1000F ) + (hours * 3600F * 1000F) +
(minutes * 60 * 1000);
return new Date(Math.round((double) first.getTime() + additionalTime));
}
static public double getCombinedDays(Date first, Date second) {
double milliCombined = first.getTime() + second.getTime();
double daysCombined = (milliCombined / 24F / 3600F / 1000F);
return (Math.round(daysCombined * 100F) / 100F);
}
static public double getCombinedHours(Date first, Date second) {
double milliCombined = first.getTime() + second.getTime();
double hoursCombined = (milliCombined / 3600F / 1000F);
return (Math.round(hoursCombined * 100F) / 100F);
}
static public Date getNextDay(Date target) {
Date next = new Date(target.getTime() + (24 * 3600 * 1000));
Calendar calendar = new GregorianCalendar();
calendar.setTime(next);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
static public boolean isSameDay(Date first, Date second) {
Calendar calendar1 = new GregorianCalendar();
calendar1.setTime(first);
Calendar calendar2 = new GregorianCalendar();
calendar2.setTime(second);
return ( (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) &&
(calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) &&
(calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)) );
}
// This algorithm can undoubtedly be made MUCH better
static public boolean isConsecutiveDay(Date first, Date second) {
return (Math.abs(getDaysBetween(stripTime(first), stripTime(second))) <= 1.0F);
}
static public Date stripTime(Date dateTime) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(dateTime);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/////////////////////
// Private Methods //
/////////////////////
static private String getDateFormat(String dateString)
throws ParseException {
int length = dateString.length();
if (length == SHORT_DATE.length())
return SHORT_DATE;
else if (length == LONG_DATE.length())
return LONG_DATE;
else
throw new ParseException("Invalid date format '" + dateString + "'");
}
static private String getTimeFormat(String timeString)
throws ParseException {
int length = timeString.length();
if ((length == SHORT_TIME.length()) || (length == SHORT_TIME.length() - 1))
return SHORT_TIME;
else if ((length == LONG_TIME.length()) || (length == LONG_TIME.length() - 1))
return LONG_TIME;
throw new ParseException("Invalid time format '" + timeString + "'");
}
static private String getMilitaryTimeFormat(String timeString)
throws ParseException {
int length = timeString.length();
if ((length == SHORT_TIME_MILITARY.length()) || (length == SHORT_TIME_MILITARY.length() - 1))
return SHORT_TIME_MILITARY;
else if ((length == LONG_TIME_MILITARY.length()) || (length == LONG_TIME_MILITARY.length() - 1))
return LONG_TIME_MILITARY;
throw new ParseException("Invalid military time format '" + timeString + "'");
}
}