Convert Date to String in Java

Earlier we saw, how to convert String to Date in Java. This post is a continuation of that post and here we will learn Date to String conversion in Java.

Java Code: Convert Date to String in Java

After this section I have shared a complete code of Date to String conversion. The below function converts a Date to a String. In the below function I have used the format dd/MM/yyyy, however if you want the result in any other format then you can simply modify the pattern in SimpleDateFormat. You can also refer one of my post on date formats in Java.

Function:

public String convertStringToDate(Date indate)
{
   String dateString = null;
   SimpleDateFormat sdfr = new SimpleDateFormat("dd/MMM/yyyy");
   /*you can also use DateFormat reference instead of SimpleDateFormat 
    * like this: DateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
    */
   try{
	dateString = sdfr.format( indate );
   }catch (Exception ex ){
	System.out.println(ex);
   }
   return dateString;
}

Complete Example program for Date to String conversion

In this example I am taking current date as an input and converting into a String. In order to get the output String in various format I have specified different-2 patterns in SimpleDateFormat.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringDemo{
   public static void main(String args[])
   {
       Date todaysDate = new Date();
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
       DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
       DateFormat df3 = new SimpleDateFormat("dd-MMM-yyyy");
       DateFormat df4 = new SimpleDateFormat("MM dd, yyyy");
       DateFormat df5 = new SimpleDateFormat("E, MMM dd yyyy");
       DateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
       try
       {
           //format() method Formats a Date into a date/time string. 
           String testDateString = df.format(todaysDate);
           System.out.println("String in dd/MM/yyyy format is: " + testDateString);
           String str2 = df2.format(todaysDate);
           System.out.println("String in dd-MM-yyyy HH:mm:ss format is: " + str2);
           String str3 = df3.format(todaysDate);
           System.out.println("String in dd-MMM-yyyy format is: " + str3);
           String str4 = df4.format(todaysDate);
           System.out.println("String in MM dd, yyyy format is: " + str4);
           String str5 = df5.format(todaysDate);
           System.out.println("String in E, MMM dd yyyy format is: " + str5);
           String str6 = df6.format(todaysDate);
           System.out.println("String in E, E, MMM dd yyyy HH:mm:ss format is: " + str6);

       }
       catch (Exception ex ){
          System.out.println(ex);
       }
    }
}

Output:

String in dd/MM/yyyy format is: 02/01/2014
String in dd-MM-yyyy HH:mm:ss format is: 02-01-2014 22:38:35
String in dd-MMM-yyyy format is: 02-Jan-2014
String in MM dd, yyyy format is: 01 02, 2014
String in E, MMM dd yyyy format is: Thu, Jan 02 2014
String in E, E, MMM dd yyyy HH:mm:ss format is: Thu, Jan 02 2014 22:38:35

References:

0 comments… add one

Leave a Comment