当前位置: 首页>>代码示例>>Java>>正文


Java Instant.truncatedTo方法代码示例

本文整理汇总了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);
}
 
开发者ID:awslabs,项目名称:swage,代码行数:44,代码来源:RollingFileWriter.java

示例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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:5,代码来源:TCKInstant.java

示例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));
    }
}
 
开发者ID:redlink-gmbh,项目名称:smarti,代码行数:22,代码来源:DateUtils.java


注:本文中的java.time.Instant.truncatedTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。