How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?

How to Get the Current Time in YYYY-MM-DD HH:MI:Sec.Millisecond Format in Java?

šŸ•’ Have you ever wondered how to get the current time in Java but with milliseconds included? The code snippet you provided is a good start, but it's missing that extra precision. šŸ’„ In this blog post, we will address this common issue and provide you with an easy solution. So let's dive in and learn how to retrieve the current time in the desired format!

The Problem

šŸ” The code you shared:

public static String getCurrentTimeStamp() {
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date now = new Date();
    String strDate = sdfDate.format(now);
    return strDate;
}

does a great job of giving you the current time in the format YYYY-MM-DD HH:MM:SS (e.g., 2009-09-22 16:47:08). However, it doesn't include the milliseconds component, which is what you're looking for.

The Solution

šŸš€ To retrieve the current time in the format YYYY-MM-DD HH:MM:SS.MS (e.g., 2009-09-22 16:47:08.128), we need to make a slight modification to the code.

public static String getCurrentTimeStampWithMillis() {
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Date now = new Date();
    String strDate = sdfDate.format(now);
    return strDate;
}

By adding an extra SSS in the date format pattern, we are telling Java to include the milliseconds component. This will give us the desired output with milliseconds.

Example Usage

šŸ’” Here's an example of how you can use the getCurrentTimeStampWithMillis() method:

public static void main(String[] args) {
    String currentTime = getCurrentTimeStampWithMillis();
    System.out.println("Current Time: " + currentTime);
}

Running the above code will display the current time in the YYYY-MM-DD HH:MM:SS.MS format in the console.

Conclusion

šŸŽ‰ And there you have it! With just a small modification to your code, you can now easily retrieve the current time in the YYYY-MM-DD HH:MM:SS.MS format. This additional precision can be useful in various scenarios where milliseconds matter.

So go ahead, update your code, and enjoy the precision of milliseconds in your timestamps! 😊

šŸ“¢ If you found this blog post helpful, share it with your fellow developers and inspire them to embrace the power of milliseconds in their Java applications! And don't forget to leave a comment below if you have any questions or further suggestions on how to improve this solution.

Happy coding! šŸ’»āœØ

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my