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
Approach:
- Get the date to be converted.
- Create an instance of SimpleDateFormat class to format the string representation of the date object.
- Get the date using the Calendar object.
- Convert the given date into a string using format() method.
- Print the result.
Below is the implementation of the above approach:
Approach:
- Get an instance of LocalDate from date.
- Convert the given date into a string using the toString() method of LocalDate class.
- Print the result.
Below is the implementation of the above approach:
Method 3: Using DateTimeFormatter.format() method
Approach:
- Get an instance of LocalDate from date.
- Create a DateTimeFormatter object to format the LocalDate object as a string.
- Use the format() method of the DateTimeFormatter object to convert the LocalDate object to a string.
- Print the result.
Below is the implementation of the above approach: