java.lang.System.currentTimeMillis()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(214)

本文整理了Java中java.lang.System.currentTimeMillis()方法的一些代码示例,展示了System.currentTimeMillis()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。System.currentTimeMillis()方法的具体详情如下:
包路径:java.lang.System
类名称:System
方法名:currentTimeMillis

System.currentTimeMillis介绍

[英]Returns the current time in milliseconds since January 1, 1970 00:00:00.0 UTC.

This method always returns UTC times, regardless of the system's time zone. This is often called "Unix time" or "epoch time". Use a java.text.DateFormat instance to format this time for display to a human.

This method shouldn't be used for measuring timeouts or other elapsed time measurements, as changing the system time can affect the results. Use #nanoTime for that.
[中]返回自1970年1月1日00:00:00.0 UTC以来的当前时间(毫秒)。
无论系统的时区如何,此方法始终返回UTC时间。这通常被称为“Unix时间”或“大纪元时间”。使用java语言。文本DateFormat实例,用于格式化此时间以显示给用户。
此方法不应用于测量超时或其他运行时间测量,因为更改系统时间可能会影响结果。用纳米时间来做这件事。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Start the expiration period for this instance.
 * @param timeToLive the number of seconds before expiration
 */
public void startExpirationPeriod(int timeToLive) {
  this.expirationTime = System.currentTimeMillis() + timeToLive * 1000;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return whether this instance has expired depending on the amount of
 * elapsed time since the call to {@link #startExpirationPeriod}.
 */
public boolean isExpired() {
  return (this.expirationTime != -1 && System.currentTimeMillis() > this.expirationTime);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a new ApplicationEvent.
 * @param source the object on which the event initially occurred (never {@code null})
 */
public ApplicationEvent(Object source) {
  super(source);
  this.timestamp = System.currentTimeMillis();
}

代码示例来源:origin: ReactiveX/RxJava

/**
 * Returns the 'current time' of the Scheduler in the specified time unit.
 * @param unit the time unit
 * @return the 'current time'
 * @since 2.0
 */
public long now(@NonNull TimeUnit unit) {
  return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}

代码示例来源:origin: ReactiveX/RxJava

/**
 * Returns the 'current time' of the Worker in the specified time unit.
 * @param unit the time unit
 * @return the 'current time'
 * @since 2.0
 */
public long now(@NonNull TimeUnit unit) {
  return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}

代码示例来源:origin: square/okhttp

/**
 * Sets the certificate to be valid immediately and until the specified duration has elapsed.
 * The precision of this field is seconds; further precision will be truncated.
 */
public Builder duration(long duration, TimeUnit unit) {
 long now = System.currentTimeMillis();
 return validityInterval(now, now + unit.toMillis(duration));
}

代码示例来源:origin: square/okhttp

/**
 * Attempt to parse a {@code Set-Cookie} HTTP header value {@code setCookie} as a cookie. Returns
 * null if {@code setCookie} is not a well-formed cookie.
 */
public static @Nullable Cookie parse(HttpUrl url, String setCookie) {
 return parse(System.currentTimeMillis(), url, setCookie);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public final synchronized void refresh() {
  logger.debug("Attempting to refresh target");
  this.targetObject = freshTarget();
  this.refreshCount++;
  this.lastRefreshTime = System.currentTimeMillis();
  logger.debug("Target refreshed successfully");
}

代码示例来源:origin: spring-projects/spring-framework

private void updateSessionReadTime(@Nullable String sessionId) {
  if (sessionId != null) {
    SessionInfo info = this.sessions.get(sessionId);
    if (info != null) {
      info.setLastReadTime(System.currentTimeMillis());
    }
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void run() {
    times.add(System.currentTimeMillis());
  }
}, 100, 100, TimeUnit.MILLISECONDS);

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void run() {
    times.add(System.currentTimeMillis());
  }
}, 100, 100, TimeUnit.MILLISECONDS);

代码示例来源:origin: spring-projects/spring-framework

@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
  long initialDelay = startTime.getTime() - System.currentTimeMillis();
  try {
    return this.scheduledExecutor.scheduleAtFixedRate(decorateTask(task, true), initialDelay, period, TimeUnit.MILLISECONDS);
  }
  catch (RejectedExecutionException ex) {
    throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
  }
}

代码示例来源:origin: ReactiveX/RxJava

void sleep() throws Exception {
  cb.await();
  try {
    long before = System.currentTimeMillis();
    Thread.sleep(5000);
    throw new IllegalStateException("Was not interrupted in time?! " + (System.currentTimeMillis() - before));
  } catch (InterruptedException ex) {
    // ignored here
  }
}

代码示例来源:origin: ReactiveX/RxJava

void beforeCancelSleep(BaseTestConsumer<?, ?> ts) throws Exception {
  long before = System.currentTimeMillis();
  Thread.sleep(50);
  if (System.currentTimeMillis() - before > 100) {
    ts.dispose();
    throw new IllegalStateException("Overslept?" + (System.currentTimeMillis() - before));
  }
}

代码示例来源:origin: spring-projects/spring-framework

public WebResponse build() throws IOException {
  WebResponseData webResponseData = webResponseData();
  long endTime = System.currentTimeMillis();
  return new WebResponse(webResponseData, this.webRequest, endTime - this.startTime);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
  ScheduledExecutorService executor = getScheduledExecutor();
  long initialDelay = startTime.getTime() - System.currentTimeMillis();
  try {
    return executor.schedule(errorHandlingTask(task, false), initialDelay, TimeUnit.MILLISECONDS);
  }
  catch (RejectedExecutionException ex) {
    throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
  ScheduledExecutorService executor = getScheduledExecutor();
  long initialDelay = startTime.getTime() - System.currentTimeMillis();
  try {
    return executor.scheduleWithFixedDelay(errorHandlingTask(task, true), initialDelay, delay, TimeUnit.MILLISECONDS);
  }
  catch (RejectedExecutionException ex) {
    throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void subscribe(Subscriber<? super String> t1) {
  t1.onSubscribe(new BooleanSubscription());
  System.out.println(count.get() + " @ " + String.valueOf(last - System.currentTimeMillis()));
  last = System.currentTimeMillis();
  if (count.getAndDecrement() == 0) {
    t1.onNext("hello");
    t1.onComplete();
  } else {
    t1.onError(new RuntimeException());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void subscribe(Observer<? super String> t1) {
  t1.onSubscribe(Disposables.empty());
  System.out.println(count.get() + " @ " + String.valueOf(last - System.currentTimeMillis()));
  last = System.currentTimeMillis();
  if (count.getAndDecrement() == 0) {
    t1.onNext("hello");
    t1.onComplete();
  } else {
    t1.onError(new RuntimeException());
  }
}

代码示例来源:origin: spring-projects/spring-framework

public void testSendAttributeChangeNotificationWhereSourceIsNotTheManagedResource() throws Exception {
  StubSpringModelMBean mbean = new StubSpringModelMBean();
  Notification notification = new AttributeChangeNotification(this, 1872, System.currentTimeMillis(), "Shall we break for some tea?", "agree", "java.lang.Boolean", Boolean.FALSE, Boolean.TRUE);
  ObjectName objectName = createObjectName();
  NotificationPublisher publisher = new ModelMBeanNotificationPublisher(mbean, objectName, mbean);
  publisher.sendNotification(notification);
  assertNotNull(mbean.getActualNotification());
  assertTrue(mbean.getActualNotification() instanceof AttributeChangeNotification);
  assertSame("The exact same Notification is not being passed through from the publisher to the mbean.", notification, mbean.getActualNotification());
  assertSame("The 'source' property of the Notification is *wrongly* being set to the ObjectName of the associated MBean.", this, mbean.getActualNotification().getSource());
}

相关文章