本文整理匯總了Java中java.time.LocalDate.withDayOfMonth方法的典型用法代碼示例。如果您正苦於以下問題:Java LocalDate.withDayOfMonth方法的具體用法?Java LocalDate.withDayOfMonth怎麽用?Java LocalDate.withDayOfMonth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.time.LocalDate
的用法示例。
在下文中一共展示了LocalDate.withDayOfMonth方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: split
import java.time.LocalDate; //導入方法依賴的package包/類
/**
* Splits the given period into multiple slices of one month long.
*
* @param start the start of the period.
* @param end the end of the period.
* @return The list of slices result of the splitting.
*/
public static List<Slice> split(LocalDate start, LocalDate end) {
Objects.requireNonNull(start);
Objects.requireNonNull(end);
Preconditions.checkArgument(!start.isAfter(end));
List<Slice> slices = Lists.newArrayList();
LocalDate startOfMonth = start.withDayOfMonth(1);
LocalDate endOfMonth = YearMonth.from(end).atEndOfMonth();
do {
slices.add(new Slice(startOfMonth, YearMonth.from(startOfMonth).atEndOfMonth()));
startOfMonth = startOfMonth.plus(1, ChronoUnit.MONTHS);
}
while (startOfMonth.isBefore(endOfMonth) || startOfMonth.isEqual(endOfMonth));
return slices;
}
示例2: test_weekOfMonthField
import java.time.LocalDate; //導入方法依賴的package包/類
@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);
}
}
示例3: next
import java.time.LocalDate; //導入方法依賴的package包/類
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));
}
示例4: previous
import java.time.LocalDate; //導入方法依賴的package包/類
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())));
}