// 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.
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.
Videos
03:34
How Do I Get The Current Timestamp In Java? - Next LVL Programming ...
01:13
How to use getTime() and getNanos() methods of java.sql.Timestamp ...
01:21
How to create java.sql.Timestamp object? | Java Date and Time - ...
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 - ...
Oracle
docs.oracle.com › javase › 8 › docs › api › java › sql › Timestamp.html
Timestamp (Java Platform SE 8 )
3 days ago - 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.
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.
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
Almost there... just a few more seconds
BeginnersBook
beginnersbook.com › 2014 › 01 › how-to-get-current-timestamp-in-java
How to get current timestamp in java
2) Got the current time in milliseconds by calling getTime() method of Date. 3) Created the object of Timtestamp class and passed the milliseconds that we got in step 2, to the constructor of this class during object creation. It constructs the timestamp using the provided milliseconds value. import java.sql.Timestamp; import java.util.Date; public class TimeStampDemo { public static void main( String[] args ) { //Date object Date date= new Date(); //getTime() returns current time in milliseconds long time = date.getTime(); //Passed the milliseconds to constructor of Timestamp class Timestamp ts = new Timestamp(time); ...