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


Java Instant.toEpochMilli方法代码示例

本文整理汇总了Java中java.time.Instant.toEpochMilli方法的典型用法代码示例。如果您正苦于以下问题:Java Instant.toEpochMilli方法的具体用法?Java Instant.toEpochMilli怎么用?Java Instant.toEpochMilli使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.time.Instant的用法示例。


在下文中一共展示了Instant.toEpochMilli方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getTimecodeString

import java.time.Instant; //导入方法依赖的package包/类
public String getTimecodeString() {
    Timecode placeholder;

    if (videoIndex.getTimecode().isPresent()) {             // Check timecode first
        placeholder = videoIndex.getTimecode().get();
    }
    else if (videoIndex.getElapsedTime().isPresent()) {     // If no timecode, try elapsed time
        Duration duration = videoIndex.getElapsedTime().get();
        double frames = duration.toMillis() / 10D; // centi-seconds
        placeholder = new Timecode(frames, 100D);
    }
    else {        // final case create name from date
        Instant timestamp = videoIndex.getTimestamp().get();
        double centisecs = timestamp.toEpochMilli() / 10D;
        placeholder = new Timecode(centisecs, 100D);
    }

    return placeholder.toString();
}
 
开发者ID:mbari-media-management,项目名称:vars-annotation,代码行数:20,代码来源:SnapTime.java

示例2: Event

import java.time.Instant; //导入方法依赖的package包/类
/**
 * @param sid    a controlled human readable and url capable identifier
 * @param origin is a url pointing to the origin of the event
 */
public Event(String sid, URI origin, Instant time, String information, String category) {
    this.time = new Timestamp(time.toEpochMilli());
    this.transientTime = time;
    this.id = sid;
    this.origin = origin.getPath();
    this.information = information;
    this.category = category;
    tId = UUID.randomUUID().toString();
    // The semantic key might as well be generated as a hash value of the event values
    // for simplicity it is just a unique id here. The reason for having a technical
    // id here is the ability to merge og split events according to needs going forward.
    if (noSequence()) {
        sequence = time.toEpochMilli() + time.getNano();
    }
}
 
开发者ID:psd2-in-a-box,项目名称:mid-tier,代码行数:20,代码来源:Event.java

示例3: test_instant

