本文整理汇总了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();
}
示例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();
}
}
示例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();
}
示例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();
}
示例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();
}
示例6: testTimestamp
import java.time.Instant; //导入方法依赖的package包/类
/**
* 获取时间戳
*/
@Test
public void testTimestamp() {
Instant instant = Instant.now();
// 获取毫秒时间, 时间戳
long milli = instant.toEpochMilli();
System.out.println(milli);
}
示例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!");
}
示例8: convert
import java.time.Instant; //导入方法依赖的package包/类
@Override
public Long convert(Instant source) {
return source.toEpochMilli();
}
示例9: convertToDatabaseColumn
import java.time.Instant; //导入方法依赖的package包/类
@Override
public Timestamp convertToDatabaseColumn(Instant instant) {
return instant == null ? null : new Timestamp(instant.toEpochMilli());
}
示例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);
}
示例11: getCode
import java.time.Instant; //导入方法依赖的package包/类
@Override
public final long getCode(Instant value) {
return value == null ? Long.MIN_VALUE : value.toEpochMilli();
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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());
}