-
Notifications
You must be signed in to change notification settings - Fork 18
Date and Calendar Support
If you really really want to use Date or Calendar, file an issue BUT PLEASE read the below first as it will save you many headaches of using java's old Date and time apis....
Many people already know about the problems that plague Date and Calendar in the jdk. There are many bug reports on both classes. It has been so prevalent that JSR 310 exists based on joda-time to replace them and it was targeted to go into jdk7, but slipped. So, PlayOrm supports
- LocalDate - just a date with no time
- LocalTime - just a time with no date
- LocalDateTime - A Date and time with no timezone
- DateTime - A Date and time with timezone
We highly advise you to use these instead!!! With joda-time, java finally has a library matching C#'s beautiful Date and Time apis where java has always been behind. I am by no means a huge C# fan, but they did their Date and Time apis very well. Java's Date and Time apis have sucked up until the creation of joda-time.
As of yet, we are storing DateTime in GMT format. For LocalDateTime there is no timezone information so we store in that local time format. To save timezones, you need a separate field. Instead store a LocalDateTime or DateTime in GMT+0 and store a timezone. You can query using timezones but the objects you get back the DateTime will be in GMT and you can convert it back to your timezone using the separate field.
Example:
Please note that we are using ISODateTimeFormat so you need to give in that format to query date types.
Let us suppose you have an entity Activity with all the Date types as below:
@NoSqlEntity
public class Activity {
@NoSqlId(usegenerator=false)
private String id;
@NoSqlIndexed
private LocalDateTime localDateTime;
@NoSqlIndexed
private DateTime dateTime;
@NoSqlIndexed
private LocalDate localDate;
@NoSqlIndexed
private LocalTime localTime;
}
Then you can create some object like this:
Activity act = new Activity();
act.setId("act");
act.setLocalDateTime(new LocalDateTime());
act.setDateTime(new LocalDateTime());
act.setLocalDate(new LocalDate());
act.setLocalTime(new LocalTime());
mgr.put(act);
mgr.flush();
Then you can do some SQL query like this on command prompt:
For LocalDateTime:
select * from Activity where localDateTime >= "2013-01-03T18:03:03.554+05:30";
For DateTime:
select * from Activity where dateTime >= "2013-01-03T18:03:03.554+05:30";
For LocalDate:
select * from Activity where localDate = "2013-01-03";
For LocalTime:
select * from Activity where localTime > "18:03:03.554+05:30";
And also some JQL like below:
Write a named query on Activity.
@NoSqlQuery(name="findByLocalDateTime", query="select * from TABLE as e where e.date = :date")
Then, in your code, use the above query as below:
LocalDateTime t = LocalDateTime.now();
Query<Activity> query = mgr.createNamedQuery(Activity.class, "findByLocalDateTime");
query.setParameter("date", t);
List<Activity> result = query.getResultList(0, null);