Java Code Examples for java.time.Instant.atZone()

The following are Jave code examples for showing how to use atZone() of the java.time.Instant class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: SensorThingsProcessor   File: ProcessorBatchAggregate.java   Source Code and License Vote up 6 votes
public List<Interval> calculateIntervalsForTime(TimeObject phenTime) {
    List<Interval> retval = new ArrayList<>();
    Instant phenTimeStart = getPhenTimeStart(phenTime);
    Instant phenTimeEnd = getPhenTimeEnd(phenTime);

    ZonedDateTime atZone = phenTimeStart.atZone(getZoneId());
    ZonedDateTime intStart = level.toIntervalStart(atZone);
    ZonedDateTime intEnd = intStart.plus(level.amount, level.unit);

    retval.add(Interval.of(intStart.toInstant(), intEnd.toInstant()));
    while (intEnd.toInstant().isBefore(phenTimeEnd)) {
        intStart = intEnd;
        intEnd = intStart.plus(level.amount, level.unit);
        retval.add(Interval.of(intStart.toInstant(), intEnd.toInstant()));
    }
    return retval;
}
 
Example 2
Project: pyplyn   File: FormatUtils.java   Source Code and License Vote up 6 votes
/**
 * Parses string as time
 *   accepted formats are milliseconds from epoch and valid date string
 *
 * @throws DateTimeParseException if the value cannot be parsed as valid datetime
 */
public static ZonedDateTime parseUTCTime(String value) {
    try {
        // parses ms unix time and returns at UTC offset
        Instant instant = Instant.ofEpochMilli(Long.parseLong(value));
        return instant.atZone(ZoneOffset.UTC);

    } catch (NumberFormatException e) {
        // parse passed date
        return Optional.of(ZonedDateTime.parse(value))
                // convert to UTC
                .map(zdt -> zdt.withZoneSameInstant(ZoneOffset.UTC))
                // and return the value; this is safe to call without orElse,
                //   since ZonedDateTime will throw an exception if it cannot parse the value
                .get();
    }
}
 
Example 3
Project: trading4j   File: D1.java   Source Code and License Vote up 6 votes
@Override
public Instant instantOfNextFrame(final Instant instant) {
    final ZonedDateTime britishTime = instant.atZone(BRITISH_TIME_ZONE);
    final DayOfWeek britishDay = britishTime.getDayOfWeek();
    if (britishDay != SUNDAY) {
        return removeMinutesAndLess(britishTime).withHour(0).plus(1, DAYS).toInstant();
    }

    final ZonedDateTime britishTimeCorrectTimeValues = removeMinutesAndLess(britishTime).withHour(22);
    if (britishTime.getDayOfWeek() == SUNDAY) {
        if (britishTime.isBefore(britishTimeCorrectTimeValues)) {
            return britishTimeCorrectTimeValues.toInstant();
        }
        return removeMinutesAndLess(britishTime).withHour(0).plus(2, DAYS).toInstant();
    }
    return britishTimeCorrectTimeValues.with(TemporalAdjusters.next(SUNDAY)).toInstant();
}
 
Example 4
Project: jdk8u-jdk   File: TCKInstant.java   Source Code and License Vote up 5 votes
@Test
public void test_atZone() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        ZonedDateTime test = instant.atZone(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 5
Project: openjdk-jdk10   File: TCKInstant.java   Source Code and License Vote up 5 votes
@Test
public void test_atZone() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        ZonedDateTime test = instant.atZone(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 6
Project: trading4j   File: D1.java   Source Code and License Vote up 5 votes
@Override
public boolean areInSameTimeFrame(final Instant instant1, final Instant instant2) {

    final ZonedDateTime britishTime = instant1.atZone(BRITISH_TIME_ZONE);
    final DayOfWeek britishDay = britishTime.getDayOfWeek();

    final ZonedDateTime localBase = britishTime.withHour(0).withMinute(0).withSecond(0).withNano(0);

    final ZonedDateTime start;
    final ZonedDateTime end;

    if (britishDay == SUNDAY) {
        if (britishTime.isBefore(localBase.withHour(22))) {
            start = localBase;
            end = localBase.withHour(22);
        } else {
            start = localBase.withHour(22);
            end = localBase.plusDays(2);
        }
    } else if (britishDay == MONDAY) {
        start = localBase.minusDays(1).withHour(22);
        end = localBase.plusDays(1);
    } else {
        start = localBase;
        end = localBase.plusDays(1);
    }

    return instant2.equals(start.toInstant())
            || (instant2.isAfter(start.toInstant()) && instant2.isBefore(end.toInstant()));
}
 
Example 7
Project: SensorThingsProcessor   File: ProcessorBatchAggregate.java   Source Code and License Vote up 4 votes
private void calculateAggregates(AggregateCombo combo) throws ServiceFailureException, ProcessException {
    Observation lastAggObs = combo.getLastForTarget();

    Instant calcIntervalStart;
    if (lastAggObs == null) {
        Observation firstSourceObs = combo.getFirstForSource();
        if (firstSourceObs == null) {
            LOGGER.debug("No source observations at all for {}.", combo);
            return;
        }
        Instant firstSourceStart = getPhenTimeStart(firstSourceObs);

        ZonedDateTime atZone = firstSourceStart.atZone(combo.getZoneId());
        ZonedDateTime firstIntStart = combo.level.toIntervalStart(atZone);
        if (atZone.isEqual(firstIntStart)) {
            calcIntervalStart = firstIntStart.toInstant();
        } else {
            calcIntervalStart = firstIntStart.plus(combo.level.duration).toInstant();
        }

    } else {
        TimeObject lastAggPhenTime = lastAggObs.getPhenomenonTime();
        calcIntervalStart = lastAggPhenTime.getAsInterval().getEnd();
    }
    Observation lastSourceObs = combo.getLastForSource();
    if (lastSourceObs == null) {
        LOGGER.debug("No source observations at all for {}.", combo);
        return;
    }
    Instant lastSourcePhenTime = getPhenTimeEnd(lastSourceObs);

    boolean more = true;
    while (more) {
        Instant calcIntervalEnd = calcIntervalStart.plus(combo.level.duration);

        if (lastSourcePhenTime.isBefore(calcIntervalEnd)) {
            LOGGER.info("Nothing (more) to do for {}.", combo);
            return;
        }

        calculateAggregate(combo, Interval.of(calcIntervalStart, calcIntervalEnd));
        calcIntervalStart = calcIntervalEnd;
    }

}
 
Example 8
Project: flow-platform   File: DateUtil.java   Source Code and License Vote up 4 votes
public static ZonedDateTime fromDateForUTC(Date date) {
    Instant instant = date.toInstant();
    return instant.atZone(ZONE_UTC);
}