From 7a0837fb0ef173cdbe4b006010a7ac83c98e865f Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Mon, 19 Aug 2019 15:19:26 +0100 Subject: [PATCH] Fix Y2038 issue in DateUtils.isToday() Before this change isToday() would stop working in Y2038 because android.text.format.Time doesn't work after Y2038. This commit fixes the code by using java.time classes that are Y2038 safe. Bug: 16550209 Test: atest android.text.format.cts.DateUtilsTest Change-Id: Ied339a694819c308add8645c7434b645dc1ccdde --- core/java/android/text/format/DateUtils.java | 25 +++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/core/java/android/text/format/DateUtils.java b/core/java/android/text/format/DateUtils.java index e94b8006d24ad..b00a9385eec81 100644 --- a/core/java/android/text/format/DateUtils.java +++ b/core/java/android/text/format/DateUtils.java @@ -32,6 +32,9 @@ import libcore.icu.LocaleData; import libcore.icu.RelativeDateTimeFormatter; import java.io.IOException; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.Calendar; import java.util.Date; import java.util.Formatter; @@ -502,17 +505,21 @@ public class DateUtils * @return true if the supplied when is today else false */ public static boolean isToday(long when) { - Time time = new Time(); - time.set(when); + return isSameDate(when, System.currentTimeMillis()); + } - int thenYear = time.year; - int thenMonth = time.month; - int thenMonthDay = time.monthDay; + private static boolean isSameDate(long oneMillis, long twoMillis) { + ZoneId zoneId = ZoneId.systemDefault(); - time.set(System.currentTimeMillis()); - return (thenYear == time.year) - && (thenMonth == time.month) - && (thenMonthDay == time.monthDay); + Instant oneInstant = Instant.ofEpochMilli(oneMillis); + LocalDateTime oneLocalDateTime = LocalDateTime.ofInstant(oneInstant, zoneId); + + Instant twoInstant = Instant.ofEpochMilli(twoMillis); + LocalDateTime twoLocalDateTime = LocalDateTime.ofInstant(twoInstant, zoneId); + + return (oneLocalDateTime.getYear() == twoLocalDateTime.getYear()) + && (oneLocalDateTime.getMonthValue() == twoLocalDateTime.getMonthValue()) + && (oneLocalDateTime.getDayOfMonth() == twoLocalDateTime.getDayOfMonth()); } /**