private static int time4j(boolean bounded) { // supports both definitions PlainDate today = SystemClock.inLocalView().today(); // using system timezone return today.get( (bounded ? Weekmodel.ISO.boundedWeekOfMonth() : Weekmodel.ISO.weekOfMonth())); }
@Override public boolean contains(PlainDate temporal) { return ((temporal.getYear() == this.year) && (temporal.get(QUARTER_OF_YEAR) == this.quarter)); }
private <T extends ChronoEntity<T>> T doApply(T entity) { if (entity.contains(PlainDate.CALENDAR_DATE)) { PlainDate date = entity.get(PlainDate.CALENDAR_DATE); Weekday current = date.get(PlainDate.DAY_OF_WEEK); int delta = this.dayOfWeek.getValue() - current.getValue(); int dom = date.getDayOfMonth() + delta; long days; if (this.ordinal == LAST) { days = (5L - (Math.floorDiv(dom - 1, 7) + 1)) * 7 + delta; int max = GregorianMath.getLengthOfMonth(date.getYear(), date.getMonth()); if (date.getDayOfMonth() + days > max) { days -= 7; } } else { days = (this.ordinal - (Math.floorDiv(dom - 1, 7) + 1)) * 7 + delta; } date = date.plus(days, CalendarUnit.DAYS); return entity.with(PlainDate.CALENDAR_DATE, date); } else { throw new ChronoException( "Rule not found for ordinal day of week in month: " + entity); } }
Object enumValue = date.get(this.getElement()); int oldOrdinal = Enum.class.cast(enumValue).ordinal(); int newOrdinal = this.delta(oldOrdinal);
/** * <p>Converts given gregorian date to a quarter year. </p> * * @param date gregorian calendar date (for example {@code PlainDate} * @return CalendarQuarter * @throws IllegalArgumentException if given date is invalid * @since 3.28/4.24 */ /*[deutsch] * <p>Konvertiert das angegebene gregorianische Datum zu einem Kalenderquartal. </p> * * @param date gregorian calendar date (for example {@code PlainDate} * @return CalendarQuarter * @throws IllegalArgumentException if given date is invalid * @since 3.28/4.24 */ public static CalendarQuarter from(GregorianDate date) { PlainDate iso = PlainDate.from(date); // includes validation return CalendarQuarter.of(iso.getYear(), iso.get(PlainDate.QUARTER_OF_YEAR)); }
/** * <p>Liefert die ISO-Kalenderwoche des Jahres. </p> * * <p>Als erste Kalenderwoche gilt die Woche, die mindestens vier Tage hat * und mit dem Montag anfängt. Die Woche davor ist dann die letzte * Woche des vorherigen Jahres und kann noch in das aktuelle Jahr * hineinreichen. </p> * * @return week of year in the range (1-53) * @see Weekmodel#ISO */ int getWeekOfYear() { return this.get(Weekmodel.ISO.weekOfYear()).intValue(); }
@Override public java.sql.Date from(PlainDate date) { int year = date.getYear(); if ((year < 1900) || (year > 9999)) { throw new ChronoException( "SQL-Date is only defined in year range of 1900-9999."); } long millis = // localMillis MathUtils.safeMultiply( date.get(EpochDays.UNIX), 86400 * 1000); if (!WITH_SQL_UTC_CONVERSION) { ZonalOffset offset = Timezone.ofSystem().getOffset(date, PlainTime.of(0)); millis -= offset.getIntegralAmount() * 1000; } return new java.sql.Date(millis); }
@Override public V getValue(PlainTimestamp context) { if (this.element.isDateElement()) { return context.date.get(this.element); } else if (this.element.isTimeElement()) { return context.time.get(this.element); } throw new ChronoException( "Missing rule for: " + this.element.name()); }
@Override public CalendarQuarter createFrom( TimeSource<?> clock, AttributeQuery attributes ) { Timezone zone; if (attributes.contains(Attributes.TIMEZONE_ID)) { zone = Timezone.of(attributes.get(Attributes.TIMEZONE_ID)); } else if (attributes.get(Attributes.LENIENCY, Leniency.SMART).isLax()) { zone = Timezone.ofSystem(); } else { return null; } PlainDate date = Moment.from(clock.currentTime()).toZonalTimestamp(zone.getID()).toDate(); return CalendarQuarter.of(date.getYear(), date.get(QUARTER_OF_YEAR)); }
@Override public java.sql.Timestamp from(PlainTimestamp tsp) { long dateMillis = // local millis MathUtils.safeMultiply( tsp.getCalendarDate().get(EpochDays.UNIX), 86400 * 1000 ); long timeMillis = tsp.getInt(PlainTime.MILLI_OF_DAY); if (!WITH_SQL_UTC_CONVERSION) { ZonalOffset offset = Timezone.ofSystem().getOffset(tsp, tsp); timeMillis -= offset.getIntegralAmount() * 1000; } java.sql.Timestamp ret = new java.sql.Timestamp( MathUtils.safeAdd(dateMillis, timeMillis)); ret.setNanos(tsp.getInt(PlainTime.NANO_OF_SECOND)); return ret; }