Date to String Java


It is required in Java, many a times in programming, to convert Date to String and also String to Date. To do either, the best class we can use is java.text.SimpleDateFormat. Following explanation gives the first one – Date to String.

Steps

1. Ceate a Date object.
2. Create a SimpleDateFormat object by passing the date format you would like to its constructor.
3. Use foramt() method of SimpleDateFormat and pass the Date object as parameter to format(). format() method returns a String with the same date format style you passed as parameter to SimpleDateFormat constructor.

Following program on Date to String follows the above 3 steps with different date formats and converting each date format into a string representation.
import java.util.Date;
import java.text.SimpleDateFormat;

public class DateString
{
  public static void main(String args[])
  {
    Date today1 = new Date();
    System.out.println("today1 :: " + today1);    

			   // let us convert different formats of today1 into strings
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    String stringToday1 = sdf1.format(today1);
    System.out.println("stringToday1 in yyyy-MM-dd :: " + stringToday1);

    SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
    String stringToday2 = sdf2.format(today1);
    System.out.println("stringToday2 in dd/MM/yyyy :: " + stringToday2);

    SimpleDateFormat sdf3 = new SimpleDateFormat("ddMMyyyy"); // without separators
    String stringToday3 = sdf3.format(today1);
    System.out.println("stringToday3 in ddMMyyyy :: " + stringToday3);

    SimpleDateFormat sdf4 = new SimpleDateFormat("dd-MM-yy"); 
    String stringToday4 = sdf4.format(today1);
    System.out.println("stringToday4 in dd-MM-yy :: " + stringToday4);

    SimpleDateFormat sdf5 = new SimpleDateFormat("dd,MMMM yyyy"); 
    String stringToday5 = sdf5.format(today1);	     // same can be dd-MMMM-yyyy
    System.out.println("stringToday5 in dd, MMMM yyyy :: " + stringToday5);
  }
}

Date to String

The above code is a practical example taking date from the user. The date can be converted also into java.sql.Date to enter into database using JDBC.

SimpleDateFormat sdf1 = new SimpleDateFormat(“yyyy-MM-dd”);
String stringToday1 = sdf1.format(today1);

First choose the format you like and pass it to the SimpleDateFormat constructor. Pass the Date object to format() method. The format() method returns the date object as string in the form of the format passed to SimpleDateFormat constructor.

Precautions

1. Lowercase "m" represents minutes and uppercase "M" represents Month (many people do mistake here only).
2. Lowercase "d" represents date in a month and uppercase "D" represents Day of a week.
3. You cannot compare ddMMyyyy with yyyyMMdd. Both are not equal.
4. Use yyyy\\MM\\dd for yyyy\MM\dd as \ (backslash) is an escape sequence.
5. SimpleDateFormat is not thread-safe; its methods are not synchronized. Better create a separate SimpleDateFormat object for each representation as in the above code when parsing date is done.

It is also important for a developer to familiarize with the utility classes used frequently in coding – Date, Calendar, TimeZone and Locale and GregorianCalendar.

In the following code, the date information is taken from keyboard from the user and converted into Date and then again back to string. It is more a practical example.

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class DateString
{
  public static void main(String args[]) throws ParseException
  {
    SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");  // pass any format you like
	    // take input from keyboard or from GUI, the date from the user. Say the user enters 12-06-2013	      
    Date today1 = sdf1.parse("12-06-2013");     // first convert user entered date into a Date object

    String dateString1 = sdf1.format(today1);	// format() method throws a checked exception, ParseException	

    System.out.println(dateString1);
  }
}

Date to String

Would you like to other way of converting String to Date also?

Leave a Comment

Your email address will not be published.