I have a GAE app that uses the java.util.Calendar class.
My app changes some internal data when a day passes/
Currently I want to test the app, so I have a fake date data-member that I set and modify manually.
I want to save it to the datastore.
After about 2 hours of trying to save it, I decided to wrap it in a class I named FakeDate. it looks like this:
@PersistenceCapable
public class FakeDate {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Serialized
private Calendar _fakeCurrentDate;
The only problem is, the data of the _fakeCurrentDate is not saved.
At first I set the Calendar date to be 8/8/2012
When I call
pm.makePersistent(_currentDate);
It looks like everything is ok, but when I call
PersistenceManager pm = PMF.get().getPersistenceManager();
Query q = pm.newQuery(FakeDate.class);
try {
@SuppressWarnings("unchecked")
List<FakeDate> list = (List<FakeDate>) q.execute();
for (FakeDate f:list){
System.out.println(f.getDayOfMonth());
System.out.println(f.getMonth());
System.out.println(f.getYear());
}
} finally {
q.closeAll();
pm.close();
}
The FakeDate instance I get holds a Calendar instance of the date 27/9/2012. This is the default date I get when calling Calendar.getInstance()
It looks like I am missing something pretty easy.
Am I?
Calendarclass is not a supported field type for a datastore model. For details for the supported types take a look at Properties and Value Types. If you need to have a Date field in your model, declare it asjava.util.Dateand initialize it by usingCalendar.Hope this helps!