// 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 Overflow
🌐
Mkyong
mkyong.com › home › java › how to get current timestamps in java
How to Get Current Timestamps in Java - Mkyong.com
March 7, 2025 - This article shows few Java examples to get the current date time or timestamp in Java. (Updated with Java 8).
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › sql › Timestamp.html
Timestamp (Java Platform SE 8 )
October 20, 2025 - 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale of the given Timestamp, its fractional seconds precision. Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the ...
🌐
Medium
medium.com › dev-java › java-timestamps-77a006356a10
Java Timestamps. Java's handling of date and time…
April 24, 2025 - Timestamp added better precision ... general-purpose date-time logic. import java.sql.Timestamp; Timestamp timestamp = new Timestamp(System.currentTimeMillis());...
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Variables Print Variables Multiple Variables Identifiers Constants (Final) Real-Life Examples Code Challenge Java Data Types
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-date-to-timestamp
Java Program to Convert Date to TimeStamp - GeeksforGeeks
July 23, 2025 - ... // Java Program to convert date to time stamp import java.sql.Timestamp; import java.util.Date; public class GFG_Article { public static void main(String args[]) { // getting the system date Date date = new Date(); // getting the object of the Timestamp class Timestamp ts = new Timestamp(date.getTime()); ...
🌐
Dariawan
dariawan.com › tutorials › java › java-sql-timestamp-examples
java.sql.Timestamp Examples | Dariawan
July 26, 2019 - static Timestamp valueOf​(LocalDateTime dateTime): Obtains an instance of Timestamp from a LocalDateTime object, with the same year, month, day of month, hours, minutes, seconds and nanos date-time value as the provided LocalDateTime.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.sql › java › sql › Timestamp.html
Timestamp (Java SE 17 & JDK 17)
January 20, 2026 - 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale of the given Timestamp, its fractional seconds precision. Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › convert between string and timestamp
Convert Between String and Timestamp | Baeldung
January 8, 2024 - Learn how to convert between String and Timestamp with a little help from LocalDateTime and Java 8.
🌐
Google
docs.cloud.google.com › java › client libraries › class timestamp (2.65.0)
Class Timestamp (2.65.0) | Java client libraries | Google Cloud Documentation
December 17, 2025 - Input string should be in the RFC 3339 format, like '2020-12-01T10:15:30.000Z' or with the timezone offset, such as '2020-12-01T10:15:30+01:00'. ... Returns the fractional seconds component, in nanoseconds.
🌐
How to do in Java
howtodoinjava.com › home › java date time › get current timestamp in java
Get Current Timestamp in Java
April 4, 2023 - In Java, timestamps should be represented with java.time.Instant from Java 8, and java.sql.Timestamp till Java 7.
🌐
Scaler
scaler.com › home › topics › java timestamp
Java Timestamp - Scaler Topics
November 19, 2022 - Explanation: In this example, we will first create two timestamp objects and use the getTime function to fetch the timestamp object time and initialize it to the before the default value of time which is Jan 1, 1970, which the negative value shows.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.sql › java › sql › Timestamp.html
Timestamp (Java SE 11 & JDK 11 )
January 20, 2026 - 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale of the given Timestamp, its fractional seconds precision. Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.sql.timestamp
Timestamp Class (Java.Sql) | Microsoft Learn
The precision of a Timestamp object is calculated to be either: <ul> <li>19 , which is the number of characters in yyyy-mm-dd hh:mm:ss <li> 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale ...
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › sql › Timestamp.html
Timestamp (Java Platform SE 6)
20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale of the given Timestamp, its fractional seconds precision. Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the ...
🌐
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 ...
🌐
Medium
chintanjoshi1.medium.com › getting-timestamps-for-specific-dates-and-times-in-java-6f6305528bb5
Getting Timestamps for Specific Dates and Times in Java | by Chintan Joshi | Medium
March 17, 2023 - For example, you could represent a duration of 30 minutes as a timestamp value of 30 * 60 * 1000 (i.e. 1,800,000 milliseconds). In this article, we’ve seen how to get timestamps for specific dates and times in Java using the Calendar ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › sql › Timestamp.html
Timestamp (Java Platform SE 7 )
20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale of the given Timestamp, its fractional seconds precision. Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the ...
🌐
Apache Commons
commons.apache.org › proper › commons-net › jacoco › org.apache.commons.net.ntp › TimeStamp.java.html
TimeStamp.java - Apache Commons
* @see System#currentTimeMillis() */ public static TimeStamp getCurrentTime() { return getNtpTime(System.currentTimeMillis()); } // initialization of static time bases /* * static { TimeZone utcZone = TimeZone.getTimeZone("UTC"); Calendar calendar = Calendar.getInstance(utcZone); calendar.set(1900, ...