Created
May 14, 2021 18:34
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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