// Get the current date and time
LocalDateTime now = LocalDateTime.now();
// Define the format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// Format the current date and time
String formattedNow = now.format(formatter);
// Print the formatted date and time
System.out.println("Current Timestamp:" + formattedNow);
Legacy Answer
Replace
new Timestamp();
with
new java.util.Date()
because there is no default constructor for Timestamp, or you can do it with the method:
new Timestamp(System.currentTimeMillis());
Answer from Jigar Joshi on Stack OverflowMkyong
mkyong.com › home › java › how to get current timestamps in java
How to Get Current Timestamps in Java - Mkyong.com
March 7, 2025 - Use java.sql.Timestamp when working with JDBC and databases. ... Founder of Mkyong.com, passionate Java and open-source technologies.
Videos
01:21
How to create java.sql.Timestamp object? | Java Date and Time - ...
01:13
How to use getTime() and getNanos() methods of java.sql.Timestamp ...
03:34
How Do I Get The Current Timestamp In Java? - Next LVL Programming ...
04:16
Java8 Tutorials : How to get current timestamp in java? - YouTube
01:13
How to format java.sql.Timestamp object? | Java Date and Time - ...
Top answer 1 of 10
284
// Get the current date and time
LocalDateTime now = LocalDateTime.now();
// Define the format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// Format the current date and time
String formattedNow = now.format(formatter);
// Print the formatted date and time
System.out.println("Current Timestamp:" + formattedNow);
Legacy Answer
Replace
new Timestamp();
with
new java.util.Date()
because there is no default constructor for Timestamp, or you can do it with the method:
new Timestamp(System.currentTimeMillis());
2 of 10
242
Use java.util.Date class instead of Timestamp.
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
This will get you the current date in the format specified.
TecAdmin
tecadmin.net › get-current-timestamp-in-java
5 Methods to Get Current TimeStamp in JAVA – TecAdmin
April 26, 2025 - Output: Current Timestamp: 1648628115654 milliseconds Formatted Date-Time: 2023-03-30 14:35:15 · The java.sql.Timestamp class is part of the Java SQL package and represents a date-time value suitable for database storage and retrieval.
Oracle
docs.oracle.com › javase › 8 › docs › api › java › sql › Timestamp.html
Timestamp (Java Platform SE 8 )
October 20, 2025 - Formats a timestamp in JDBC timestamp escape format. yyyy-mm-dd hh:mm:ss.fffffffff, where ffffffffff indicates nanoseconds. ... Gets this Timestamp object's nanos value.
Java67
java67.com › 2016 › 09 › how-to-get-current-timestamp-value-in-java.html
How to get current TimeStamp value in Java? Example | Java67
It's simple, we are just converting a date to the timestamp to get the current timestamp value. Later we are formatting into the required format using the SimpleDateFormat class, one of the utility classes but be careful it's not thread-safe, see here to learn more about using SimpleDateFormat class in Java.
Liberian Geek
liberiangeek.net › home › how-to/tips › how to get a current timestamp in java?
How to Get a Current Timestamp in Java? | Liberian Geek
December 19, 2023 - The output shows that the current process “timestamp” data has been retrieved: The “java.time.Instant” class represents an instantaneous position on the timeline. This position helps in getting the information related to currently running processes. This class offers a “toEpochMilli()” method to convert the retrieved timestamp into milliseconds.
Android Developers
developer.android.com › api reference › timestamp
Timestamp | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Alvin Alexander
alvinalexander.com › java › java-timestamp-example-current-time-now
Java Timestamp example: How to create a “current timestamp” (i.e., now) | alvinalexander.com
August 1, 2024 - You can create a “current time” JDBC Timestamp in just a few lines of code by using the Java Calendar class and a java.util.Date instance. I show this in the three steps of the following example code, where I (a) get a Calendar instance, (b) get a Date from that instance, and then (c) get a Timestamp instance from that Date:
Naukri
naukri.com › code360 › library › java-timestamp
Java Timestamp - Naukri Code 360
October 8, 2024 - Almost there... just a few more seconds
Oracle
docs.oracle.com › javase › 7 › docs › api › java › sql › Timestamp.html
Timestamp (Java Platform SE 7 )
... Returns a hash code value for this object. The result is the exclusive OR of the two halves of the primitive long value returned by the Date.getTime() method. That is, the hash code is the value of the expression: (int)(this.getTime()^(this.getTime() >>> 32)) The hashCode method uses the ...