Convert String to date in Java

Posted in :

Convert String to Date: Function

After this section I have shared a complete example to demonstrate String to Date conversion in various date formats. For those who just want a function for this conversion, here is the function code:

public Date convertStringToDate(String dateString)
{
    Date date = null;
    Date formatteddate = null;
    DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    try{
        date = df.parse(dateString);
        formatteddate = df.format(date);
    }
    catch ( Exception ex ){
        System.out.println(ex);
    }
    return formatteddate;
}

Example program for string to date conversion

package beginnersbook.com;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateDemo{
   public static void main(String args[])
   {
       String testDateString = "02/04/2014";
       String testDateString2 = "02-04-2014 23:37:50";
       String testDateString3 = "02-Apr-2014";
       String testDateString4 = "04 02, 2014";
       String testDateString5 = "Thu, Apr 02 2014";
       String testDateString6 = "Thu, Apr 02 2014 23:37:50";
       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. 
           Date d1 = df.parse(testDateString);
           System.out.println("Date: " + d1);
           System.out.println("Date in dd/MM/yyyy format is: "+df.format(d1));

           Date d2 = df2.parse(testDateString2);
           System.out.println("Date: " + d2);
           System.out.println("Date in dd-MM-yyyy HH:mm:ss format is: "+df2.format(d2));

           Date d3 = df3.parse(testDateString3);
           System.out.println("Date: " + d3);
           System.out.println("Date in dd-MMM-yyyy format is: "+df3.format(d3));

           Date d4 = df4.parse(testDateString4);
           System.out.println("Date: " + d4);
           System.out.println("Date in MM dd, yyyy format is: "+df4.format(d4));

           Date d5 = df5.parse(testDateString5);
           System.out.println("Date: " + d5);
           System.out.println("Date in E, MMM dd yyyy format is: "+df5.format(d5));

           Date d6 = df6.parse(testDateString6);
           System.out.println("Date: " + d6);
           System.out.println("Date in E, E, MMM dd yyyy HH:mm:ss format is: "+df6.format(d6));

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

Output:

Date: Wed Apr 02 00:00:00 IST 2014
Date in dd/MM/yyyy format is: 02/04/2014
Date: Wed Apr 02 23:37:50 IST 2014
Date in dd-MM-yyyy HH:mm:ss format is: 02-04-2014 23:37:50
Date: Wed Apr 02 00:00:00 IST 2014
Date in dd-MMM-yyyy format is: 02-Apr-2014
Date: Wed Apr 02 00:00:00 IST 2014
Date in MM dd, yyyy format is: 04 02, 2014
Date: Wed Apr 02 00:00:00 IST 2014
Date in E, MMM dd yyyy format is: Wed, Apr 02 2014
Date: Wed Apr 02 23:37:50 IST 2014
Date in E, E, MMM dd yyyy HH:mm:ss format is: Wed, Apr 02 2014 23:37:50

 

 


Java string to date(字串轉日期)

Java字串轉日期範例
//欲轉換的日期字串
String dateString = “20010-03-02 20:25:58”;
//設定日期格式
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
//進行轉換
Date date = sdf.parse(dateString);
System.out.println(date);

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *