// 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 ...
🌐
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.
🌐
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.
🌐
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....
🌐
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 ...
🌐
w3resource
w3resource.com › java-exercises › datetime › java-datetime-exercise-20.php
Java - Get current timestamp
2 weeks ago - 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.
🌐
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:
🌐
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...
🌐
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.
🌐
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); ...
🌐
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.