// 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
🌐
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 - With java.time.LocalTime, let’s retrieve the current system time: ... LocalDateTime localDateTime = LocalDateTime.now(); LocalTime localTime = localDateTime.toLocalTime(); Use java.time.Instant to get a time stamp from the Java epoch.
Discussions

datetime - How to get the current date/time in Java - Stack Overflow
Do not be mislead by toString method ... JVM’s current default time zone while generating a String. That method’s behavior was a well-intentioned by horribly unwise design decision. One of many reasons to avoid the legacy date-time classes (Date, Calendar, SimpleDateFormat, Timestamp, etc.). 2018-03-28T00:06:14.05Z+00:00 ... @Basil, There is no timezone internally associated with java.util... More on stackoverflow.com
🌐 stackoverflow.com
How to get timestamp along with timezone in java
Hello, How to get a timestamp with timezone in java. import java.util.Date; import java.util.TimeZone; SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); String dateV... More on forums.oracle.com
🌐 forums.oracle.com
May 4, 2023
parse Timestamp to String
Your SimpleDateFormat constructor needs to match the string you're giving it. Try "yyyy-MM-dd HH:mm:ss.SSS" More on reddit.com
🌐 r/programminghelp
1
1
February 27, 2024
Get about the epoch when I'm trying to get about the current time
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
4
1
December 28, 2021
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
To display the current time (hour, minute, second, and nanoseconds), import the java.time.LocalTime class, and use its now() method:
🌐
Mkyong
mkyong.com › home › java › how to get current timestamps in java
How to Get Current Timestamps in Java - Mkyong.com
March 7, 2025 - Starting with Java 8, the modern and preferred way to get a timestamp is by using the Instant class: ... package com.mkyong; import java.time.Instant; public class CurrentTimestampExample { public static void main(String[] args) { Instant ...
🌐
Medium
medium.com › @AlexanderObregon › javas-instant-now-method-explained-5403bac7ec1e
Java’s Instant.now() Method Explained | Medium
September 13, 2024 - Java’s Instant.now() method is a powerful and precise tool for capturing the current timestamp in your applications.
Top answer
1 of 16
792

What's the best way to get the current date/time in Java?

There is no "best" way.

It depends on what form of date / time you want:

  • If you want the date / time as a single numeric value, then System.currentTimeMillis() gives you that, expressed as the number of milliseconds after the UNIX epoch (as a Java long). This value is a delta from a UTC time-point, and is independent of the local time-zone1.

  • If you want the date / time in a form that allows you to access the components (year, month, etc) numerically, you could use one of the following:

    • new Date() gives you a Date object initialized with the current date / time. The problem is that the Date API methods are mostly flawed ... and deprecated.

    • Calendar.getInstance() gives you a Calendar object initialized with the current date / time, using the default Locale and TimeZone. Other overloads allow you to use a specific Locale and/or TimeZone. Calendar works ... but the APIs are still cumbersome.

    • new org.joda.time.DateTime() gives you a Joda-time object initialized with the current date / time, using the default time zone and chronology. There are lots of other Joda alternatives ... too many to describe here. (But note that some people report that Joda time has performance issues.; e.g. https://stackoverflow.com/questions/6280829.)

    • in Java 8, calling java.time.LocalDateTime.now() and java.time.ZonedDateTime.now() will give you representations2 for the current date / time.

Prior to Java 8, most people who know about these things recommended Joda-time as having (by far) the best Java APIs for doing things involving time point and duration calculations.

With Java 8 and later, the standard java.time package is recommended. Joda time is now considered "obsolete", and the Joda maintainers are recommending that people migrate3.


Note: the Calendar, org.joda.time and java.time solutions can use either the platform's default timezone or an explicit timezone provided via constructor arguments. Generally, using an explicit timezone rather than the default zone will make your application's behavior more predictable / less susceptible to problems if (for example) you redeploy to a data center in a different timezone.

But no matter what you do, you (and maybe your application) should be aware that the timezone of the user, your service and the data center can all be different. The concept of the "current date/time" is complicated.


1 - System.currentTimeMillis() gives the "system" time. While it is normal practice for the system clock to be set to (nominal) UTC, there will be a difference (a delta) between the local UTC clock and true UTC. The size of the delta depends on how well (and how often) the system's clock is synced with UTC.
2 - Note that LocalDateTime doesn't include a time zone. As the javadoc says: "It cannot represent an instant on the time-line without additional information such as an offset or time-zone."
3 - Note: your Java 8 code won't break if you don't migrate, but the Joda codebase may eventually stop getting bug fixes and other patches. As of 2020-02, an official "end of life" for Joda has not been announced, and the Joda APIs have not been marked as Deprecated.

2 of 16
451

(Attention: only for use with Java versions <8. For Java 8+ check other replies.)

If you just need to output a time stamp in format YYYY.MM.DD-HH.MM.SS (very frequent case) then here's the way to do it:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
🌐
Phrase
phrase.com › home › resources › blog › how to get the current utc date and time in java?
Solved: How to Get the Current UTC Date and Time in Java?
September 23, 2022 - A better and more modern option bundled within the core Java library (and not using any third-party offerings) is to use the java.time package. If you look at the documentation page, the package is based on the ISO calendar system and offers a simplified API for displaying and handling date and time instances. Below is an overview of the capabilities exposed in this package: Based on this, we can infer that this package represents the time (with the location unspecified), as UTC, or as an offset from UTC. Indeed, if we check the Instant class to get the current time, we can see the date time printed as UTC:
Find elsewhere
🌐
CalliCoder
callicoder.com › how-to-get-current-epoch-timestamp-java
How to get current epoch timestamp in Java | CalliCoder
February 18, 2022 - public class CurrentEpochTimestampExample { public static void main(String[] args) { // Get epoch timestamp using System.currentTimeMillis() long currentTimestamp = System.currentTimeMillis(); System.out.println("Current epoch timestamp in millis: " + currentTimestamp); } } import java.time.Instant; public class CurrentEpochTimestampExample { public static void main(String[] args) { // Get current timestamp using Instant currentTimestamp = Instant.now().toEpochMilli(); System.out.println("Current epoch timestamp in millis: " + currentTimestamp); } }
🌐
Attacomsian
attacomsian.com › blog › java-get-current-date-time
How to get current date and time in Java
October 14, 2022 - Java 8 Date & Time API · Get current date using LocalDate class · Get current time using LocalTime class · Get current date and time using LocalDateTime class · Get current date and time with time zone using ZonedDateTime class · Get current timestamp using Instant class ·
🌐
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 ...
🌐
Oracle
forums.oracle.com › ords › apexds › post › how-to-get-timestamp-along-with-timezone-in-java-2000
How to get timestamp along with timezone in java
May 4, 2023 - Hello, How to get a timestamp with timezone in java. import java.util.Date; import java.util.TimeZone; SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); String dateV...
🌐
Vultr Docs
docs.vultr.com › java › examples › get-current-datetime
Java Program to Get Current Date/TIme | Vultr Docs
December 16, 2024 - Import the Instant class. Use Instant.now() to get the GMT/UTC time. ... import java.time.Instant; Instant timestamp = Instant.now(); System.out.println("Current GMT/UTC Time: " + timestamp); Explain Code
🌐
TecAdmin
tecadmin.net › get-current-timestamp-in-java
5 Methods to Get Current TimeStamp in JAVA – TecAdmin
April 26, 2025 - The System.currentTimeMillis() method provides the simplest way to obtain the current timestamp in Java. This method returns the current time in milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT). ... public class GetCurrentTimestamp { public static void main(String[] args) { ...
🌐
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....
🌐
Sentry
sentry.io › sentry answers › java › how do i get the current date and time in java?
How do I get the current date and time in Java? | Sentry
January 15, 2025 - You can the current date and time using classes from the java.time package introduced in Java 8. We will demonstrate how to use the following recommended java.time classes: ... Note: You need to be running Java version 8 or later to use java.time classes. The LocalDateTime class is commonly used to get the current date and time without time zone information:
🌐
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 ...
🌐
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
Answer (1 of 5): Thanks for A2A. To get instance of calendar: Calendar cal = Calendar.getInstance() To set current date timestamp where ‘time’ is a timestamp for the current day: cal.setTimeInMillis(time.getTime()) To set hour: cal. set (Calendar .HOUR_OF_DAY, 0) To set minute: cal. set (...
🌐
W3Docs
w3docs.com › java
How to get current timestamp in string format in Java? "yyyy ...
Here's an example of how you can ... import java.util.Date; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); String timestamp = dateFormat.format(new Date());...
🌐
w3resource
w3resource.com › java-exercises › datetime › java-datetime-exercise-20.php
Java - Get current timestamp
import java.time.*; public class ... and time. ... Write a Java program to get the current timestamp using System.currentTimeMillis() and format it....