Java Code Examples for java.time.LocalDate#withDayOfMonth()

The following examples show how to use java.time.LocalDate#withDayOfMonth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImmutableHolidayCalendar.java    From Strata with Apache License 2.0 6 votes vote down vote up
private LocalDate shiftNextSameLast(LocalDate baseDate) {
  int baseYear = baseDate.getYear();
  int baseMonth = baseDate.getMonthValue();
  int baseDom = baseDate.getDayOfMonth();
  // find data for month
  int index = (baseYear - startYear) * 12 + baseMonth - 1;
  int monthData = lookup[index];
  // shift to move the target day-of-month into bit-0, removing earlier days
  int shifted = monthData >>> (baseDom - 1);
  // return last business day-of-month if no more business days in the month
  int dom;
  if (shifted == 0) {
    // need to find the most significant bit, which is the last business day
    // use JDK numberOfLeadingZeros() method which is mapped to a fast intrinsic
    int leading = Integer.numberOfLeadingZeros(monthData);
    dom = 32 - leading;
  } else {
    // find least significant bit, which is the next/same business day
    // use JDK numberOfTrailingZeros() method which is mapped to a fast intrinsic
    dom = baseDom + Integer.numberOfTrailingZeros(shifted);
  }
  // only one call to LocalDate to aid inlining
  return baseDate.withDayOfMonth(dom);
}
 
Example 2
Source File: TestLocalDate.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 3
Source File: TCKLocalDate.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 4
Source File: ImmutableHolidayCalendar.java    From Strata with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDate lastBusinessDayOfMonth(LocalDate date) {
  try {
    // find data for month
    int index = (date.getYear() - startYear) * 12 + date.getMonthValue() - 1;
    // need to find the most significant bit, which is the last business day
    // use JDK numberOfLeadingZeros() method which is mapped to a fast intrinsic
    int leading = Integer.numberOfLeadingZeros(lookup[index]);
    return date.withDayOfMonth(32 - leading);

  } catch (ArrayIndexOutOfBoundsException ex) {
    return lastBusinessDayOfMonthOutOfRange(date);
  }
}
 
Example 5
Source File: TestLocalDate.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 6
Source File: TestLocalDate.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate next(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() + 1;
    if (newDayOfMonth <= date.getMonth().length(isIsoLeap(date.getYear()))) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.withDayOfMonth(1);
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() + 1);
    }
    return date.with(date.getMonth().plus(1));
}
 
Example 7
Source File: TCKLocalDate.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 8
Source File: TCKLocalDate.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate next(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() + 1;
    if (newDayOfMonth <= date.getMonth().length(isIsoLeap(date.getYear()))) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.withDayOfMonth(1);
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() + 1);
    }
    return date.with(date.getMonth().plus(1));
}
 
Example 9
Source File: DateTest.java    From java-master with Apache License 2.0 5 votes vote down vote up
@Test
public void test3() {
    LocalDate date1 = LocalDate.of(2014, 3, 18);
    LocalDate date2 = date1.withYear(2011);
    LocalDate date3 = date2.withDayOfMonth(25);
    LocalDate date4 = date3.with(ChronoField.MONTH_OF_YEAR, 9);
    // 以相对方式创建LocalDate对象修改版
    date1 = LocalDate.of(2014, 3, 18);
    date2 = date1.plusWeeks(1);
    date3 = date2.minusYears(3);
    date4 = date3.plus(6, ChronoUnit.MONTHS);
}
 
Example 10
Source File: SequenceService.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
protected SequenceVersion getVersionByMonth(Sequence sequence, LocalDate refDate) {

    SequenceVersion sequenceVersion =
        sequenceVersionRepository.findByMonth(sequence, refDate.getMonthValue(), refDate.getYear());
    if (sequenceVersion == null) {
      sequenceVersion =
          new SequenceVersion(
              sequence,
              refDate.withDayOfMonth(1),
              refDate.withDayOfMonth(refDate.lengthOfMonth()),
              1L);
    }

    return sequenceVersion;
  }
 
Example 11
Source File: TCKLocalDate.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 12
Source File: TCKLocalDate.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate next(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() + 1;
    if (newDayOfMonth <= date.getMonth().length(isIsoLeap(date.getYear()))) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.withDayOfMonth(1);
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() + 1);
    }
    return date.with(date.getMonth().plus(1));
}
 
