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:
- Using Simple Date format(): In this method, we use simple date format i.e Date Format df= New simpleDateFormat (“MM-DD-YYYY”).
- 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.
- Using Date Time Formatter: In this method we use Formatter for printing and parsing date-time objects.
- 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.
- 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/