w3resource logo
Java Programming Exercies

Java DateTime, Calendar Exercises: Get current timestamp

Java DateTime, Calendar : Exercise-20 with Solution

Write a Java program to get current timestamp.

Sample Solution:

Java Code:

import java.time.*;
public class Exercise20 {
   public static void main(String[] args)
    {
    Instant timestamp = Instant.now(); 
    System.out.println("\nCurrent Timestamp: " + timestamp+"\n"); 
    }
}

Sample Output:

Current Timestamp: 2017-06-20T12:33:51.108Z 

N.B.: The result may varry for your system date and time.

Java Code Editor:

import java.time.*;
public class Exercise20 {
   public static void main(String[] args)
    {
    Instant timestamp = Instant.now(); 
    System.out.println("\nCurrent Timestamp: " + timestamp+"\n"); 
    }
}

Improve this sample solution and post your code through Disqus