// 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 - Use java.sql.Timestamp when working with JDBC and databases. ... Founder of Mkyong.com, passionate Java and open-source technologies.
🌐
Baeldung
baeldung.com › home › java › java dates › get the current date and time in java
Get the Current Date and Time in Java | Baeldung
January 8, 2024 - Use java.time.Instant to get a time stamp from the Java epoch. According to the JavaDoc, “epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z, where instants after the epoch have positive values: Instant instant ...
🌐
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.
🌐
How to do in Java
howtodoinjava.com › home › java date time › get current timestamp in java
Get Current Timestamp in Java
April 4, 2023 - Timestamp timestamp1 = new Timestamp(System.currentTimeMillis()); Date date = new Date(); Timestamp timestamp2 = new Timestamp(date.getTime()); System.out.println(timestamp1); //2022-02-15 13:55:56.18 System.out.println(timestamp2); //2022-02-15 ...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-date-to-timestamp
Java Program to Convert Date to TimeStamp - GeeksforGeeks
July 23, 2025 - Create an object of the Timestamp class and pass the value returned by the getTime() method. Finally, print this Timestamp object value. Example: Java ·
Find elsewhere
🌐
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.
🌐
JavaBeat
javabeat.net › home › how to get a current timestamp in java?
How to Get a Current Timestamp in Java?
May 6, 2024 - We can invoke the constructor of ... args) { //Getting the Current DateTime Date currentTS = new Date(); System.out.println("The Current Timestamp == " + currentTS); //Converting Current Timestamp in Milliseconds ...
🌐
Attacomsian
attacomsian.com › blog › java-get-unix-timestamp
How to get the Unix timestamp in Java
October 6, 2022 - To convert a Unix timestamp back ... using a universal timezone (UTC). In Java 7 and below, you can use the System.currentTimeMillis() method to get the current time in 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 · 中文 – 简体
🌐
Medium
medium.com › @AlexanderObregon › javas-instant-now-method-explained-5403bac7ec1e
Java’s Instant.now() Method Explained | Medium
September 13, 2024 - When Instant.now() is called, it returns the current timestamp in UTC (Coordinated Universal Time). This timestamp is represented as an Instant object, which stores the time as seconds and nanoseconds from the standard Java epoch (1970-01-0...
🌐
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:
🌐
w3resource
w3resource.com › java-exercises › datetime › java-datetime-exercise-20.php
Java - Get current timestamp
May 19, 2025 - import java.time.*; public class Exercise20 { public static void main(String[] args) { Instant timestamp = Instant.now(); System.out.println("\nCurrent Timestamp: " + timestamp+"\n"); } } ... N.B.: The result may varry for your system date and time.
🌐
LabEx
labex.io › tutorials › java-how-to-retrieve-system-timestamp-421176
How to retrieve system timestamp | LabEx
Learn efficient methods to retrieve system timestamps in Java, exploring current time retrieval techniques, system clock methods, and practical timestamp applications for developers.
🌐
Quora
quora.com › How-do-you-calculate-the-starting-timestamp-for-the-current-day-in-Java
How to calculate the starting timestamp for the current day in Java - Quora
Avoid java.util.Date / Calendar for new code. If interoperating with old APIs: ... If you must use epoch seconds rather than millis: startOfDay.getEpochSecond() or startOfDayMillis / 1000. For nanosecond precision, use ZonedDateTime or Instant with getNano() where supported.
🌐
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 ...