本文整理汇总了Java中java.time.LocalDateTime.plus方法的典型用法代码示例。如果您正苦于以下问题:Java LocalDateTime.plus方法的具体用法?Java LocalDateTime.plus怎么用?Java LocalDateTime.plus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.LocalDateTime
的用法示例。
在下文中一共展示了LocalDateTime.plus方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: includes
import java.time.LocalDateTime; //导入方法依赖的package包/类
private boolean includes(TimeFrame frame, LocalDateTime dateTime) {
LocalDateTime start = LocalDateTime.from(frame.getStart());
LocalDateTime end = LocalDateTime.from(frame.getEnd());
int distance = (int) (ChronoUnit.DAYS.between(start, dateTime) - 1);
if (distance > 0) {
int factor = distance / frame.getRecurrence().getDays();
if (factor > 0) {
Period advance = frame.getRecurrence().multipliedBy(factor);
start.plus(advance);
end.plus(advance);
}
}
while (!start.isAfter(dateTime)) {
if (end.isAfter(dateTime)) {
return true;
}
start = start.plus(frame.getRecurrence());
end = end.plus(frame.getRecurrence());
}
return false;
}
示例2: changeStartDate
import java.time.LocalDateTime; //导入方法依赖的package包/类
/**
* Changes the start date of the entry interval.
*
* @param date the new start date
* @param keepDuration if true then this method will also change the end date and time in such a way that the total duration
* of the entry will not change. If false then this method will ensure that the entry's interval
* stays valid, which means that the start time will be before the end time and that the
* duration of the entry will be at least the duration defined by the {@link #minimumDurationProperty()}.
*/
public final void changeStartDate(LocalDate date, boolean keepDuration) {
requireNonNull(date);
Interval interval = getInterval();
LocalDateTime newStartDateTime = getStartAsLocalDateTime().with(date);
LocalDateTime endDateTime = getEndAsLocalDateTime();
if (keepDuration) {
endDateTime = newStartDateTime.plus(getDuration());
setInterval(newStartDateTime, endDateTime, getZoneId());
} else {
/*
* We might have a problem if the new start time is AFTER the current end time.
*/
if (newStartDateTime.isAfter(endDateTime)) {
interval = interval.withEndDateTime(newStartDateTime.plus(interval.getDuration()));
}
setInterval(interval.withStartDate(date));
}
}
示例3: changeStartTime
import java.time.LocalDateTime; //导入方法依赖的package包/类
/**
* Changes the start time of the entry interval.
*
* @param time the new start time
* @param keepDuration if true then this method will also change the end time in such a way that the total duration
* of the entry will not change. If false then this method will ensure that the entry's interval
* stays valid, which means that the start time will be before the end time and that the
* duration of the entry will be at least the duration defined by the {@link #minimumDurationProperty()}.
*/
public final void changeStartTime(LocalTime time, boolean keepDuration) {
requireNonNull(time);
Interval interval = getInterval();
LocalDateTime newStartDateTime = getStartAsLocalDateTime().with(time);
LocalDateTime endDateTime = getEndAsLocalDateTime();
if (keepDuration) {
endDateTime = newStartDateTime.plus(getDuration());
setInterval(newStartDateTime, endDateTime);
} else {
/*
* We might have a problem if the new start time is AFTER the current end time.
*/
if (newStartDateTime.isAfter(endDateTime.minus(getMinimumDuration()))) {
interval = interval.withEndDateTime(newStartDateTime.plus(getMinimumDuration()));
}
setInterval(interval.withStartTime(time));
}
}
示例4: getSlotsByDateRangeType
import java.time.LocalDateTime; //导入方法依赖的package包/类
private List<BookingSlot> getSlotsByDateRangeType(String type, LocalDate startDate) {
LocalDateTime startDateTime = startDate.atStartOfDay();
LocalDateTime endDateTime;
switch (type) {
case DATE:
endDateTime = startDateTime.plus(1, ChronoUnit.DAYS);
break;
case SCHOOL_WEEK:
endDateTime = startDateTime.plus(5, ChronoUnit.DAYS);
break;
case WEEK:
endDateTime = startDateTime.plus(1, ChronoUnit.WEEKS);
break;
default:
endDateTime = startDateTime.plus(SLOT_TIME, ChronoUnit.MINUTES);
break;
}
Timestamp startTimestamp = Timestamp.valueOf(startDateTime);
Timestamp endTimestamp = Timestamp.valueOf(endDateTime);
return slotRepository.findByTimestampRange(startTimestamp, endTimestamp);
}
示例5: testRangeOfLocalDateTimes
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test(dataProvider = "localDateTimeRanges")
public void testRangeOfLocalDateTimes(LocalDateTime start, LocalDateTime end, Duration step, boolean parallel) {
final Range<LocalDateTime> range = Range.of(start, end, step);
final Array<LocalDateTime> array = range.toArray(parallel);
final boolean ascend = start.isBefore(end);
final int expectedLength = (int)Math.ceil(Math.abs((double)ChronoUnit.SECONDS.between(start, end)) / (double)step.getSeconds());
Assert.assertEquals(array.length(), expectedLength);
Assert.assertEquals(array.typeCode(), ArrayType.LOCAL_DATETIME);
Assert.assertTrue(!array.style().isSparse());
Assert.assertEquals(range.start(), start, "The range start");
Assert.assertEquals(range.end(), end, "The range end");
LocalDateTime expected = null;
for (int i=0; i<array.length(); ++i) {
final LocalDateTime actual = array.getValue(i);
expected = expected == null ? start : ascend ? expected.plus(step) : expected.minus(step);
Assert.assertEquals(actual, expected, "Value matches at " + i);
Assert.assertTrue(ascend ? actual.compareTo(start) >=0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + i);
}
}
示例6: testRangeOfLocalDateTimes
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Test(dataProvider = "LocalDateTimeRanges")
public void testRangeOfLocalDateTimes(LocalDateTime start, LocalDateTime end, Duration step, boolean parallel) {
final boolean ascend = start.isBefore(end);
final Range<LocalDateTime> range = Range.of(start, end, step, v -> v.getHour() == 6);
final Array<LocalDateTime> array = range.toArray(parallel);
final LocalDateTime first = array.first(v -> true).map(ArrayValue::getValue).get();
final LocalDateTime last = array.last(v -> true).map(ArrayValue::getValue).get();
Assert.assertEquals(array.typeCode(), ArrayType.LOCAL_DATETIME);
Assert.assertTrue(!array.style().isSparse());
Assert.assertEquals(range.start(), start, "The range start");
Assert.assertEquals(range.end(), end, "The range end");
int index = 0;
LocalDateTime value = first;
while (ascend ? value.isBefore(last) : value.isAfter(last)) {
final LocalDateTime actual = array.getValue(index);
Assert.assertEquals(actual, value, "Value matches at " + index);
Assert.assertTrue(ascend ? actual.compareTo(start) >= 0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + index);
value = ascend ? value.plus(step) : value.minus(step);
while (value.getHour() == 6) value = ascend ? value.plus(step) : value.minus(step);
index++;
}
}
示例7: mutate
import java.time.LocalDateTime; //导入方法依赖的package包/类
/**
* Creates a new ChronoFrequency which has progressed in the ChronoSeries by one step.
*
* @param chronoSeries time series data to traverse
* @return new ChronoFrequency with latest frequency from traversing time series data
*/
@NotNull
@Override
public ChronoFrequency mutate(@NotNull ChronoSeries chronoSeries) {
if (seriesPosition >= requireNonNull(chronoSeries).getSize()) {
//start from beginning of series
return new ChronoFrequency(chronoUnit, 0, getMinimumFrequency(), getMaximumFrequency(),
chronoSeries.getTimestamp(0));
}
Instant nextTimestamp = chronoSeries.getTimestamp(seriesPosition);
LocalDateTime firstDateTime = getLastOccurrenceTimestamp().atZone(ZoneOffset.UTC).toLocalDateTime();
LocalDateTime secondDateTime = nextTimestamp.atZone(ZoneOffset.UTC).toLocalDateTime();
if (secondDateTime.isBefore(firstDateTime)) {
throw new RuntimeException("first: " + firstDateTime + "; second: " + secondDateTime);
}
//update chrono frequency gene
long frequency = getChronoUnit().between(firstDateTime, secondDateTime);
LocalDateTime addedDateTime = firstDateTime.plus(frequency, getChronoUnit());
if (addedDateTime.isBefore(secondDateTime) && isWithinRange(frequency)) {
frequency++;
} else if (frequency == 0) {
//no point in a frequency of nothing
frequency++;
}
return mutateChronoFrequency(frequency, nextTimestamp);
}
示例8: changeStartAndEndTime
import java.time.LocalDateTime; //导入方法依赖的package包/类
private void changeStartAndEndTime(MouseEvent evt) {
DraggedEntry draggedEntry = dayView.getDraggedEntry();
LocalDateTime locationTime = dayView.getZonedDateTimeAt(evt.getX(), evt.getY()).toLocalDateTime();
LOGGER.fine("changing start/end time, time = " + locationTime //$NON-NLS-1$
+ " offset duration = " + offsetDuration); //$NON-NLS-1$
if (locationTime != null && offsetDuration != null) {
LocalDateTime newStartTime = locationTime.minus(offsetDuration);
newStartTime = grid(newStartTime);
LocalDateTime newEndTime = newStartTime.plus(entryDuration);
LOGGER.fine("new start time = " + newStartTime); //$NON-NLS-1$
LOGGER.fine("new start time (grid) = " + newStartTime); //$NON-NLS-1$
LOGGER.fine("new end time = " + newEndTime); //$NON-NLS-1$
LocalDate startDate = newStartTime.toLocalDate();
LocalTime startTime = newStartTime.toLocalTime();
LocalDate endDate = LocalDateTime.of(startDate, startTime).plus(entryDuration).toLocalDate();
LocalTime endTime = newEndTime.toLocalTime();
LOGGER.finer("new interval: sd = " + startDate + ", st = " + startTime + ", ed = " + endDate + ", et = " + endTime);
draggedEntry.setInterval(startDate, startTime, endDate, endTime);
requestLayout();
}
}
示例9: getReservations
import java.time.LocalDateTime; //导入方法依赖的package包/类
@Override
public Set<Reservation> getReservations(String venue, LocalDate date, LocalTime startTime, Duration duration) {
// Let's start from an implementation of the simplest linear searh algorithm
LocalDateTime startDateTime = date.atTime(startTime);
LocalDateTime endDateTime = startDateTime.plus(duration);
return getVenueReservations(venue).filter(conflictReservation -> {
LocalDateTime conflictStartDateTime = conflictReservation.getDate().atTime(conflictReservation.getStartTime());
LocalDateTime conflictEndDateTime = conflictStartDateTime.plus(conflictReservation.getDuration());
return startDateTime.isEqual(conflictStartDateTime)
|| isInBetween(startDateTime, conflictStartDateTime, conflictEndDateTime)
|| isInBetween(endDateTime, conflictStartDateTime, conflictEndDateTime)
|| isInBetween(conflictStartDateTime, startDateTime, endDateTime)
|| isInBetween(conflictEndDateTime, startDateTime, endDateTime);
}).collect(toSet());
}
示例10: resolveInstantFromString
import java.time.LocalDateTime; //导入方法依赖的package包/类
/**
* Return a future instant from a string formatted #w#d#h#m
* @param string String to resolve from
* @return Instant in the future
*/
public static Instant resolveInstantFromString(String string) {
Matcher matcher = Pattern.compile("\\d+|[wdhmWDHM]+").matcher(string);
Instant now = Instant.now();
LocalDateTime nowLDT = LocalDateTime.ofInstant(now, ZoneId.systemDefault());
int previous = 0;
while(matcher.find()) {
String s = matcher.group().toLowerCase();
if (Util.isInteger(s)) {
previous = Integer.parseInt(s);
continue;
}
switch(s) {
case "w":
nowLDT = nowLDT.plus(previous, ChronoUnit.WEEKS);
break;
case "d":
nowLDT = nowLDT.plus(previous, ChronoUnit.DAYS);
break;
case "h":
nowLDT = nowLDT.plus(previous, ChronoUnit.HOURS);
break;
case "m":
nowLDT = nowLDT.plus(previous, ChronoUnit.MINUTES);
break;
default:
break;
}
}
return nowLDT.atZone(ZoneId.systemDefault()).toInstant();
}
示例11: testLocalDateTime
import java.time.LocalDateTime; //导入方法依赖的package包/类
public static void testLocalDateTime() {
//使用默认时区时钟瞬时时间创建 Clock.systemDefaultZone() -->即相对于 ZoneId.systemDefault()默认时区
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
//自定义时区
LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(now2);//会以相应的时区显示日期
//自定义时钟
Clock clock = Clock.system(ZoneId.of("Asia/Dhaka"));
LocalDateTime now3 = LocalDateTime.now(clock);
System.out.println(now3);//会以相应的时区显示日期
//不需要写什么相对时间 如java.util.Date 年是相对于1900 月是从0开始
//2013-12-31 23:59
LocalDateTime d1 = LocalDateTime.of(2013, 12, 31, 23, 59);
//年月日 时分秒 纳秒
LocalDateTime d2 = LocalDateTime.of(2013, 12, 31, 23, 59, 59, 11);
//使用瞬时时间 + 时区
Instant instant = Instant.now();
LocalDateTime d3 = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
System.out.println(d3);
//解析String--->LocalDateTime
LocalDateTime d4 = LocalDateTime.parse("2013-12-31T23:59");
System.out.println(d4);
LocalDateTime d5 = LocalDateTime.parse("2013-12-31T23:59:59.999");//999毫秒 等价于999000000纳秒
System.out.println(d5);
//使用DateTimeFormatter API 解析 和 格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime d6 = LocalDateTime.parse("2013/12/31 23:59:59", formatter);
System.out.println(formatter.format(d6));
//时间获取
System.out.println(d6.getYear());
System.out.println(d6.getMonth());
System.out.println(d6.getDayOfYear());
System.out.println(d6.getDayOfMonth());
System.out.println(d6.getDayOfWeek());
System.out.println(d6.getHour());
System.out.println(d6.getMinute());
System.out.println(d6.getSecond());
System.out.println(d6.getNano());
//时间增减
LocalDateTime d7 = d6.minusDays(1);
LocalDateTime d8 = d7.plus(1, IsoFields.QUARTER_YEARS);
//LocalDate 即年月日 无时分秒
//LocalTime即时分秒 无年月日
//API和LocalDateTime类似就不演示了
}
示例12: createTickValues
import java.time.LocalDateTime; //导入方法依赖的package包/类
private List<LocalDateTime> createTickValues(final double WIDTH, final LocalDateTime START, final LocalDateTime END) {
List<LocalDateTime> dateList = new ArrayList<>();
LocalDateTime dateTime = LocalDateTime.now();
if (null == START || null == END) return dateList;
// The preferred gap which should be between two tick marks.
double majorTickSpace = 100;
double noOfTicks = WIDTH / majorTickSpace;
List<LocalDateTime> previousDateList = new ArrayList<>();
Interval previousInterval = Interval.values()[0];
// Starting with the greatest interval, add one of each dateTime unit.
for (Interval interval : Interval.values()) {
// Reset the dateTime.
dateTime = LocalDateTime.of(START.toLocalDate(), START.toLocalTime());
// Clear the list.
dateList.clear();
previousDateList.clear();
currentInterval = interval;
// Loop as long we exceeded the END bound.
while(dateTime.isBefore(END)) {
dateList.add(dateTime);
dateTime = dateTime.plus(interval.getAmount(), interval.getInterval());
}
// Then check the size of the list. If it is greater than the amount of ticks, take that list.
if (dateList.size() > noOfTicks) {
dateTime = LocalDateTime.of(START.toLocalDate(), START.toLocalTime());
// Recheck if the previous interval is better suited.
while(dateTime.isBefore(END) || dateTime.isEqual(END)) {
previousDateList.add(dateTime);
dateTime = dateTime.plus(previousInterval.getAmount(), previousInterval.getInterval());
}
break;
}
previousInterval = interval;
}
if (previousDateList.size() - noOfTicks > noOfTicks - dateList.size()) {
dateList = previousDateList;
currentInterval = previousInterval;
}
// At last add the END bound.
dateList.add(END);
List<LocalDateTime> evenDateList = makeDatesEven(dateList, dateTime);
// If there are at least three dates, check if the gap between the START date and the second date is at least half the gap of the second and third date.
// Do the same for the END bound.
// If gaps between dates are to small, remove one of them.
// This can occur, e.g. if the START bound is 25.12.2013 and years are shown. Then the next year shown would be 2014 (01.01.2014) which would be too narrow to 25.12.2013.
if (evenDateList.size() > 2) {
LocalDateTime secondDate = evenDateList.get(1);
LocalDateTime thirdDate = evenDateList.get(2);
LocalDateTime lastDate = evenDateList.get(dateList.size() - 2);
LocalDateTime previousLastDate = evenDateList.get(dateList.size() - 3);
// If the second date is too near by the START bound, remove it.
if (secondDate.toEpochSecond(ZoneOffset.ofHours(0)) - START.toEpochSecond(ZoneOffset.ofHours(0)) < thirdDate.toEpochSecond(ZoneOffset.ofHours(0)) - secondDate.toEpochSecond(ZoneOffset.ofHours(0))) {
evenDateList.remove(secondDate);
}
// If difference from the END bound to the last date is less than the half of the difference of the previous two dates,
// we better remove the last date, as it comes to close to the END bound.
if (END.toEpochSecond(ZoneOffset.ofHours(0)) - lastDate.toEpochSecond(ZoneOffset.ofHours(0)) < ((lastDate.toEpochSecond(ZoneOffset.ofHours(0)) - previousLastDate.toEpochSecond(ZoneOffset.ofHours(0)) * 0.5))) {
evenDateList.remove(lastDate);
}
}
return evenDateList;
}
示例13: adjustTime
import java.time.LocalDateTime; //导入方法依赖的package包/类
/**
* Adjusts the given time either rounding it up or down.
*
* @param time
* the time to adjust
* @param roundUp
* the rounding direction
* @param firstDayOfWeek
* the first day of the week (needed for rounding weeks)
* @return the adjusted time
*/
public LocalDateTime adjustTime(LocalDateTime time, boolean roundUp,
DayOfWeek firstDayOfWeek) {
requireNonNull(time);
if (roundUp) {
time = time.plus(getAmount(), getUnit());
}
return Util.truncate(time, getUnit(), getAmount(), firstDayOfWeek);
}