From 9a2ada418b0a53cb10e009398116ff8cd914d71e Mon Sep 17 00:00:00 2001 From: Sungmin Choi Date: Thu, 28 Feb 2013 20:17:15 +0900 Subject: [PATCH] use Calendar in DateUtils format method The DatePickerDialog is using DateUtils to format the dialog's title and the DateUtils class does not work with dates outside to the specified range. For example, if user selects 2038-03-07, DatePickerDialog shows 1902-01-30 on Title. The reason for the DateUtils class not being able to format dates outside the range of 1902 and 2036 is because internally it is using the Time class which does not support such dates. To fix it, use Calendar class in DataUilts format method. Steps to reproduce: 1. Settings -> Date & time 2. uncheck Automatic date & time 3. Set Date 4. choose any date before 1902 or after 2037 5. update wrong date on Title Bug: https://code.google.com/p/android/issues/detail?id=13050 Change-Id: I003266765751b5c340426af84daef271f39f771e --- core/java/android/text/format/DateUtils.java | 38 ++++++++++++++------ 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/core/java/android/text/format/DateUtils.java b/core/java/android/text/format/DateUtils.java index 08813dfaf5fb9..6c8a7379eb63d 100644 --- a/core/java/android/text/format/DateUtils.java +++ b/core/java/android/text/format/DateUtils.java @@ -1057,30 +1057,34 @@ public class DateUtils // computation below that'd otherwise be thrown out. boolean isInstant = (startMillis == endMillis); - Time startDate; + Calendar startCalendar, endCalendar; + Time startDate = new Time(); if (timeZone != null) { - startDate = new Time(timeZone); + startCalendar = Calendar.getInstance(TimeZone.getTimeZone(timeZone)); } else if (useUTC) { - startDate = new Time(Time.TIMEZONE_UTC); + startCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); } else { - startDate = new Time(); + startCalendar = Calendar.getInstance(); } - startDate.set(startMillis); + startCalendar.setTimeInMillis(startMillis); + setTimeFromCalendar(startDate, startCalendar); - Time endDate; + Time endDate = new Time(); int dayDistance; if (isInstant) { endDate = startDate; dayDistance = 0; } else { if (timeZone != null) { - endDate = new Time(timeZone); + endCalendar = Calendar.getInstance(TimeZone.getTimeZone(timeZone)); } else if (useUTC) { - endDate = new Time(Time.TIMEZONE_UTC); + endCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); } else { - endDate = new Time(); + endCalendar = Calendar.getInstance(); } - endDate.set(endMillis); + endCalendar.setTimeInMillis(endMillis); + setTimeFromCalendar(endDate, endCalendar); + int startJulianDay = Time.getJulianDay(startMillis, startDate.gmtoff); int endJulianDay = Time.getJulianDay(endMillis, endDate.gmtoff); dayDistance = endJulianDay - startJulianDay; @@ -1423,6 +1427,20 @@ public class DateUtils 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 * lots of options that allow the caller to control, for example, if the