Search code examples
androiddatetimesimpledateformatdate-conversion

Converting String value date into Date using simple date format in android


I have a string variable which contains date and time in the following format

EDIT:

String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";

I need to covert and parse it into this date format using simpledateformat as below:

Date nd=04-03-2014 16:58:00

But I don't know the string pattern for converting using simpledateformat.

I have given as

 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-dd-MM hh:mm:ss");

But it gave me wrong output.

Is the string pattern for simpledateformat sdf correct? If I'm wrong can someone please correct me in converting newdate to date nd format.


Solution

  • Try to use

    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");

    But before conversion replace "." on ":" in "GMT+05.30"

    Example:

        String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
    
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
    
        try {
         Date d = sdf.parse(newdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }