Skip to content

Commit 37e5e93

Browse files
mzhang4copybara-github
authored andcommitted
Update java.util.date and hack to support date.toInstant and Date.From(instant).
PiperOrigin-RevId: 431763716
1 parent bf0da50 commit 37e5e93

File tree

3 files changed

+313
-183
lines changed

3 files changed

+313
-183
lines changed

jre_emul/android/platform/libcore/luni/src/test/java/libcore/java/util/DateTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package libcore.java.util;
1818

19+
import java.time.Instant;
1920
import java.util.Calendar;
2021
import java.util.Date;
2122
import java.util.Locale;
@@ -79,4 +80,33 @@ public void test_parse_timezones() {
7980
Date.parse("Wed, 06 Jan 2016 11:55:59 GMT+05"));
8081

8182
}
83+
84+
/**
85+
* Checks conversion between long, Date and Instant.
86+
*/
87+
public void test_convertToInstantAndBack() {
88+
check_convertToInstantAndBack(0);
89+
check_convertToInstantAndBack(-1);
90+
check_convertToInstantAndBack( 999999999);
91+
check_convertToInstantAndBack(1000000000);
92+
check_convertToInstantAndBack(1000000001);
93+
check_convertToInstantAndBack(1000000002);
94+
check_convertToInstantAndBack(1000000499);
95+
check_convertToInstantAndBack(1000000500);
96+
check_convertToInstantAndBack(1000000999);
97+
check_convertToInstantAndBack(1000001000);
98+
check_convertToInstantAndBack(Long.MIN_VALUE + 808); // minimum ofEpochMilli argument
99+
check_convertToInstantAndBack(Long.MAX_VALUE);
100+
check_convertToInstantAndBack(System.currentTimeMillis());
101+
check_convertToInstantAndBack(Date.parse("Wed, 06 Jan 2016 11:55:59 GMT+0500"));
102+
}
103+
private static void check_convertToInstantAndBack(long millis) {
104+
Date date = new Date(millis);
105+
Instant instant = date.toInstant();
106+
assertEquals(date, Date.from(instant));
107+
assertEquals(instant, Instant.ofEpochMilli(millis));
108+
assertEquals("Millis should be a millions of nanos", 0, instant.getNano() % 1000000);
109+
assertEquals(millis, date.getTime());
110+
assertEquals(millis, instant.toEpochMilli());
111+
}
82112
}

jre_emul/android/platform/libcore/ojluni/src/main/java/java/time/Instant.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,3 +1363,47 @@ static Instant readExternal(DataInput in) throws IOException {
13631363
}
13641364

13651365
}
1366+
1367+
/*-HEADER[
1368+
#include "java/util/Date.h"
1369+
1370+
@class JavaTimeInstant; // Needed because HEADER comments are grouped with #includes.
1371+
1372+
@interface JavaUtilDate (JavaUtilTime)
1373+
1374+
- (JavaTimeInstant *)toInstant;
1375+
+ (JavaUtilDate *)fromWithJavaTimeInstant:(JavaTimeInstant *)instant;
1376+
1377+
@end
1378+
1379+
FOUNDATION_EXPORT JavaUtilDate *JavaUtilDate_fromWithJavaTimeInstant_(id instant);
1380+
]-*/
1381+
1382+
/*-[
1383+
1384+
#include "java/lang/ArithmeticException.h"
1385+
#include "java/lang/IllegalArgumentException.h"
1386+
1387+
@implementation JavaUtilDate (JavaUtilTime)
1388+
1389+
- (JavaTimeInstant *)toInstant {
1390+
return JavaTimeInstant_ofEpochMilliWithLong_([self getTime]);
1391+
}
1392+
1393+
+ (JavaUtilDate *)fromWithJavaTimeInstant:(JavaTimeInstant *)instant {
1394+
return JavaUtilDate_fromWithJavaTimeInstant_(instant);
1395+
}
1396+
1397+
@end
1398+
1399+
JavaUtilDate *JavaUtilDate_fromWithJavaTimeInstant_(JavaTimeInstant *instant) {
1400+
JavaUtilDate_initialize();
1401+
@try {
1402+
return create_JavaUtilDate_initWithLong_([((JavaTimeInstant *) nil_chk(instant)) toEpochMilli]);
1403+
}
1404+
@catch (JavaLangArithmeticException *ex) {
1405+
@throw create_JavaLangIllegalArgumentException_initWithJavaLangThrowable_(ex);
1406+
}
1407+
}
1408+
1409+
]-*/

0 commit comments

Comments
 (0)