本文整理匯總了Java中java.time.Instant.truncatedTo方法的典型用法代碼示例。如果您正苦於以下問題:Java Instant.truncatedTo方法的具體用法?Java Instant.truncatedTo怎麽用?Java Instant.truncatedTo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.time.Instant
的用法示例。
在下文中一共展示了Instant.truncatedTo方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: rollIfNeeded
import java.time.Instant; //導入方法依賴的package包/類
/**
* 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);
}
示例2: test_truncatedTo_invalid
import java.time.Instant; //導入方法依賴的package包/類
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
input.truncatedTo(unit);
}
示例3: getDateRange
import java.time.Instant; //導入方法依賴的package包/類
/**
* 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));
}
}