Java Code Examples for java.time.ZonedDateTime#isBefore()

The following examples show how to use java.time.ZonedDateTime#isBefore() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WalkForward.java    From ta4j-origins with MIT License 6 votes vote down vote up
/**
 * Returns a new time series which is a view of a subset of the current series.
 * <p>
 * The new series has begin and end indexes which correspond to the bounds of the sub-set into the full series.<br>
 * The tick of the series are shared between the original time series and the returned one (i.e. no copy).
 * @param series the time series to get a sub-series of
 * @param beginIndex the begin index (inclusive) of the time series
 * @param duration the duration of the time series
 * @return a constrained {@link TimeSeries time series} which is a sub-set of the current series
 */
public static TimeSeries subseries(TimeSeries series, int beginIndex, Duration duration) {

    // Calculating the sub-series interval
    ZonedDateTime beginInterval = series.getTick(beginIndex).getEndTime();
    ZonedDateTime endInterval = beginInterval.plus(duration);

    // Checking ticks belonging to the sub-series (starting at the provided index)
    int subseriesNbTicks = 0;
    int endIndex = series.getEndIndex();
    for (int i = beginIndex; i <= endIndex; i++) {
        // For each tick...
        ZonedDateTime tickTime = series.getTick(i).getEndTime();
        if (tickTime.isBefore(beginInterval) || !tickTime.isBefore(endInterval)) {
            // Tick out of the interval
            break;
        }
        // Tick in the interval
        // --> Incrementing the number of ticks in the subseries
        subseriesNbTicks++;
    }

    return new BaseTimeSeries(series, beginIndex, beginIndex + subseriesNbTicks - 1);
}
 
Example 2
Source File: RangeBasicTests.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "ZonedDateTimeRanges")
public void testRangeOfZonedDateTimes(ZonedDateTime start, ZonedDateTime end, Duration step, boolean parallel) {
    final Range<ZonedDateTime> range = Range.of(start, end, step);
    final Array<ZonedDateTime> 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.ZONED_DATETIME);
    Assert.assertTrue(!array.style().isSparse());
    Assert.assertEquals(range.start(), start, "The range start");
    Assert.assertEquals(range.end(), end, "The range end");
    ZonedDateTime expected = null;
    for (int i=0; i<array.length(); ++i) {
        final ZonedDateTime 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);
    }
}
 
Example 3
Source File: BetweenRoutePredicateFactory.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Override
public Predicate<ServerWebExchange> apply(Config config) {
	Assert.isTrue(config.getDatetime1().isBefore(config.getDatetime2()),
			config.getDatetime1() + " must be before " + config.getDatetime2());

	return new GatewayPredicate() {
		@Override
		public boolean test(ServerWebExchange serverWebExchange) {
			final ZonedDateTime now = ZonedDateTime.now();
			return now.isAfter(config.getDatetime1())
					&& now.isBefore(config.getDatetime2());
		}

		@Override
		public String toString() {
			return String.format("Between: %s and %s", config.getDatetime1(),
					config.getDatetime2());
		}
	};
}
 
