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-27Input: date = “2018-02-17”
Output: 2018-02-17
Method 1: Using DateFormat.format() method
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:
Java
// Java program to convert Date to String import java.util.Calendar; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; class GFG { // Function to convert date to string public static String convertDateToString(String date) { // Converts the string // format to date object DateFormat df = new SimpleDateFormat(date); // Get the date using calendar object Date today = Calendar.getInstance() .getTime(); // Convert the date into a // string using format() method String dateToString = df.format(today); // Return the result return (dateToString); } // Driver Code public static void main(String args[]) { // Given Date String date = "07-27-2020"; // Convert and print the result System.out.print( convertDateToString(date)); } } |
07-27-2020
Method 2: Using LocalDate.toString() method
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:
Java
// Java program to convert Date to String import java.time.LocalDate; class GFG { // Function to convert date to string public static String convertDateToString(String date) { // Get an instance of LocalTime // from date LocalDate givenDate = LocalDate.parse(date); // Convert the given date into a // string using toString()method String dateToString = givenDate.toString(); // Return the result return (dateToString); } // Driver Code public static void main(String args[]) { // Given Date String date = "2020-07-27"; // Convert and print the result System.out.print( convertDateToString(date)); } } |
2020-07-27
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Convert String to Date in Java
- Program to Convert Milliseconds to a Date Format in Java
- Convert a List of String to a comma separated String in Java
- Convert a Set of String to a comma separated String in Java
- Convert an ArrayList of String to a String array in Java
- Convert Set of String to Array of String in Java
- Convert String to Double in Java
- How to convert an Array to String in Java?
- How to convert a string to boolean in Java
- Program to convert IntStream to String in Java
- Convert List of Characters to String in Java
- Convert a String to a List of Characters in Java
- How to Convert a Short value to String value in Java with Examples
- How to Convert a Byte value to String value in Java with Examples
- How to Convert a Double value to String value in Java with Examples
- Program to Convert Set of Integer to Set of String in Java
- How to Convert a Float value to String value in Java with Examples
- Program to convert String to IntStream in Java
- How to Convert a String value to Short value in Java with Examples
- How to Convert a String value to Float value in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

