LuxuryMaster LuxuryMaster - 1 month ago 13
Java Question

Java - GAE Datastore persisting Date with specific format

I have a Google app engine JDO datastore entity with a date field and it looks like the following.

@PersistenceCapable
public class Test {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

@Persistent
private Date dateTime;

}


The problem I'm having is it defaults to datetime (2014-08-30 00:00:00.000Z) format which is not how I want to store the date. I want to define specific format 'yyyy-MM-dd'.

Looks like there's support for python to do this. However, I can't find similar documentation for Java.

How can I change the datastore entity definition to support what I'm trying to accomplish?

Answer

Datastore will always store dates as UTC along with the time and tzinfo=None included. There is no method to override this on Java neither on Pyhton as explained here [1].

The exception is that in Python there is a class that will retrieve the "raw" date from Datastore and display the date part of it. This won't prevent Datastore from storing the date with the full format.

In short, all you can do is ignore the time data and manipulate the data retrieved from Datastore with the aproppiate functions [2] to get only the date.

[1] https://cloud.google.com/appengine/docs/python/datastore/typesandpropertyclasses#datetime

[2] http://docs.oracle.com/javase/7/docs/api/java/util/Date.html