在Java中的等价物,类似于React中的moment。

huangapple go评论119阅读模式
英文:

Equivalent of moment react in java

问题

我必须找到开始加载时间和完成加载时间事件之间的差异。开始加载时间在Java中,而结束时间在React中。

在React中,我有这样的代码:

  1. completeLoad = moment();

我想要记录Java中的初始加载时间,然后找到两者之间的差异。我不确定如何在Java中以类似 moment() 的格式记录时间。在这方面需要一些帮助。

英文:

I have to find the difference between the start and the complete load time event. The start load is in java whereas end is in react.

In react I have this

  1. completeLoad = moment();

I want to record the initial load time in java and then find the difference between the two. I am not sure how to record time in java in similar format as momemt(). Need some help here.

答案1

得分: 3

在Java中,您可以使用Instant来表示时间线上的单个瞬时点。Instant#now可以从系统时钟获取当前的瞬时点。

  1. import java.time.Instant;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Instant now = Instant.now();
  5. System.out.println(now);
  6. System.out.println(now.toEpochMilli());
  7. System.out.println(now.getEpochSecond());
  8. }
  9. }

示例运行的输出:

  1. 2020-10-07T17:16:10.487996Z
  2. 1602090970487
  3. 1602090970
英文:

In Java, you have Instant which models a single instantaneous point on the time-line. Instant#now gives you the current instant from the system clock.

  1. import java.time.Instant;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Instant now = Instant.now();
  5. System.out.println(now);
  6. System.out.println(now.toEpochMilli());
  7. System.out.println(now.getEpochSecond());
  8. }
  9. }

Output from a sample run:

  1. 2020-10-07T17:16:10.487996Z
  2. 1602090970487
  3. 1602090970

huangapple
  • 本文由 发表于 2020年10月8日 01:07:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64248948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定