Search code examples
javareactjsmomentjsepochinstant

Equivalent of moment react in java


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

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.


Solution

  • 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.

    import java.time.Instant;
    
    public class Main {
        public static void main(String[] args) {
            Instant now = Instant.now();
            System.out.println(now);
            System.out.println(now.toEpochMilli());
            System.out.println(now.getEpochSecond());
        }
    }
    

    Output from a sample run:

    2020-10-07T17:16:10.487996Z
    1602090970487
    1602090970