Saturday, June 4, 2011

Jena and DateTime

Before you start storing timestamp literals in your triple store, consider the value of typed literals.  Typed literals can be easily converted to their native language type, in this case - java.util.Calendar.  Furthermore, SPARQL lets you do things like compare and filter on date values. 

Here is a quick JUnit test to illustrate using Jena to create/read triples with dateTime typed literals, as well as using ARQ to query for dateTime literals.



Some notes on the SPARQL (not shown above):
- A dateTime literal can be used in SORT expressions, e.g. ORDER BY ASC(?date)
- A dateTime literal can be used in FILTER expressions with comparisons such as FILTER ( ?date >= "$someDateTime"^^xsd:dateTime
- Note that in SPARQL, you explicitly type the literal. You should define a PREFIX with xsd: so you can use the short hand

Using xsd:dateTime is a good choice because the API translates to java.util.Calendar, of which you can easily do things like get your hands on a java.util.Date, automatically handle time zones, etc.

No comments:

Post a Comment