本文整理汇总了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())));
}