Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created May 14, 2021 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravkukade/a965f1a078d382ff7366e7ba6d13561c to your computer and use it in GitHub Desktop.
Save gauravkukade/a965f1a078d382ff7366e7ba6d13561c to your computer and use it in GitHub Desktop.
A Java program to get the current timestamp using java.sql.Timestamp Check the blogpost at https://coderolls.com/how-to-get-current-timestamps-in-java/
import java.sql.Timestamp;
import java.util.Date;
/**
* A Java program to get the current timestamp using java.sql.Timestamp
*
* @author Gaurav Kukade at coderolls.com
*
*/
public class TimestampExample {
public static void main(String[] args) {
//using system time in milliseconds
Timestamp timestampFromSystemTime = new Timestamp(System.currentTimeMillis());
System.out.println("1. timestampFromSystemTime: "+timestampFromSystemTime); // 2021-05-13 20:55:44.179
//using java.util.Date
Date date = new Date();
Timestamp timestampFromDateObject = new Timestamp(date.getTime());
System.out.println("2. timestampFromDateObject: " +timestampFromDateObject); // 2021-05-13 20:55:44.187
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment