In Java, dealing with dates and times is a common task in various applications, from simple utilities to complex enterprise systems. Often, we will find yourself needing to convert dates to strings for display or data manipulation purposes. In this section, we will explore the different approaches to converting a Date object to a String in Java.
We can convert Date to String in Java by using the format() method of java.text.DateFormat class.

The SimpleDateFormat class allows us to format a Date object into a string representation according to a specified pattern. DateFormat is an abstract class. The child class of DateFormat is SimpleDateFormat. It is the implementation of DateFormat class.
The signature of format() method is given below:/p>
Let's see the simple code to convert Date to String in Java.
Let us take an example to convert date to string.
Java
Output:
Converted String: 2024-43-17 03:43:28
In this example, we create a SimpleDateFormat instance with the desired pattern "yyyy-MM-dd HH:mm:ss" to represent the date in "year-month-day hour:minute:second" format.
Let's see the complete example to convert date and time into String in Java by using the format() method of java.text.SimpleDateFormat class.
Java
Output:
Date Format with MM/dd/yyyy : 03/17/2024 Date Format with dd-M-yyyy hh:mm:ss : 17-3-2024 03:45:32 Date Format with dd MMMM yyyy : 17 March 2024 Date Format with dd MMMM yyyy zzzz : 17 March 2024 Greenwich Mean Time Date Format with E, dd MMM yyyy HH:mm:ss z : Sun, 17 Mar 2024 15:45:32 GMT
With the introduction of the java.time package in Java 8, we can use the DateTimeFormatter class to achieve the same result. Here's how we can do it:
Java
Output:
Formatted DateTime: 2024-03-17 15:30:45
Explanation
This output represents the current date and time formatted according to the specified pattern ("yyyy-MM-dd HH:mm:ss").
In this example: We use LocalDateTime.now() to obtain the current date and time. DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") creates a formatter with the desired pattern that represents the year, month, day, hour, minute, and second components of the date and time. currentDateTime.format(formatter) formats the LocalDateTime object using the specified formatter, resulting in a string representation of the date and time according to the pattern.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India