Search This Blog

Friday 11 April 2014

How to convert String to Date in Java

How to convert String to Date in Java - SimpleDateFormat Example

SimpleDateFormat in Java can be used to convert String to Date in Java. java.text.SimpleDateFormat is an implementation of DateFormat which defines a date pattern and can convert a particular String which follows that pattern into Date in Java. e.g. ddMMyy or dd-MM-yyyy. Though converting String to Date is quite easy using SimpleDateFormat, but you need to remember that SimpleDateFormat is not thread-safe, which means you can not share same instance of SimpleDateFormat between multiple threads. Avoid storing SimpleDateFormat in static variable and if you want to safely share or reuse SimpleDateFormat.



Steps to Convert String into Date

How to convert String to Date in Java program with SimpleDateFormat exampleConverting String to date is rather common scenario because you may get date in a String format from any file or xml document. SimpleDateFormat in Java also support time information e.g. HH for hour , mm for minutes and SS for seconds.

Here are steps we need to use for conversion in Java:

1) Create a SimpleDateFormat object with a date pattern e.g. dd-MM-yyyy. here d denotes day of month, M is for month of year and yyyy is year in four digit e.g. 2012. Java documentation of SimpleDateFormat has complete list of date and time pattern specified.

2) Call parse() method of SimpleDateFormat and cast the result into Date object and you are done. parse() method of SimpleDateFormat throws ParseException so you need to either throw it or you can provide handling of this exception. Let’s see some SimpleDateFormat Example of converting string to date in Java to get hold of concept.       

SimpleDateFormat Example in Java


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

/**
 * Java program to convert String to Date in Java. This example
 * use SimpleDateFormat for String to Date conversion, you can also
 * use JODA date and time API for that.
 *
 * @author Javin
 */

public class StringToDateExample{
   
    
public static void main(String args[]) throws ParseException{
       
        DateFormat formatter = 
null;
        Date convertedDate = 
null;
       
        
// Creating SimpleDateFormat with yyyyMMdd format e.g."20110914"
        String yyyyMMdd = 
"20110914";
        formatter = 
new SimpleDateFormat("yyyyMMdd");
        convertedDate = 
(Date) formatter.parse(yyyyMMdd);
        System.
out.println("Date from yyyyMMdd String in Java : " + convertedDate);

        
//convert string to date with ddMMyyyy format example "14092011"
        String ddMMyyyy = 
"14092011";
        formatter = 
new SimpleDateFormat("ddMMyyyy");
        convertedDate = 
(Date) formatter.parse(ddMMyyyy);
        System.
out.println("Date from ddMMyyyy String in Java : " + convertedDate);

        
//String to Date conversion in Java with dd-MM-yyyy format e.g. "14-09-2011"
        String dd_MM_YY = 
"14-09-2011";
        formatter = 
new SimpleDateFormat("dd-MM-yyyy");
        convertedDate = 
(Date) formatter.parse(dd_MM_YY);
        System.
out.println("Date from dd-MM-yyyy String in Java : " + convertedDate);

        
// dd/MM/yyyy date format for example "14/09/2011"
        String stringDateFormat = 
"14/09/2011";
        formatter = 
new SimpleDateFormat("dd/MM/yyyy");
        convertedDate = 
(Date) formatter.parse(stringDateFormat);
        System.
out.println("Date from dd/MM/yyyy String in Java : " + convertedDate);

        
//parsing string into date with dd-MMM-yy format e.g. "14-Sep-11"
        
//MMMM denotes three letter month String e.g. Sep
        String ddMMMyy = 
"14-Sep-11";
        formatter = 
new SimpleDateFormat("dd-MMM-yy");
        convertedDate = 
(Date) formatter.parse(ddMMMyy);
        System.
out.println("Date from dd-MMM-yy String in Java : " + convertedDate);

        
//convert string to Date of dd-MMMM-yy format e.g. "14-September-11"
        
//MMMM denotes full month String e.g. September
        String dMMMMyy = 
"14-September-11";
        formatter = 
new SimpleDateFormat("dd-MMMM-yy");
        convertedDate = 
(Date) formatter.parse(dMMMMyy);
        System.
out.println("Date from dd-MMMM-yy String in Java : " + convertedDate);
       
        
//SimpleDateFormat also allows to include time information e.g. dd-MM-yyyy:HH:mm:SS
        String date = 
"15-09-2011:23:30:45";
        formatter = 
new SimpleDateFormat("dd-MM-yyyy:HH:mm:SS");
        convertedDate = 
(Date) formatter.parse(date);
        System.
out.println("Date from dd-MM-yyyy:HH:mm:SS String in Java : " 
                            + convertedDate);
    
}
}

Output:
Date from yyyyMMdd String in Java : Wed Sep 
14 00:00:00 PST 2011
Date from ddMMyyyy String in Java : Wed Sep 
14 00:00:00 PST 2011
Date from dd-MM-yyyy String in Java : Wed Sep 
14 00:00:00 PST 2011
Date from dd/MM/yyyy String in Java : Wed Sep 
14 00:00:00 PST 2011
Date from dd-MMM-yy String in Java : Wed Sep 
14 00:00:00 PST 2011
Date from dd-MMMM-yy String in Java : Wed Sep 
14 00:00:00 PST 2011
Date from dd-MM-yyyy:HH:mm:SS String in Java : Thu Sep 
15 23:30:00 PST 2011


No comments:

Post a Comment