Get a timestamp in Java

In this source code example, we show you how to get a timestamp in Java.

Get a time stamp in Java

package com.ramesh.java8.datetime;

import java.time.Instant;

/**
 * Useful Java8DateUtiliy Methods
 * @author javaguides.net
 *
 */

public final class Java8DateUtility {
    /**
     * get time stamp.
     * @return
     */
    public static Instant getTimeStamp() {
        Instant timestamp = Instant.now();
        System.out.println("What is value of this instant " + timestamp);
        return timestamp;
    }
}

JUnit test case

package com.ramesh.java8.datetime;

import org.junit.Test;

/**
 * JUnit test cases for Java8DateUtiliy Methods
 * @author javaguides.net
 *
 */
public class Java8DateUtilityTest {

    @Test
    public void getTimeStamp() {
        System.out.println("get time stamp ::" + Java8DateUtility.getTimeStamp());
    }
}
Run the JUnit test cases will print the output:
get time stamp ::2018-07-21T13:10:51.642Z


Comments