Java Program to Convert Date to String
Last Updated :
23 Nov, 2022
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-13
Input: 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
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[])
{
Date date = Calendar.getInstance().getTime();
DateFormat date_format = new SimpleDateFormat( "yyyy-mm-dd" );
String date_string = date_format.format(date);
System.out.println( "Date to String : "
+ date_string);
}
}
|
Output
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
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)
{
Date date = new Date();
SimpleDateFormat date_format1 = new SimpleDateFormat( "MM/dd/yyyy" );
String date_str = date_format1.format(date);
System.out.println( "MM/dd/yyyy : " + date_str);
SimpleDateFormat date_format2 = new SimpleDateFormat( "dd MMMM yyyy zzzz" );
date_str = date_format2.format(date);
System.out.println( "dd MMMM yyyy zzzz : "
+ date_str);
}
}
|
Output
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
import java.time.LocalDate;
class GFG {
public static String
convertDateToString(String date)
{
LocalDate today = LocalDate.parse(date);
String dateToString
= today.toString();
return (dateToString);
}
public static void main(String args[])
{
String date = "2020-11-13" ;
System.out.print(
convertDateToString(date));
}
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...