The date type object can be converted to a string in Java using a large variety of in-built classes available in Java.
Given a date, we need to convert the date to a given format of the string.
Examples:
Input: date = “2020-11-13”
Output: 2020-11-13Input: date = “2020-12-14”
Output: 2020-12-14
Method 1: Using DateFormat class
- The Calendar class is used to provide functions for carrying out interconversion between any instant in time and a set of calendar fields. The Calendar class has an in-built method getInstance() which is used to fetch the current date and time. It returns the date which is stored in a Date type variable.
- DateFormat class is an in-built class in Java that is used to parse and format date or time values. We can specify the format for the Date using the SimpleDateFormat class object, which takes as an argument the date format and returns the DateFormat specifier.
The DateFormat class has an in-built method format() which has the following syntax :
DateFormat.format(date_type_object)
Returns a string variable corresponding to the date type object provided as an argument to the function.
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.
Java
// Java Program to convert date to string import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class DateToString { public static void main(String args[]) { // used to fetch current date and time Date date = Calendar.getInstance().getTime(); // specify the format yyyy-mm-dd to print current // date to as an argument DateFormat date_format = new SimpleDateFormat( "yyyy-mm-dd" ); // print date in the specified format String date_string = date_format.format(date); // printing date in string System.out.println( "Date to String : " + date_string); } } |
Date to String : 2020-06-12
Method 2: Using SimpleDateFormat class
SimpleDateFormat class is used for formatting and parsing dates. It has an in-built format() method, which can be used to print date in string formats in a variety of ways.
Java
// Java Program to convert date to string import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class DateToString { public static void main(String[] args) { // define today's date under date variable Date date = new Date(); // specify the date format to be used SimpleDateFormat date_format1 = new SimpleDateFormat( "MM/dd/yyyy" ); String date_str = date_format1.format(date); System.out.println( "MM/dd/yyyy : " + date_str); // another date format SimpleDateFormat date_format2 = new SimpleDateFormat( "dd MMMM yyyy zzzz" ); date_str = date_format2.format(date); System.out.println( "dd MMMM yyyy zzzz : " + date_str); } } |
MM/dd/yyyy : 11/12/2020 dd MMMM yyyy zzzz : 12 November 2020 Coordinated Universal Time
Method 3: Using LocalDate.toString() method
- 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 today = LocalDate.parse(date); // Convert the given date into a // string using toString()method String dateToString = today.toString(); // Return the result return (dateToString); } // Driver Code public static void main(String args[]) { // Given Date String date = "2020-11-13" ; // Convert and print the result System.out.print( convertDateToString(date)); } } |
2020-11-13
Time Complexity: O(1)
Auxiliary Space: O(1)