• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
August 30, 2022 |830 Views
Java program to Convert Date to String
  Share  1 Like
Description
Discussion

In this video, we will write a Java program for converting dates into strings.

Examples:
Input: date = “2022-8-24”
Output: 2022-08-24

Here we see five methods to convert date to string in Java:

  1. Using Simple Date format(): In this method, we use simple date format i.e Date Format df= New simpleDateFormat (“MM-DD-YYYY”).
  2. Using string class: In this method, we create an instance of SimpleDateFormat class to format the string representation of the date object. After that get the date using the Calendar object, and convert the given date into a string using format() method.
  3. Using Date Time Formatter: In this method we use Formatter for printing and parsing date-time objects.
  4. Using Time zone, Date Time Formatter: The ZonedDateTime class permits us to create a date/time object with a time zone. The default time zone will be used. So here we use the DateTimeFormatter class to display the time zone with date.
  5. Using Local Date Time: Here we get an instance of LocalDate from date. Then convert the given date into a string using the toString() method of LocalDate class.

How to convert date to string in Java:
https://www.geeksforgeeks.org/how-to-convert-date-to-string-in-java/