You can convert the date to any format that is required for your JSON response.
In your case, you always used the default JSON date converter for fields like java.util.Date
. This is basically what is generated for you using Spring Roo. Take a look at your aspects of * _Roo_Json and you will find smth. eg:
public java.lang.String PizzaOrder.toJson() { return new JSONSerializer().exclude("*.class").serialize(this); }
Such an implementation uses the flexjson.transformer.BasicDateTransformer
class to convert the date for you. It runs as follows:
public class BasicDateTransformer extends AbstractTransformer { public void transform(Object object) { getContext().write(String.valueOf(((Date) object).getTime())); } }
You want to use another, more powerful transformer. Fortunately, it comes with your Roo and is called flexjson.transformer.DateTransformer
. Now, to format dates correctly, simply replace the default new transformer, for example. eg:
public java.lang.String PizzaOrder.toJson() { return new JSONSerializer().exclude("*.class") .transform(new DateTransformer("MM/dd/yyyy"), Date.class) .serialize(this); }
What all: -)
Know that you can also apply different Date
transformations (and not only) to different fields, such as:
transform(new DateTransformer("MM/dd/yyyy"), "person.birthday")
For more information on flexjson, see the FLEXJSON project page .
nowaq source share