Example 4
Source File: RangeFilterTests.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "ZonedDateTimeRanges")
public void testRangeOfZonedDateTimes(ZonedDateTime start, ZonedDateTime end, Duration step, boolean parallel) {
    final boolean ascend = start.isBefore(end);
    final Range<ZonedDateTime> range = Range.of(start, end, step, v -> v.getHour() == 6);
    final Array<ZonedDateTime> array = range.toArray(parallel);
    final ZonedDateTime first = array.first(v -> true).map(ArrayValue::getValue).get();
    final ZonedDateTime last = array.last(v -> true).map(ArrayValue::getValue).get();
    Assert.assertEquals(array.typeCode(), ArrayType.ZONED_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;
    ZonedDateTime value = first;
    while (ascend ? value.isBefore(last) : value.isAfter(last)) {
        final ZonedDateTime 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++;
    }
}
 
Example 5
Source File: DateTimeGroupFunction.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public State calculate(@Nullable Set<Item> items) {
    if (items != null && !items.isEmpty()) {
        ZonedDateTime max = null;
        for (Item item : items) {
            DateTimeType itemState = item.getStateAs(DateTimeType.class);
            if (itemState != null) {
                if (max == null || max.isBefore(itemState.getZonedDateTime())) {
                    max = itemState.getZonedDateTime();
                }
            }
        }
        if (max != null) {
            return new DateTimeType(max);
        }
    }
    return UnDefType.UNDEF;
}
 
Example 6
Source File: JpaAction.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean isMaintenanceWindowAvailable() {
    if (!hasMaintenanceSchedule()) {
        // if there is no defined maintenance schedule, a window is always
        // available.
        return true;
    } else if (isMaintenanceScheduleLapsed()) {
        // if a defined maintenance schedule has lapsed, a window is never
        // available.
        return false;
    } else {
        final ZonedDateTime now = ZonedDateTime.now(ZoneOffset.of(maintenanceWindowTimeZone));
        final Optional<ZonedDateTime> start = getMaintenanceWindowStartTime();
        final Optional<ZonedDateTime> end = getMaintenanceWindowEndTime();

        if (start.isPresent() && end.isPresent()) {
            return now.isAfter(start.get()) && now.isBefore(end.get());
        } else {
            return false;
        }
    }
}
 
Example 7
Source File: WorkflowServiceImpl.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
protected List<DmDependencyTO> getChildrenForRenamedItem(String site, DmDependencyTO renameItem) {
    List<DmDependencyTO> toRet = new ArrayList<>();
    List<DmDependencyTO> children = renameItem.getChildren();
    ZonedDateTime date = renameItem.getScheduledDate();
    if (children != null) {
        Iterator<DmDependencyTO> childItr = children.iterator();
        while (childItr.hasNext()) {
            DmDependencyTO child = childItr.next();
            ZonedDateTime pageDate = child.getScheduledDate();
            if ((date == null && pageDate != null) || (date != null && !date.equals(pageDate))) {
                if (!renameItem.isNow()) {
                    child.setNow(false);
                    if (date != null && (pageDate != null && pageDate.isBefore(date))) {
                        child.setScheduledDate(date);
                    }
                }
                toRet.add(child);
                List<DmDependencyTO> childDeps = child.flattenChildren();
                for (DmDependencyTO childDep : childDeps) {
                    if (objectStateService.isUpdatedOrNew(site, childDep.getUri())) {
                        toRet.add(childDep);
                    }
                }
                child.setReference(false);
                childItr.remove();
            }
        }
    }
    return toRet;
}
 
Example 8
Source File: LinkMinTransactionDatetimeAttributeBin.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public void setKey(GraphReadMethods graph, int attribute, int element) {
    ZonedDateTime currentMin = MAX_DATE_TIME;
    final int transactionCount = graph.getLinkTransactionCount(element);
    for (int t = 0; t < transactionCount; t++) {
        final int transaction = graph.getLinkTransaction(element, t);
        final ZonedDateTime zdt = graph.getObjectValue(attribute, transaction);
        if (zdt.isBefore(currentMin)) {
            currentMin = zdt;
        }
    }
    key = currentMin.equals(MAX_DATE_TIME) ? null : currentMin;
}
 
Example 9
Source File: WalkForward.java    From ta4j-origins with MIT License 5 votes vote down vote up
/**
 * Builds a list of split indexes from splitDuration.
 * @param series the time series to get split begin indexes of
 * @param splitDuration the duration between 2 splits
 * @return a list of begin indexes after split
 */
public static List<Integer> getSplitBeginIndexes(TimeSeries series, Duration splitDuration) {
    ArrayList<Integer> beginIndexes = new ArrayList<>();

    int beginIndex = series.getBeginIndex();
    int endIndex = series.getEndIndex();
    
    // Adding the first begin index
    beginIndexes.add(beginIndex);

    // Building the first interval before next split
    ZonedDateTime beginInterval = series.getFirstTick().getEndTime();
    ZonedDateTime endInterval = beginInterval.plus(splitDuration);

    for (int i = beginIndex; i <= endIndex; i++) {
        // For each tick...
        ZonedDateTime tickTime = series.getTick(i).getEndTime();
        if (tickTime.isBefore(beginInterval) || !tickTime.isBefore(endInterval)) {
            // Tick out of the interval
            if (!endInterval.isAfter(tickTime)) {
                // Tick after the interval
                // --> Adding a new begin index
                beginIndexes.add(i);
            }

            // Building the new interval before next split
            beginInterval = endInterval.isBefore(tickTime) ? tickTime : endInterval;
            endInterval = beginInterval.plus(splitDuration);
        }
    }
    return beginIndexes;
}
 
Example 10
Source File: BeforeRoutePredicateFactory.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public Predicate<ServerWebExchange> apply(Config config) {
	return new GatewayPredicate() {
		@Override
		public boolean test(ServerWebExchange serverWebExchange) {
			final ZonedDateTime now = ZonedDateTime.now();
			return now.isBefore(config.getDatetime());
		}

		@Override
		public String toString() {
			return String.format("Before: %s", config.getDatetime());
		}
	};
}
 
Example 11
Source File: WorkflowServiceImpl.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
protected List<DmDependencyTO> getRefAndChildOfDiffDateFromParent(
    String site, List<DmDependencyTO> submittedItems, boolean removeInPages) throws ServiceLayerException {
    List<DmDependencyTO> childAndReferences = new ArrayList<>();
    for (DmDependencyTO submittedItem : submittedItems) {
        List<DmDependencyTO> children = submittedItem.getChildren();
        ZonedDateTime date = submittedItem.getScheduledDate();
        if (children != null) {
            Iterator<DmDependencyTO> childItr = children.iterator();
            while (childItr.hasNext()) {
                DmDependencyTO child = childItr.next();
                ZonedDateTime pageDate = child.getScheduledDate();
                if ((date == null && pageDate != null) || (date != null && !date.equals(pageDate))) {
                    if (!submittedItem.isNow()) {
                        child.setNow(false);
                        if (date != null && (pageDate != null && pageDate.isBefore(date))) {
                            child.setScheduledDate(date);
                        }
                    }
                    childAndReferences.add(child);
                    List<DmDependencyTO> childDeps = child.flattenChildren();
                    for (DmDependencyTO childDep : childDeps) {
                        if (objectStateService.isUpdatedOrNew(site, childDep.getUri())) {
                            childAndReferences.add(childDep);
                        }
                    }
                    child.setReference(false);
                    childItr.remove();
                    if (removeInPages) {
                        String uri = child.getUri();
                        List<DmDependencyTO> pages = submittedItem.getPages();
                        if (pages != null) {
                            Iterator<DmDependencyTO> pagesIter = pages.iterator();
                            while (pagesIter.hasNext()) {
                                DmDependencyTO page = pagesIter.next();
                                if (page.getUri().equals(uri)) {
                                    pagesIter.remove();
                                }
                            }
                        }
                    }
                }
            }
        }

        List<String> dependenciesPaths = dependencyService.getPublishingDependencies(site, submittedItem.getUri());
        for (String depPath : dependenciesPaths) {
            DmDependencyTO dmDependencyTO = new DmDependencyTO();
            dmDependencyTO.setUri(depPath);
            childAndReferences.add(dmDependencyTO);
        }
    }
    return childAndReferences;
}
 
Example 12
Source File: TvMazeUtils.java    From drftpd with GNU General Public License v2.0 4 votes vote down vote up
private static String calculateAge(ZonedDateTime epDate) {
    // Get now
    ZonedDateTime now = ZonedDateTime.now();

    // Get the (positive) time between now and epData
    ZonedDateTime t1 = now;
    ZonedDateTime t2 = epDate;
    if (epDate.isBefore(now)) {
        t1 = epDate;
        t2 = now;
    }

    String age = "";
    long years = ChronoUnit.YEARS.between(t1, t2);
    if (years > 0) {
        age += years+"y";
        t2 = t2.minusYears(years);
    }

    long months = ChronoUnit.MONTHS.between(t1, t2);
    if (months > 0) {
        age += months+"m";
        t2 = t2.minusMonths(months);
    }

    long weeks = ChronoUnit.WEEKS.between(t1, t2);
    if (weeks > 0) {
        age += weeks+"w";
        t2 = t2.minusWeeks(weeks);
    }

    long days = ChronoUnit.DAYS.between(t1, t2);
    if (days > 0) {
        age += days+"d";
        t2 = t2.minusDays(days);
    }
    boolean spaceadded = false;

    long hours = ChronoUnit.HOURS.between(t1, t2);
    if (hours > 0) {
        age += " "+hours+"h";
        spaceadded = true;
        t2 = t2.minusHours(hours);
    }

    long minutes = ChronoUnit.MINUTES.between(t1, t2);
    if (minutes > 0) {
        if (!spaceadded) { age += " ";  }
        age += minutes+"m";
    }
    if (age.length() == 0) { age = "0"; }

    return age;
}
 
Example 13
Source File: TCKZonedDateTime.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_isBefore_null() {
    ZonedDateTime a = ZonedDateTime.of(2008, 6, 30, 23, 30, 59, 0, ZONE_0100);
    a.isBefore(null);
}
 
Example 14
Source File: TCKZonedDateTime.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_isBefore_null() {
    ZonedDateTime a = ZonedDateTime.of(2008, 6, 30, 23, 30, 59, 0, ZONE_0100);
    a.isBefore(null);
}
 
Example 15
Source File: Tick.java    From ta4j-origins with MIT License 4 votes vote down vote up
/**
 * @param timestamp a timestamp
 * @return true if the provided timestamp is between the begin time and the end time of the current period, false otherwise
 */
default boolean inPeriod(ZonedDateTime timestamp) {
    return timestamp != null
            && !timestamp.isBefore(getBeginTime())
            && timestamp.isBefore(getEndTime());
}
 
Example 16
Source File: TCKZonedDateTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_isBefore_null() {
    ZonedDateTime a = ZonedDateTime.of(2008, 6, 30, 23, 30, 59, 0, ZONE_0100);
    a.isBefore(null);
}
 
Example 17
Source File: TCKZonedDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_isBefore_null() {
    ZonedDateTime a = ZonedDateTime.of(2008, 6, 30, 23, 30, 59, 0, ZONE_0100);
    a.isBefore(null);
}
 
Example 18
Source File: TCKZonedDateTime.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_isBefore_null() {
    ZonedDateTime a = ZonedDateTime.of(2008, 6, 30, 23, 30, 59, 0, ZONE_0100);
    a.isBefore(null);
}
 
Example 19
Source File: TCKZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_isBefore_null() {
    ZonedDateTime a = ZonedDateTime.of(2008, 6, 30, 23, 30, 59, 0, ZONE_0100);
    a.isBefore(null);
}
 
Example 20
Source File: WorkflowServiceImpl.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
protected List<DmDependencyTO> getRefAndChildOfDiffDateFromParent_new(String site,
                                                                      List<DmDependencyTO> submittedItems,
                                                                      boolean removeInPages) {
    List<DmDependencyTO> childAndReferences = new ArrayList<>();
    for (DmDependencyTO submittedItem : submittedItems) {
        List<DmDependencyTO> children = submittedItem.getChildren();
        ZonedDateTime date = submittedItem.getScheduledDate();
        if (children != null) {
            Iterator<DmDependencyTO> childItr = children.iterator();
            while (childItr.hasNext()) {
                DmDependencyTO child = childItr.next();
                ZonedDateTime pageDate = child.getScheduledDate();
                if ((date == null && pageDate != null) || (date != null && !date.equals(pageDate))) {
                    if (!submittedItem.isNow()) {
                        child.setNow(false);
                        if (date != null && (pageDate != null && pageDate.isBefore(date))) {
                            child.setScheduledDate(date);
                        }
                    }
                    childAndReferences.add(child);
                    List<DmDependencyTO> childDeps = child.flattenChildren();
                    for (DmDependencyTO childDep : childDeps) {
                        if (objectStateService.isUpdatedOrNew(site, childDep.getUri())) {
                            childAndReferences.add(childDep);
                        }
                    }
                    child.setReference(false);
                    childItr.remove();
                    if (removeInPages) {
                        String uri = child.getUri();
                        List<DmDependencyTO> pages = submittedItem.getPages();
                        if (pages != null) {
                            Iterator<DmDependencyTO> pagesIter = pages.iterator();
                            while (pagesIter.hasNext()) {
                                DmDependencyTO page = pagesIter.next();
                                if (page.getUri().equals(uri)) {
                                    pagesIter.remove();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return childAndReferences;
}