Merge "use Calendar in DateUtils format method"

This commit is contained in:
Elliott Hughes
2013-03-11 20:11:20 +00:00
committed by Gerrit Code Review

View File

@ -1057,30 +1057,34 @@ public class DateUtils
// computation below that'd otherwise be thrown out. // computation below that'd otherwise be thrown out.
boolean isInstant = (startMillis == endMillis); boolean isInstant = (startMillis == endMillis);
Time startDate; Calendar startCalendar, endCalendar;
Time startDate = new Time();
if (timeZone != null) { if (timeZone != null) {
startDate = new Time(timeZone); startCalendar = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
} else if (useUTC) { } else if (useUTC) {
startDate = new Time(Time.TIMEZONE_UTC); startCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
} else { } else {
startDate = new Time(); startCalendar = Calendar.getInstance();
} }
startDate.set(startMillis); startCalendar.setTimeInMillis(startMillis);
setTimeFromCalendar(startDate, startCalendar);
Time endDate; Time endDate = new Time();
int dayDistance; int dayDistance;
if (isInstant) { if (isInstant) {
endDate = startDate; endDate = startDate;
dayDistance = 0; dayDistance = 0;
} else { } else {
if (timeZone != null) { if (timeZone != null) {
endDate = new Time(timeZone); endCalendar = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
} else if (useUTC) { } else if (useUTC) {
endDate = new Time(Time.TIMEZONE_UTC); endCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
} else { } else {
endDate = new Time(); endCalendar = Calendar.getInstance();
} }
endDate.set(endMillis); endCalendar.setTimeInMillis(endMillis);
setTimeFromCalendar(endDate, endCalendar);
int startJulianDay = Time.getJulianDay(startMillis, startDate.gmtoff); int startJulianDay = Time.getJulianDay(startMillis, startDate.gmtoff);
int endJulianDay = Time.getJulianDay(endMillis, endDate.gmtoff); int endJulianDay = Time.getJulianDay(endMillis, endDate.gmtoff);
dayDistance = endJulianDay - startJulianDay; dayDistance = endJulianDay - startJulianDay;
@ -1423,6 +1427,20 @@ public class DateUtils
return formatter.format(fullFormat, timeString, startWeekDayString, dateString); return formatter.format(fullFormat, timeString, startWeekDayString, dateString);
} }
private static void setTimeFromCalendar(Time t, Calendar c) {
t.hour = c.get(Calendar.HOUR_OF_DAY);
t.minute = c.get(Calendar.MINUTE);
t.month = c.get(Calendar.MONTH);
t.monthDay = c.get(Calendar.DAY_OF_MONTH);
t.second = c.get(Calendar.SECOND);
t.weekDay = c.get(Calendar.DAY_OF_WEEK) - 1;
t.year = c.get(Calendar.YEAR);
t.yearDay = c.get(Calendar.DAY_OF_YEAR);
t.isDst = (c.get(Calendar.DST_OFFSET) != 0) ? 1 : 0;
t.gmtoff = c.get(Calendar.ZONE_OFFSET) + c.get(Calendar.DST_OFFSET);
t.timezone = c.getTimeZone().getID();
}
/** /**
* Formats a date or a time according to the local conventions. There are * Formats a date or a time according to the local conventions. There are
* lots of options that allow the caller to control, for example, if the * lots of options that allow the caller to control, for example, if the