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

The following are Jave code examples for showing how to use truncatedTo() 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: swage   File: RollingFileWriter.java   Source Code and License Vote up 5 votes
/**
 * If necessary, close the current logfile and open a new one,
 * updating {@link #writer}.
 */
private void rollIfNeeded() throws IOException {
    // Do not roll unless record is complete, as indicated by flush
    if (!flushed) return;
    flushed = false;

    // If we have not yet passed the roll over mark do nothing
    Instant now = Instant.now();
    if (now.isBefore(rollAt)) {
        return;
    }

    // New file time, may not be the rollAt time if one or more intervals
    // have passed without anything being written
    Instant rollTime = now.truncatedTo(rollInterval);

    // Determine the name of the file that will be written to
    String name = this.baseName + format.format(LocalDateTime.ofInstant(rollTime, timeZone));
    this.outPath = this.directory.resolve(name);

    // Finish writing to previous log
    if (writer != null) {
        writer.flush();
        writer.close();
    }

    // Ensure the parent directory always exists, even if it was removed out from under us.
    // A no-op if the directory already exists.
    Files.createDirectories(outPath.getParent());

    // Create new file, set it as our write destination
    writer = Files.newBufferedWriter(
                outPath,
                StandardCharsets.UTF_8,
                StandardOpenOption.CREATE,
                StandardOpenOption.APPEND);

    // Point to the next time we want to roll the log, update rollAt
    this.rollAt = rollTime.plus(1, rollInterval);
}
 
Example 2
Project: jdk8u-jdk   File: TCKInstant.java   Source Code and License Vote up 4 votes
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 3
Project: openjdk-jdk10   File: TCKInstant.java   Source Code and License Vote up 4 votes
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
Example 4
Project: smarti   File: DateUtils.java   Source Code and License Vote up 3 votes
/**
 * Returns the earliest/latest {@link Date} based on the {@link DateValue#getDate()}
 * and {@link DateValue#getGrain()}.
 * @param dateValue the date value
 * @return the earliest possible start in {@link Pair#getLeft()} and the latest
 * possible end in {@link Pair#getRight()}. If the parsed {@link DateValue} does
 * not have a {@link DateValue#getGrain()} the left and right date will be the
 * value of {@link DateValue#getDate()}
 * @throws NullPointerException if the parsed {@link DateValue} or
 * {@link DateValue#getDate()} are <code>null</code>
 */
public static Pair<Date, Date> getDateRange(DateValue dateValue){
    if(dateValue.getGrain() == null){
        return new ImmutablePair<>(dateValue.getDate(), dateValue.getDate());
    } else {
        Instant inst = dateValue.getDate().toInstant();
        Instant startInst = inst.truncatedTo(dateValue.getGrain().getChronoUnit());
        Instant endInst = startInst.plus(1,dateValue.getGrain().getChronoUnit());
        return new ImmutablePair<>(Date.from(startInst), Date.from(endInst));
    }
}