user590586
user590586

Reputation: 3050

JPA timestamp column and net.sf.json result from query

Inside my entity class I have a column of type timestamp:

@Column(name = "TESTD")
@Temporal(TemporalType.TIMESTAMP)    
private Date test_date;

Inside my session bean I'm creating a select query which return a resultList, and then I'm creating from it a json object:

Query query = em.createNamedQuery("findAllTest");                                               
List<entityClass> results = query.getResultList();  
JSONSerializer.toJSON((List)results ,jsonConfig);

when creating the json object I want the timestamp column to be formatted (and not to return as object). How can this be done? how can I cast/format the timestamp column according to the date format I want? what is the best way to do this?

Upvotes: 0

Views: 588

Answers (2)

chrislhardin
chrislhardin

Reputation: 1745

I create a second transient getter that returns the date as a String formatted how I want. It's a bit if a hack but works.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691865

I guess you're using json-lib, based on the code sample, and I've never used it, but the javadoc shows that JsonConfig provides the following method:

public void registerJsonValueProcessor(Class propertyType,
                                       JsonValueProcessor jsonValueProcessor)

Registers a JsonValueProcessor.
[Java -> JSON] 

So I guess you could use that method, and register a processor for Calendar.class that would transform the Calendar object into a String using the format you want..

Upvotes: 1

Related Questions