下面列出了java.util.GregorianCalendar#setFirstDayOfWeek ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void test_toCalendar_ZDT() {
ZonedDateTime zdt = ZonedDateTime.of(2012, 6, 30, 11, 30, 40, 0, PARIS);
GregorianCalendar calendar = new GregorianCalendar(PARIS_TZ);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setMinimalDaysInFirstWeek(4);
calendar.set(2012, 6 - 1, 30, 11, 30, 40);
calendar.set(Calendar.MILLISECOND, 0);
calendar.setTimeInMillis(calendar.getTimeInMillis());
calendar.setGregorianChange(new Date(Long.MIN_VALUE));
GregorianCalendar test = DateTimeUtils.toGregorianCalendar(zdt);
assertEquals(test, calendar);
}
/**
* 当前星期的第一天
* @param date
* @return
*/
public static Date getWeekStartDate(Date date){
GregorianCalendar c = new GregorianCalendar(Locale.CHINA);
c.setTime(date);
c.setFirstDayOfWeek(RANGE_WEEK_MONDAY);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
c.set(Calendar.HOUR_OF_DAY, 00);
c.set(Calendar.MINUTE, 00);
c.set(Calendar.SECOND, 00);
return c.getTime();
}
/**
* 当前星期的最后一天
* @param date
* @return
*/
public static Date getWeekEndDate(Date date){
GregorianCalendar c = new GregorianCalendar(Locale.CHINA);
c.setTime(date);
c.setFirstDayOfWeek(RANGE_WEEK_MONDAY);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return c.getTime();
}
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal);
assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal);
assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal);
assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal);
assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal);
assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
@Test
@UseDataProvider("provider_rangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals("Unexpected calendar type", cal.getCalendarType(), "gregory");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals("Day mismatch in " + isoDate + "; cal: " + cal,
isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH));
assertEquals("Month mismatch in " + isoDate, isoDate.get(MONTH_OF_YEAR),
cal.get(Calendar.MONTH) + 1);
assertEquals("Year mismatch in " + isoDate, isoDate.get(YEAR_OF_ERA),
cal.get(Calendar.YEAR));
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals("Calendar DayOfWeek does not match ISO DayOfWeek", jdow, dow);
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals("Calendar WeekOfMonth does not match ISO WeekOfMonth",
jweekOfMonth, isoWeekOfMonth);
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals("GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear",
jweekOfYear, weekOfYear);
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals("GregorianCalendar getWeekYear does not match YearOfWeekBasedYear",
jWeekYear, weekBasedYear);
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals("length of weekBasedYear", jweeksInWeekyear, weeksInWeekBasedYear);
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal);
assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
@Test(dataProvider = "RangeVersusCalendar")
public void test_DayOfWeek_IsoChronology_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
GregorianCalendar cal = new GregorianCalendar();
assertEquals(cal.getCalendarType(), "gregory", "Unexpected calendar type");
LocalDate isoDate = IsoChronology.INSTANCE.date(isoStartDate);
for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
for (int minDays = 1; minDays <= 7; minDays++) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
cal.setFirstDayOfWeek(Math.floorMod(firstDayOfWeek.getValue(), 7) + 1);
cal.setMinimalDaysInFirstWeek(minDays);
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.set(Calendar.YEAR, isoDate.get(YEAR));
cal.set(Calendar.MONTH, isoDate.get(MONTH_OF_YEAR) - 1);
cal.set(Calendar.DAY_OF_MONTH, isoDate.get(DAY_OF_MONTH));
// For every date in the range
while (isoDate.isBefore(isoEndDate)) {
assertEquals(isoDate.get(DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + isoDate + "; cal: " + cal);
assertEquals(isoDate.get(MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + isoDate);
assertEquals(isoDate.get(YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + isoDate);
int jdow = Math.floorMod(cal.get(Calendar.DAY_OF_WEEK) - 2, 7) + 1;
int dow = isoDate.get(weekDef.dayOfWeek());
assertEquals(jdow, dow, "Calendar DayOfWeek does not match ISO DayOfWeek");
int jweekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int isoWeekOfMonth = isoDate.get(weekDef.weekOfMonth());
assertEquals(jweekOfMonth, isoWeekOfMonth, "Calendar WeekOfMonth does not match ISO WeekOfMonth");
int jweekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
int weekOfYear = isoDate.get(weekDef.weekOfWeekBasedYear());
assertEquals(jweekOfYear, weekOfYear, "GregorianCalendar WeekOfYear does not match WeekOfWeekBasedYear");
int jWeekYear = cal.getWeekYear();
int weekBasedYear = isoDate.get(weekDef.weekBasedYear());
assertEquals(jWeekYear, weekBasedYear, "GregorianCalendar getWeekYear does not match YearOfWeekBasedYear");
int jweeksInWeekyear = cal.getWeeksInWeekYear();
int weeksInWeekBasedYear = (int)isoDate.range(weekDef.weekOfWeekBasedYear()).getMaximum();
assertEquals(jweeksInWeekyear, weeksInWeekBasedYear, "length of weekBasedYear");
isoDate = isoDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
}