njnjnj
njnjnj

Reputation: 984

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.

Upvotes: 1

Views: 1254

Answers (4)

Vasily Galuzin
Vasily Galuzin

Reputation: 141

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();
    }

Upvotes: 2

Hamid Shatu
Hamid Shatu

Reputation: 9700

First of all, your date string is not correct, which should be

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

not this...

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

you placed Dot (.) in GMT+05.30 instead of Colon (:) as GMT+05:30.

Now, Use this format...

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

For Exmaple:

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

    try {
        date = formatter.parse(newdate);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String convertedDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(date);
    Log.i("ConvertedDate", convertedDate);

OutPut:

03-04 19:39:49.959: I/ConvertedDate(310): 04-03-2014 17:28

Upvotes: 0

Suwer
Suwer

Reputation: 260

Your date format is not correct. Try:

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

instead of:

"Tue Mar 04 16:58:00 GMT+05.30 2014";

It seems that SimplaDateFormat this GMT+05:30 part works only with ':'.

Than you can parse it like that:

    String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
    SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy" );
    Date  nd =  sdf.parse(newdate);

Upvotes: 0

Decoy
Decoy

Reputation: 1597

Try

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

Upvotes: 0

Related Questions