當前位置: 首頁>>代碼示例>>Java>>正文


Java LocalDateTime.plus方法代碼示例

本文整理匯總了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;
}
 
開發者ID:quanticc,項目名稱:sentry,代碼行數:22,代碼來源:TimeFrameService.java

示例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));
    }
}
 
開發者ID:dlemmermann,項目名稱:CalendarFX,代碼行數:33,代碼來源:Entry.java

示例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));
    }
}
 
開發者ID:dlemmermann,項目名稱:CalendarFX,代碼行數:32,代碼來源:Entry.java

示例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);
}
 
開發者ID:ericywl,項目名稱:InfoSys-1D,代碼行數:27,代碼來源:BookingSlotServiceImpl.java

示例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);
    }
}
 
開發者ID:zavtech,項目名稱:morpheus-core,代碼行數:20,代碼來源:RangeBasicTests.java

示例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++;
    }
}
 
開發者ID:zavtech,項目名稱:morpheus-core,代碼行數:23,代碼來源:RangeFilterTests.java

示例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);
}
 
開發者ID:BFergerson,項目名稱:Chronetic,代碼行數:35,代碼來源:ChronoFrequency.java

示例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();
    }
}
 
開發者ID:dlemmermann,項目名稱:CalendarFX,代碼行數:31,代碼來源:DayViewEditController.java

示例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());
}
 
開發者ID:samolisov,項目名稱:bluemix-liberty-microprofile-demo,代碼行數:16,代碼來源:HashMapReservationDao.java

示例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();
}
 
開發者ID:paul-io,項目名稱:momo-2,代碼行數:36,代碼來源:Util.java

示例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類似就不演示了  
    }
 
開發者ID:juebanlin,項目名稱:util4j,代碼行數:47,代碼來源:TimeIntroduction.java

示例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;
}
 
開發者ID:HanSolo,項目名稱:charts,代碼行數:74,代碼來源:Axis.java

示例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);
}
 
開發者ID:dlemmermann,項目名稱:CalendarFX,代碼行數:22,代碼來源:VirtualGrid.java


注:本文中的java.time.LocalDateTime.plus方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。