ivae
ivae

Reputation: 37

Android Use SimpleDateFormat

I get a data from server like this:

data = "2015-04-24T23:00:17+08:00"

I am in UTC+8,I want get

data = "2015-04-24 23:00:17"

I program this

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Data = formatter.parse(data);

But I get that

Fri Apr 24 23:00:17 格林尼治标准时间+0800 2015

How can I correct it?

if I use

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M-dd HH:mm:ss",Locale.ENGLISH);

I get

Fri Apr 24 23:00:17 GMT+08:00 2015

It is not I want

Upvotes: 2

Views: 170

Answers (3)

Vishnu Prabhu
Vishnu Prabhu

Reputation: 459

Once you parse the date you returned from server, you have a java.util.Date object. If you want to convert to your own format you have to again format the date object.

private String getDate(String serverDate) {
    SimpleDateFormat serverFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    try {
        Date date = serverFormatter.parse(serverDate);

        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = myFormatter.format(date);

        return formattedDate;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

Upvotes: 0

shkschneider
shkschneider

Reputation: 18243

You are missing a Locale.

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

See the documentation.

Upvotes: 2

Tarun
Tarun

Reputation: 13808

For your desired output you need to change the date format to

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

Upvotes: 0

Related Questions