Question

I have the date in the string format like this "Sat Sep 8 10:13:09 GMT+0530 2012", can anybody help me how to convert this string date in to Date class object.

Was it helpful?

Solution

Try this code. Of course, I have modified as per your string, but you might have to modify the format little bit if it doesn't work out.

import java.util.*;
import java.text.*;

public class StringToDate {
    public static void main(String[] args){
        try{  
            String str_date="Sat Sep 8 10:13:09 GMT+0530 2012";
            DateFormat formatter ; 
            Date date ; 
            formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'z yyyy");
            date = (Date)formatter.parse(str_date);  
            System.out.println("Today is " +date );
        } 
        catch (ParseException e){
            System.out.println("Exception :"+e);  
        }  
    }
}  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top