Example 13
Source File: TestLocalDate.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 14
Source File: TestLocalDate.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 15
Source File: TCKLocalDate.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate next(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() + 1;
    if (newDayOfMonth <= date.getMonth().length(isIsoLeap(date.getYear()))) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.withDayOfMonth(1);
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() + 1);
    }
    return date.with(date.getMonth().plus(1));
}
 
Example 16
Source File: TestLocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate next(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() + 1;
    if (newDayOfMonth <= date.getMonth().length(isIsoLeap(date.getYear()))) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.withDayOfMonth(1);
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() + 1);
    }
    return date.with(date.getMonth().plus(1));
}
 
Example 17
Source File: TCKWeekFields.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_weekOfMonthField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOM = day.get(womField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = day.withDayOfMonth(1);
        int offset = - (day1.get(dowField) - 1);

        int week1 = day1.get(womField);
        if (week1 == 0) {
            // week of the 1st is partial; start with first full week
            offset += 7;
        }

        offset += actualDOW - 1;
        offset += (actualWOM - 1) * 7;
        LocalDate result = day1.plusDays(offset);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfMonth: "
                + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
                week, day.getDayOfWeek(), offset, actualDOW, actualWOM, day, result));
        day = day.plusDays(1);
    }
}
 
Example 18
Source File: TCKLocalDate.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 19
Source File: TimeDiscreteEndOfMonthIndex.java    From finmath-lib with Apache License 2.0 4 votes vote down vote up
@Override
public RandomVariable getValue(final double evaluationTime, final LIBORModelMonteCarloSimulationModel model) throws CalculationException {

	final LocalDate referenceDate = model.getModel().getForwardRateCurve().getReferenceDate();

	final LocalDate evaluationDate = FloatingpointDate.getDateFromFloatingPointDate(referenceDate, evaluationTime);

	// Roll to start of month (to prevent "overflow)
	LocalDate endDate = evaluationDate.withDayOfMonth(1).plusMonths(fixingOffsetMonths);

	// Roll to end of month.
	endDate = endDate.withDayOfMonth(endDate.lengthOfMonth());

	final double time = FloatingpointDate.getFloatingPointDateFromDate(referenceDate, endDate);
	return baseIndex.getValue(time, model);
}
 
Example 20
Source File: ForecastRecapService.java    From axelor-open-suite with GNU Affero General Public License v3.0 4 votes vote down vote up
public void populateWithSalaries(ForecastRecap forecastRecap) throws AxelorException {
  List<Employee> employeeList = new ArrayList<Employee>();
  ForecastRecapLineType salaryForecastRecapLineType =
      this.getForecastRecapLineType(ForecastRecapLineTypeRepository.ELEMENT_SALARY);
  if (forecastRecap.getBankDetails() != null) {
    employeeList =
        Beans.get(EmployeeRepository.class)
            .all()
            .filter(
                "self.mainEmploymentContract.payCompany = ?1 AND self.bankDetails = ?2",
                forecastRecap.getCompany(),
                forecastRecap.getBankDetails())
            .fetch();
  } else {
    employeeList =
        Beans.get(EmployeeRepository.class)
            .all()
            .filter("self.mainEmploymentContract.payCompany = ?1", forecastRecap.getCompany())
            .fetch();
  }
  LocalDate itDate =
      LocalDate.parse(forecastRecap.getFromDate().toString(), DateTimeFormatter.ISO_DATE);
  while (!itDate.isAfter(forecastRecap.getToDate())) {
    LocalDate monthEnd = itDate.withDayOfMonth(itDate.lengthOfMonth());
    if (itDate.isEqual(monthEnd)) {
      for (Employee employee : employeeList) {
        forecastRecap.addForecastRecapLineListItem(
            this.createForecastRecapLine(
                itDate,
                salaryForecastRecapLineType.getTypeSelect(),
                employee
                    .getHourlyRate()
                    .multiply(employee.getWeeklyWorkHours().multiply(new BigDecimal(4))),
                null,
                null,
                null,
                salaryForecastRecapLineType));
      }
      itDate = itDate.plusMonths(1);
    } else {
      itDate = monthEnd;
    }
  }
}