import java.time.Instant; //导入方法依赖的package包/类
public void test_instant() {
    Clock system = Clock.systemUTC();
    assertEquals(system.getZone(), ZoneOffset.UTC);
    for (int i = 0; i < 10000; i++) {
        // assume can eventually get these within 10 milliseconds
        Instant instant = system.instant();
        long systemMillis = System.currentTimeMillis();
        if (systemMillis - instant.toEpochMilli() < 10) {
            return;  // success
        }
    }
    fail();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:TCKClock_System.java

示例4: toMillis

import java.time.Instant; //导入方法依赖的package包/类
private static long toMillis(String date) {
	TemporalAccessor temporalAccessor = DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(date);
	LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
	ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
	Instant instant = Instant.from(zonedDateTime);
	return instant.toEpochMilli();
}
 
开发者ID:DescartesResearch,项目名称:Pet-Supply-Store,代码行数:8,代码来源:TrainingSynchronizer.java

示例5: getRetiringFederationCreationTime

import java.time.Instant; //导入方法依赖的package包/类
public Long getRetiringFederationCreationTime(Object[] args)
{
    logger.trace("getRetiringFederationCreationTime");

    Instant creationTime = bridgeSupport.getRetiringFederationCreationTime();

    if (creationTime == null) {
        // -1 is returned when no retiring federation
        return -1L;
    }

    // Return the creation time in milliseconds from the epoch
    return creationTime.toEpochMilli();
}
 
开发者ID:rsksmart,项目名称:rskj,代码行数:15,代码来源:Bridge.java

示例6: testTimestamp

import java.time.Instant; //导入方法依赖的package包/类
/**
 * 获取时间戳
 */
@Test
public void testTimestamp() {
    Instant instant = Instant.now();

    // 获取毫秒时间, 时间戳
    long milli = instant.toEpochMilli();
    System.out.println(milli);
}
 
开发者ID:cbooy,项目名称:cakes,代码行数:12,代码来源:InstantDemo.java

示例7: main

import java.time.Instant; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
    int N = 10000;
    long t1970 = new java.util.Date(70, 0, 01).getTime();
    Random r = new Random();
    for (int i = 0; i < N; i++) {
        int days  = r.nextInt(50) * 365 + r.nextInt(365);
        long secs = t1970 + days * 86400 + r.nextInt(86400);
        int nanos = r.nextInt(NANOS_PER_SECOND);
        int nanos_ms = nanos / 1000000 * 1000000; // millis precision
        long millis = secs * 1000 + r.nextInt(1000);

        LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
        LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
        Instant inst = Instant.ofEpochSecond(secs, nanos);
        Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
        //System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);

        /////////// Timestamp ////////////////////////////////
        Timestamp ta = new Timestamp(millis);
        ta.setNanos(nanos);
        if (!isEqual(ta.toLocalDateTime(), ta)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ta.toLocalDateTime(), ta);
            throw new RuntimeException("FAILED: j.s.ts -> ldt");
        }
        if (!isEqual(ldt, Timestamp.valueOf(ldt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ldt, Timestamp.valueOf(ldt));
            throw new RuntimeException("FAILED: ldt -> j.s.ts");
        }
        Instant inst0 = ta.toInstant();
        if (ta.getTime() != inst0.toEpochMilli() ||
            ta.getNanos() != inst0.getNano() ||
            !ta.equals(Timestamp.from(inst0))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts");
        }
        inst = Instant.ofEpochSecond(secs, nanos);
        Timestamp ta0 = Timestamp.from(inst);
        if (ta0.getTime() != inst.toEpochMilli() ||
            ta0.getNanos() != inst.getNano() ||
            !inst.equals(ta0.toInstant())) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: instant -> timestamp -> instant");
        }

        ////////// java.sql.Date /////////////////////////////
        // j.s.d/t uses j.u.d.equals() !!!!!!!!
        java.sql.Date jsd = new java.sql.Date(millis);
        if (!isEqual(jsd.toLocalDate(), jsd)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jsd.toLocalDate(), jsd);
            throw new RuntimeException("FAILED: j.s.d -> ld");
        }
        LocalDate ld = ldt.toLocalDate();
        if (!isEqual(ld, java.sql.Date.valueOf(ld))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ld, java.sql.Date.valueOf(ld));
            throw new RuntimeException("FAILED: ld -> j.s.d");
        }
        ////////// java.sql.Time /////////////////////////////
        java.sql.Time jst = new java.sql.Time(millis);
        if (!isEqual(jst.toLocalTime(), jst)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jst.toLocalTime(), jst);
            throw new RuntimeException("FAILED: j.s.t -> lt");
        }
        // millis precision
        LocalTime lt = ldt_ms.toLocalTime();
        if (!isEqual(lt, java.sql.Time.valueOf(lt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(lt, java.sql.Time.valueOf(lt));
            throw new RuntimeException("FAILED: lt -> j.s.t");
        }
    }
    System.out.println("Passed!");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:78,代码来源:JavatimeTest.java

示例8: convert

import java.time.Instant; //导入方法依赖的package包/类
@Override
public Long convert(Instant source) {
	return source.toEpochMilli();
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:5,代码来源:DateTimeConverters.java

示例9: convertToDatabaseColumn

import java.time.Instant; //导入方法依赖的package包/类
@Override
public Timestamp convertToDatabaseColumn(Instant instant) {
    return instant == null ? null : new Timestamp(instant.toEpochMilli());
}
 
开发者ID:chaokunyang,项目名称:amanda,代码行数:5,代码来源:InstantConverter.java

示例10: test_epochMillis

import java.time.Instant; //导入方法依赖的package包/类
@Test(dataProvider="sampleEpochMillis")
public void test_epochMillis(String name, long millis) {
    Instant t1 = Instant.ofEpochMilli(millis);
    long m = t1.toEpochMilli();
    assertEquals(millis, m, name);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:7,代码来源:TestInstant.java

示例11: getCode

import java.time.Instant; //导入方法依赖的package包/类
@Override
public final long getCode(Instant value) {
    return value == null ? Long.MIN_VALUE : value.toEpochMilli();
}
 
开发者ID:zavtech,项目名称:morpheus-core,代码行数:5,代码来源:LongCoding.java

示例12: testMatching

import java.time.Instant; //导入方法依赖的package包/类
private static void testMatching(XMLFormatter formatter,
        LogRecord record,  Instant instant, long expectedNanos,
        boolean useInstant) {

    ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
    int zdtNanos = zdt.getNano();
    assertEquals(expectedNanos, zdtNanos, "ZonedDateTime.getNano()");

    String str = formatter.format(record);

    String match = "."+expectedNanos;
    if (str.contains(match) != useInstant) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string does not contain expected nanos: "
                + "\n\texpected match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    System.out.println("Found expected match for '"+match+"' in \n"+str);

    match = "<millis>"+instant.toEpochMilli()+"</millis>";
    if (!str.contains(match)) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string does not contain expected millis: "
                + "\n\texpected match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    System.out.println("Found expected match for '"+match+"' in \n"+str);

    match = "<nanos>";
    if (str.contains(match) != useInstant) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string "
                + (useInstant
                        ? "does not contain expected nanos: "
                        : "contains unexpected nanos: ")
                + "\n\t" + (useInstant ? "expected" : "unexpected")
                + " match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    match = "<nanos>"+getNanoAdjustment(record)+"</nanos>";
    if (str.contains(match) != useInstant) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string "
                + (useInstant
                        ? "does not contain expected nanos: "
                        : "contains unexpected nanos: ")
                + "\n\t" + (useInstant ? "expected" : "unexpected")
                + " match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    if (useInstant) {
        System.out.println("Found expected match for '"+match+"' in \n"+str);
    } else {
        System.out.println("As expected '"+match+"' is not present in \n"+str);
    }

    match = useInstant ? DateTimeFormatter.ISO_INSTANT.format(instant)
            : zdt.truncatedTo(ChronoUnit.SECONDS)
                    .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    match = "<date>"+match+"</date>";
    if (!str.contains(match)) {
        throw new RuntimeException(formatter.getClass().getSimpleName()
                + ".format()"
                + " string does not contain expected date: "
                + "\n\texpected match for: '" + match + "'"
                + "\n\tin: \n" + str);
    }
    System.out.println("Found expected match for '"+match+"' in \n"+str);

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:75,代码来源:XmlFormatterNanos.java

示例13: from

import java.time.Instant; //导入方法依赖的package包/类
/**
 * Obtains an instance of {@code Date} from an {@code Instant} object.
 * <p>
 * {@code Instant} uses a precision of nanoseconds, whereas {@code Date}
 * uses a precision of milliseconds.  The conversion will trancate any
 * excess precision information as though the amount in nanoseconds was
 * subject to integer division by one million.
 * <p>
 * {@code Instant} can store points on the time-line further in the future
 * and further in the past than {@code Date}. In this scenario, this method
 * will throw an exception.
 *
 * @param instant  the instant to convert
 * @return a {@code Date} representing the same point on the time-line as
 *  the provided instant
 * @exception NullPointerException if {@code instant} is null.
 * @exception IllegalArgumentException if the instant is too large to
 *  represent as a {@code Date}
 * @since 1.8
 */
public static Date from(Instant instant) {
    try {
        return new Date(instant.toEpochMilli());
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException(ex);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:28,代码来源:Date.java

示例14: from

import java.time.Instant; //导入方法依赖的package包/类
/**
 * Obtains an instance of {@code Date} from an {@code Instant} object.
 * <p>
 * {@code Instant} uses a precision of nanoseconds, whereas {@code Date}
 * uses a precision of milliseconds.  The conversion will truncate any
 * excess precision information as though the amount in nanoseconds was
 * subject to integer division by one million.
 * <p>
 * {@code Instant} can store points on the time-line further in the future
 * and further in the past than {@code Date}. In this scenario, this method
 * will throw an exception.
 *
 * @param instant  the instant to convert
 * @return a {@code Date} representing the same point on the time-line as
 *  the provided instant
 * @exception NullPointerException if {@code instant} is null.
 * @exception IllegalArgumentException if the instant is too large to
 *  represent as a {@code Date}
 * @since 1.8
 */
public static Date from(Instant instant) {
    try {
        return new Date(instant.toEpochMilli());
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException(ex);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:Date.java

示例15: dateTimeOf

import java.time.Instant; //导入方法依赖的package包/类
/**
 * Gets DateTime for Instant.
 *
 * @param time Time object to be converted.
 * @return DateTime representing time
 */
public static DateTime dateTimeOf(final Instant time) {
    return new DateTime(time.toEpochMilli());
}
 
开发者ID:mrluo735,项目名称:cas-5.1.0,代码行数:10,代码来源:DateTimeUtils.java


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