VOOZH about

URL: https://www.geeksforgeeks.org/dsa/how-to-convert-date-to-string-in-java/

⇱ How to convert Date to String in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to convert Date to String in Java

Last Updated : 15 Jul, 2025

Given a date, the task is to write a Java program to convert the given date into a string.

Examples:

Input: date = "2020-07-27" 
Output: 2020-07-27

Input: date = "2018-02-17" 
Output: 2018-02-17

Method 1: Using DateFormat.format() method

Approach:

  1. Get the date to be converted.
  2. Create an instance of SimpleDateFormat class to format the string representation of the date object.
  3. Get the date using the Calendar object.
  4. Convert the given date into a string using format() method.
  5. Print the result.

Below is the implementation of the above approach:

Output:
07-27-2020

Method 2: Using LocalDate.toString() method

Approach:

  1. Get an instance of LocalDate from date.
  2. Convert the given date into a string using the toString() method of LocalDate class.
  3. Print the result.

Below is the implementation of the above approach:

Output:
2020-07-27

Method 3: Using DateTimeFormatter.format() method

Approach:

  1. Get an instance of LocalDate from date.
  2. Create a DateTimeFormatter object to format the LocalDate object as a string.
  3. Use the format() method of the DateTimeFormatter object to convert the LocalDate object to a string.
  4. Print the result.

Below is the implementation of the above approach:


Output
2022-12-31
Comment