/** * Converts a {@code ZonedDateTime} to a {@code Calendar}. * <p> * The resulting {@code GregorianCalendar} is pure Gregorian and uses * ISO week definitions, starting on Monday and with 4 days in a minimal week. * <p> * Fractions of the instant smaller than milliseconds will be dropped. * * @param zdt the zoned date-time, not null * @return the calendar, not null * @throws IllegalArgumentException if the conversion fails */ public static GregorianCalendar toGregorianCalendar(ZonedDateTime zdt) { TimeZone zone = toTimeZone(zdt.getZone()); GregorianCalendar cal = new GregorianCalendar(zone); cal.setGregorianChange(new Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); try { cal.setTimeInMillis(zdt.toInstant().toEpochMilli()); } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } return cal; }
/** * Converts a {@code ZonedDateTime} to a {@code Calendar}. * <p> * The resulting {@code GregorianCalendar} is pure Gregorian and uses * ISO week definitions, starting on Monday and with 4 days in a minimal week. * <p> * Fractions of the instant smaller than milliseconds will be dropped. * * @param zdt the zoned date-time, not null * @return the calendar, not null * @throws IllegalArgumentException if the conversion fails */ public static GregorianCalendar toGregorianCalendar(ZonedDateTime zdt) { TimeZone zone = toTimeZone(zdt.getZone()); GregorianCalendar cal = new GregorianCalendar(zone); cal.setGregorianChange(new Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); try { cal.setTimeInMillis(zdt.toInstant().toEpochMilli()); } catch (ArithmeticException ex) { throw new IllegalArgumentException(ex); } return cal; }