// 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 - 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 ...
Videos
03:34
How Do I Get The Current Timestamp In Java? - Next LVL Programming ...
02:29
java.sql.Timestamp class Introduction | Java Date and Time - YouTube
01:13
How to format java.sql.Timestamp object? | Java Date and Time - ...
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 - ...
08:44
Learn DATES & TIMES with Java in 8 minutes! 📆 - YouTube
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.
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()); ...
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 ...
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.
Naukri
naukri.com › code360 › library › java-timestamp
Java Timestamp - Naukri Code 360
October 8, 2024 - Almost there... just a few more seconds
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 ...
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 ...
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, ...