/*
"now" doesn't need to be calendarDate-aware,
since this is just a temp to find out
what year, month, and day today is.
*/
Date now = new Date();
int thisYear = Date.getYear(now, timeZone, locale);
int thisMonth = Date.getMonth(now, timeZone, locale);
int thisDayOfMonth = Date.getDayOfMonth(now, timeZone, locale);
int thisDayOfWeek = Date.getDayOfWeek(now, timeZone, locale);
Integer daysToAdd = null;
// if the user entered a day name, offset into the future to the
// appropriate day of the month, month, and year
int dayOfWeek = findDay(string);
if (dayOfWeek >= 0) {
int add = ((7 + dayOfWeek) - thisDayOfWeek) % 7;
if (add == 0) {
add = 7;
}
daysToAdd = Constants.getInteger(add);
}
// otherwise, check if the string is a prefix for strings
// like "today", "tomorrow", etc.
else {
for (int i = -1, len = string.length(); i <= 1; i++) {
String key = RelativeDayKeys[i+1];
String test = lookupLocalizedFormat(key, locale);
if (!StringUtil.nullOrEmptyString(test)) {
if (test.length() >= len) {
String sub = test.substring(0, len);
if (sub.equalsIgnoreCase(string)) {
daysToAdd = Constants.getInteger(i);
break;
}
}
}
}
}
Date result = null;
if (daysToAdd != null) {
result = new Date(thisYear, thisMonth, thisDayOfMonth,
calendarDate, timeZone, locale);
Date.addDays(result, daysToAdd.intValue(), timeZone, locale);
return result;
}
else {
// lastly, try TodayTimeFormatKey. Get the number of milisecs
// that the time represent with respect to GMT. Then add on to
// today's.
result = parseDateUsingFormats(string, locale,
TodayTimeFormatKey, TimeZone.getTimeZone("GMT"), calendarDate);
if (result != null) {
Date today = new Date(thisYear, thisMonth, thisDayOfMonth,
calendarDate, timeZone, locale);
if (result.getTime() < Date.MillisPerDay) {
result.setTime(result.getTime() + today.getTime());
}
}
return result;
}
}