@VisibleForTesting DateTime getEffectiveDateForNewBCD(final int bcd, @Nullable final LocalDate effectiveFromDate, final InternalCallContext internalCallContext) { if (internalCallContext.getAccountRecordId() == null) { throw new IllegalStateException("Need to have a valid context with accountRecordId"); } // Today as seen by this account final LocalDate startDate = effectiveFromDate != null ? effectiveFromDate : internalCallContext.toLocalDate(internalCallContext.getCreatedDate()); // We want to compute a LocalDate in account TZ which maps to the provided 'bcd' and then compute an effectiveDate for when that BCD_CHANGE event needs to be triggered // // There is a bit of complexity to make sure the date we chose exists (e.g: a BCD of 31 in a february month would not make sense). final int currentDay = startDate.getDayOfMonth(); final int lastDayOfMonth = startDate.dayOfMonth().getMaximumValue(); final LocalDate requestedDate; if (bcd < currentDay) { final LocalDate startDatePlusOneMonth = startDate.plusMonths(1); final int lastDayOfNextMonth = startDatePlusOneMonth.dayOfMonth().getMaximumValue(); final int originalBCDORLastDayOfMonth = bcd <= lastDayOfNextMonth ? bcd : lastDayOfNextMonth; requestedDate = new LocalDate(startDatePlusOneMonth.getYear(), startDatePlusOneMonth.getMonthOfYear(), originalBCDORLastDayOfMonth); } else if (bcd == currentDay && effectiveFromDate == null) { // will default to immediate event requestedDate = null; } else if (bcd <= lastDayOfMonth) { requestedDate = new LocalDate(startDate.getYear(), startDate.getMonthOfYear(), bcd); } else /* bcd > lastDayOfMonth && bcd > currentDay */ { requestedDate = new LocalDate(startDate.getYear(), startDate.getMonthOfYear(), lastDayOfMonth); } return requestedDate == null ? internalCallContext.getCreatedDate() : internalCallContext.toUTCDateTime(requestedDate); }
public static LocalDate alignProposedBillCycleDate(final LocalDate proposedDate, final int billingCycleDay, final BillingPeriod billingPeriod) { // billingCycleDay alignment only makes sense for month based BillingPeriod (MONTHLY, QUARTERLY, BIANNUAL, ANNUAL) final boolean isMonthBased = (billingPeriod.getPeriod().getMonths() | billingPeriod.getPeriod().getYears()) > 0; if (!isMonthBased) { return proposedDate; } final int lastDayOfMonth = proposedDate.dayOfMonth().getMaximumValue(); int proposedBillCycleDate = proposedDate.getDayOfMonth(); if (proposedBillCycleDate < billingCycleDay) { if (billingCycleDay <= lastDayOfMonth) { proposedBillCycleDate = billingCycleDay; } else { proposedBillCycleDate = lastDayOfMonth; } } return new LocalDate(proposedDate.getYear(), proposedDate.getMonthOfYear(), proposedBillCycleDate, proposedDate.getChronology()); }
@Override public void serialize(LocalDate dt, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { if (provider.isEnabled(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS)) { // Timestamp here actually means an array of values jgen.writeStartArray(); jgen.writeNumber(dt.year().get()); jgen.writeNumber(dt.monthOfYear().get()); jgen.writeNumber(dt.dayOfMonth().get()); jgen.writeEndArray(); } else { jgen.writeString(printLocalDate(dt)); } }
@Override public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider provider) throws IOException { if (_serializationShape(provider) == FORMAT_STRING) { gen.writeString(_format.createFormatter(provider).print(value)); return; } // 28-Jul-2017, tatu: Wrt [dataformat-joda#39]... we could perhaps support timestamps, // but only by specifying what to do with time (`LocalTime`) AND timezone. For now, // seems like asking for trouble really... so only use array notation. gen.writeStartArray(); gen.writeNumber(value.year().get()); gen.writeNumber(value.monthOfYear().get()); gen.writeNumber(value.dayOfMonth().get()); gen.writeEndArray(); } }
// Parse using DateTimeFormatter.parseDateTime().toLocalDate(); LocalDate original = ...; LocalDate startOfMonth = original.dayOfMonth().withMinimumValue(); LocalDate endOfMonth = original.dayOfMonth().withMaximumValue();
public static final int JANUARY = 1; public static final int DECEMBER = 12; public static final int FIRST_OF_THE_MONTH = 1; public final int getLastDayOfMonth(final int month, final int year) { int lastDay = 0; if ((month >= JANUARY) && (month <= DECEMBER)) { LocalDate aDate = new LocalDate(year, month, FIRST_OF_THE_MONTH); lastDay = aDate.dayOfMonth().getMaximumValue(); } return lastDay; }
@Override public void serialize(LocalDate dt, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { if (provider.isEnabled(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS)) { // Timestamp here actually means an array of values jgen.writeStartArray(); jgen.writeNumber(dt.year().get()); jgen.writeNumber(dt.monthOfYear().get()); jgen.writeNumber(dt.dayOfMonth().get()); jgen.writeEndArray(); } else { jgen.writeString(printLocalDate(dt)); } }
@Override public int getRowCount(int column) { int nbYears = (firstMonth + column - 1) / 12; LocalDate date = new LocalDate(year + nbYears, (firstMonth + column - 1) % 12 + 1, 1); return date.dayOfMonth().getMaximumValue(); }
/** * Get a LocalDate for the first day of a month. * @return LocalDate */ public static LocalDate firstDayMonth(LocalDate localDate) { return localDate.dayOfMonth().withMinimumValue(); }
/** * Get a LocalDate for the last day of a month. * @return LocalDate */ public static LocalDate lastDayMonth(LocalDate localDate) { return localDate.dayOfMonth().withMaximumValue(); } }
/** * Get a LocalDate for the first day of the previous month. * @return LocalDate */ public static LocalDate firstDayPreviousMonth() { return new LocalDate().minusMonths(1).dayOfMonth().withMinimumValue(); }
public static DateBounds getMonthBounds(LocalDate actualDate) { LocalDate lowBound = actualDate.dayOfMonth().withMinimumValue(); LocalDate hiBound = actualDate.dayOfMonth().withMaximumValue(); return new DateBounds(lowBound, hiBound); }
/** * Get a LocalDate for the last day of the previous month. * @return LocalDate */ public static LocalDate lastDayPreviousMonth() { return new LocalDate().minusMonths(1).dayOfMonth().withMaximumValue(); }
public static LocalDate getEndOfMonth(final int year, final int month) { return (new LocalDate()) .withWeekyear(year) .withMonthOfYear(month).dayOfMonth().withMaximumValue(); }
public static LocalDate getBeginningOfMonth(final int year, final int month) { return (new LocalDate()) .withWeekyear(year) .withMonthOfYear(month).dayOfMonth().withMinimumValue(); }
private LocalDate alignProposedBillCycleDate(final LocalDate proposedDate) { if (!shouldUsePatch) { return proposedDate; } final int lastDayOfMonth = proposedDate.dayOfMonth().getMaximumValue(); int proposedBillCycleDate = proposedDate.getDayOfMonth(); if (proposedBillCycleDate < billingCycleDay && billingCycleDay <= lastDayOfMonth) { proposedBillCycleDate = billingCycleDay; } return new LocalDate(proposedDate.getYear(), proposedDate.getMonthOfYear(), proposedBillCycleDate, proposedDate.getChronology()); } }
private void init() { LocalDate firstDate = new LocalDate(year, month, 1); startDate = firstDate.withDayOfWeek(DateTimeConstants.MONDAY); LocalDate lastDate = firstDate.withDayOfMonth(firstDate.dayOfMonth().getMaximumValue()); LocalDate endDate; if (lastDate.getDayOfWeek() != DateTimeConstants.SUNDAY) { endDate = lastDate.withDayOfWeek(DateTimeConstants.SUNDAY); } else { endDate = lastDate; } Duration d = new Duration(startDate.toDateMidnight(DateTimeZone.UTC), endDate.toDateMidnight(DateTimeZone.UTC)); nbWeek = ((int) d.getStandardDays() + 1) / 7; }
public static LocalDate calculateBillingCycleDateOnOrAfter(final LocalDate date, final int billingCycleDayLocal) { final int lastDayOfMonth = date.dayOfMonth().getMaximumValue(); final LocalDate fixedDate; if (billingCycleDayLocal > lastDayOfMonth) { fixedDate = new LocalDate(date.getYear(), date.getMonthOfYear(), lastDayOfMonth, date.getChronology()); } else { fixedDate = new LocalDate(date.getYear(), date.getMonthOfYear(), billingCycleDayLocal, date.getChronology()); } LocalDate proposedDate = fixedDate; while (proposedDate.isBefore(date)) { proposedDate = proposedDate.plusMonths(1); } return proposedDate; }
private int diff360EIsda(final LocalDate start, final LocalDate end) { if (start.equals(end)) { return 0; } int dayStart = start.getDayOfMonth(); int dayEnd = end.getDayOfMonth(); if (start.dayOfMonth().getMaximumValue() == dayStart) { dayStart = CalculatorConstants.MONTH_30_DAYS; } if (end.getMonthOfYear() != 2 && end.dayOfMonth().getMaximumValue() == dayEnd) { dayEnd = CalculatorConstants.MONTH_30_DAYS; } return (end.getYear() - start.getYear()) * CalculatorConstants.YEAR_360 + (end.getMonthOfYear() - start.getMonthOfYear()) * CalculatorConstants.MONTH_30_DAYS + dayEnd - dayStart; }
private void calculateFirstBillingCycleDate() { final int lastDayOfMonth = startDate.dayOfMonth().getMaximumValue(); final LocalDate billingCycleDate; if (billingCycleDay > lastDayOfMonth) { billingCycleDate = new LocalDate(startDate.getYear(), startDate.getMonthOfYear(), lastDayOfMonth, startDate.getChronology()); } else { billingCycleDate = new LocalDate(startDate.getYear(), startDate.getMonthOfYear(), billingCycleDay, startDate.getChronology()); } LocalDate proposedDate = billingCycleDate; while (proposedDate.isBefore(startDate)) { proposedDate = proposedDate.plus(billingPeriod.getPeriod()); } firstBillingCycleDate = BillCycleDayCalculator.alignProposedBillCycleDate(proposedDate, billingCycleDay, billingPeriod); }