Frage

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.

War es hilfreich?

Lösung

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

Andere Tipps

Try

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

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

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top