本文整理汇总了Java中java.time.LocalDateTime.with方法的典型用法代码示例。如果您正苦于以下问题:Java LocalDateTime.with方法的具体用法?Java LocalDateTime.with怎么用?Java LocalDateTime.with使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.LocalDateTime
的用法示例。
在下文中一共展示了LocalDateTime.with方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRoundingDate
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test
public void testRoundingDate() {
int month = randomIntBetween(1, 12);
int day = randomIntBetween(1, 28);
int hourOfDay = randomIntBetween(0, 23);
int minute = randomIntBetween(0, 59);
int year = randomIntBetween(2016, 2025);
LocalDateTime dateTime = LocalDateTime.of(year, month, day, hourOfDay, minute);
LocalDateTime roundedDate = Generator.roundEndDateToTheNextHalfHour(dateTime);
assertThat(roundedDate.isAfter(dateTime), is(true));
LocalDateTime expectedDate = dateTime.with(temporal -> {
int currentMinute = temporal.get(ChronoField.MINUTE_OF_HOUR);
if (currentMinute >= 30) {
return temporal.plus(60 - currentMinute, ChronoUnit.MINUTES);
} else {
return temporal.plus(30 - currentMinute, ChronoUnit.MINUTES);
}
});
assertThat(roundedDate, is(expectedDate));
}
示例2: getDateRelativeToToday
import java.time.LocalDateTime; //导入方法依赖的package包/类
private LocalDateTime getDateRelativeToToday(ChronoField field, int buffer) {
LocalDateTime date = LocalDateTime.now();
// want a date with a buffer either side of it and also not including the date itself
int gap = 2 * buffer + 1;
int max = (int)field.range().getSmallestMaximum();
int rand = getRandomInt(1, max - gap);
int value = rand >= date.get(field) - buffer ? rand + gap : rand;
LocalDateTime relativeDate = date.with(field, value);
// Adding logging to help with some spurious errors...
log.info("Date: " + date + " rand: " + rand + " value: " + value + " Picked date: " + relativeDate);
return relativeDate;
}
示例3: test_appendText2arg_format
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test(dataProvider="printText")
public void test_appendText2arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
DateTimeFormatter f = builder.appendText(field, style).toFormatter(Locale.ENGLISH);
LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
dt = dt.with(field, value);
String text = f.format(dt);
assertEquals(text, expected);
}
示例4: test_appendText1arg_format
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
if (style == TextStyle.FULL) {
DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
dt = dt.with(field, value);
String text = f.format(dt);
assertEquals(text, expected);
}
}
示例5: adjustField
import java.time.LocalDateTime; //导入方法依赖的package包/类
private static LocalDateTime adjustField(LocalDateTime time, ChronoField field, int stepRate) {
return time.with(field, time.get(field) - time.get(field) % stepRate);
}