Emin
Emin

Reputation: 270

Java Date ISO8601

I have the the following class used with JPA and JAX-RS:

import java.util.Date;

public class UserModel implements Serializable{

    private Date created;

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }
}

When I access this class via Response resource I get the "created" in ISO8601 format:

2013-08-21T22:06:36+02:00

However when I access it in Java code (System.out.println) I get it in the following format:

Wed Aug 21 22:06:36 CEST 2013

Is there any way to get it always in ISO8601 format? Apparently for java automatically transforms Date format to the latter one.

Upvotes: 0

Views: 2299

Answers (1)

Emin
Emin

Reputation: 270

Here is how I solved it:

I have changed the following:

public Date getCreated() {
    return created;
}

to

public String getCreated() {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
    return df.format(created);
}

Upvotes: 2

Related Questions