本文整理汇总了Java中java.time.Instant.getNano方法的典型用法代码示例。如果您正苦于以下问题:Java Instant.getNano方法的具体用法?Java Instant.getNano怎么用?Java Instant.getNano使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.Instant
的用法示例。
在下文中一共展示了Instant.getNano方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
}
示例2: convert
import java.time.Instant; //导入方法依赖的package包/类
@Override
public Long convert(final Instant value) {
return value.getEpochSecond() * NANOS_PER_SECOND + value.getNano();
}
示例3: previousTransition
import java.time.Instant; //导入方法依赖的package包/类
/**
* Gets the previous transition before the specified instant.
* <p>
* This returns details of the previous transition after the specified instant.
* For example, if the instant represents a point where "summer" daylight saving time
* applies, then the method will return the transition from the previous "winter" time.
*
* @param instant the instant to get the previous transition after, not null, but null
* may be ignored if the rules have a single offset for all instants
* @return the previous transition after the specified instant, null if this is before the first transition
*/
public ZoneOffsetTransition previousTransition(Instant instant) {
if (savingsInstantTransitions.length == 0) {
return null;
}
long epochSec = instant.getEpochSecond();
if (instant.getNano() > 0 && epochSec < Long.MAX_VALUE) {
epochSec += 1; // allow rest of method to only use seconds
}
// check if using last rules
long lastHistoric = savingsInstantTransitions[savingsInstantTransitions.length - 1];
if (lastRules.length > 0 && epochSec > lastHistoric) {
// search year the instant is in
ZoneOffset lastHistoricOffset = wallOffsets[wallOffsets.length - 1];
int year = findYear(epochSec, lastHistoricOffset);
ZoneOffsetTransition[] transArray = findTransitionArray(year);
for (int i = transArray.length - 1; i >= 0; i--) {
if (epochSec > transArray[i].toEpochSecond()) {
return transArray[i];
}
}
// use last from preceding year
int lastHistoricYear = findYear(lastHistoric, lastHistoricOffset);
if (--year > lastHistoricYear) {
transArray = findTransitionArray(year);
return transArray[transArray.length - 1];
}
// drop through
}
// using historic rules
int index = Arrays.binarySearch(savingsInstantTransitions, epochSec);
if (index < 0) {
index = -index - 1;
}
if (index <= 0) {
return null;
}
return new ZoneOffsetTransition(savingsInstantTransitions[index - 1], wallOffsets[index - 1], wallOffsets[index]);
}
示例4: 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!");
}
示例5: previousTransition
import java.time.Instant; //导入方法依赖的package包/类
/**
* Gets the previous transition before the specified instant.
* <p>
* This returns details of the previous transition before the specified instant.
* For example, if the instant represents a point where "summer" daylight saving time
* applies, then the method will return the transition from the previous "winter" time.
*
* @param instant the instant to get the previous transition after, not null, but null
* may be ignored if the rules have a single offset for all instants
* @return the previous transition before the specified instant, null if this is before the first transition
*/
public ZoneOffsetTransition previousTransition(Instant instant) {
if (savingsInstantTransitions.length == 0) {
return null;
}
long epochSec = instant.getEpochSecond();
if (instant.getNano() > 0 && epochSec < Long.MAX_VALUE) {
epochSec += 1; // allow rest of method to only use seconds
}
// check if using last rules
long lastHistoric = savingsInstantTransitions[savingsInstantTransitions.length - 1];
if (lastRules.length > 0 && epochSec > lastHistoric) {
// search year the instant is in
ZoneOffset lastHistoricOffset = wallOffsets[wallOffsets.length - 1];
int year = findYear(epochSec, lastHistoricOffset);
ZoneOffsetTransition[] transArray = findTransitionArray(year);
for (int i = transArray.length - 1; i >= 0; i--) {
if (epochSec > transArray[i].toEpochSecond()) {
return transArray[i];
}
}
// use last from preceding year
int lastHistoricYear = findYear(lastHistoric, lastHistoricOffset);
if (--year > lastHistoricYear) {
transArray = findTransitionArray(year);
return transArray[transArray.length - 1];
}
// drop through
}
// using historic rules
int index = Arrays.binarySearch(savingsInstantTransitions, epochSec);
if (index < 0) {
index = -index - 1;
}
if (index <= 0) {
return null;
}
return new ZoneOffsetTransition(savingsInstantTransitions[index - 1], wallOffsets[index - 1], wallOffsets[index]);
}
示例6: KerberosTime
import java.time.Instant; //导入方法依赖的package包/类
/**
* Creates a KerberosTime object from an Instant object
*/
public KerberosTime(Instant instant) {
this(instant.getEpochSecond()*1000 + instant.getNano()/1000000L,
instant.getNano()/1000%1000);
}
示例7: formatTime
import java.time.Instant; //导入方法依赖的package包/类
private static String formatTime(String prefix, Instant time) {
return prefix + ": " + time + " - seconds: "
+ time.getEpochSecond() + ", nanos: "
+ time.getNano();
}
示例8: testWithOffset
import java.time.Instant; //导入方法依赖的package包/类
static void testWithOffset(String name, long offset, Clock clock)
throws IllegalAccessException {
offsetField.set(clock, offset);
long beforeMillis = System.currentTimeMillis();
final Instant instant = clock.instant();
long afterMillis = System.currentTimeMillis();
long actualOffset = offsetField.getLong(clock);
long instantMillis = instant.getEpochSecond() * MILLIS_IN_SECOND
+ instant.getNano() / NANOS_IN_MILLI;
if (instantMillis < beforeMillis || instantMillis > afterMillis) {
throw new RuntimeException(name
+ ": Invalid instant: " + instant
+ " (~" + instantMillis + "ms)"
+ " when time in millis is in ["
+ beforeMillis + ", " + afterMillis
+ "] and offset in seconds is " + offset);
}
Answer isOffLimits = isOffLimits(beforeMillis / MILLIS_IN_SECOND,
afterMillis / MILLIS_IN_SECOND, offset);
switch (isOffLimits) {
case YES:
if (actualOffset == offset) {
throw new RuntimeException(name
+ ": offset was offlimit but was not recomputed "
+ " when time in millis is in ["
+ beforeMillis + ", " + afterMillis
+ "] and offset in seconds was " + offset);
}
break;
case NO:
if (actualOffset != offset) {
throw new RuntimeException(name
+ ": offset was not offlimit but was recomputed.");
}
break;
default:
break;
}
if (distance(actualOffset, instant.getEpochSecond()) >= MAX_OFFSET) {
throw new RuntimeException(name + ": Actual offset is too far off:"
+ " offset=" + actualOffset
+ "instant.seconds=" + instant.getEpochSecond());
}
long adjustment = (instant.getEpochSecond() - actualOffset) * NANOS_IN_SECOND
+ instant.getNano();
validateAdjustment(name, actualOffset, beforeMillis, afterMillis, adjustment);
}
示例9: from
import java.time.Instant; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code Timestamp} from an {@link Instant} object.
* <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 an {@code Timestamp} 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 Timesamp}
* @since 1.8
*/
public static Timestamp from(Instant instant) {
try {
Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
stamp.nanos = instant.getNano();
return stamp;
} catch (ArithmeticException ex) {
throw new IllegalArgumentException(ex);
}
}
示例10: from
import java.time.Instant; //导入方法依赖的package包/类
/**
* Obtains an instance of {@code Timestamp} from an {@link Instant} object.
* <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 an {@code Timestamp} 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 Timestamp}
* @since 1.8
*/
public static Timestamp from(Instant instant) {
try {
Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
stamp.nanos = instant.getNano();
return stamp;
} catch (ArithmeticException ex) {
throw new IllegalArgumentException(ex);
}
}