me.at.coding
me.at.coding

Reputation: 17744

URL-compatible and easy to parse date-time format?

Using Play 2 I want to create a REST API, which shall include

/resource/<startDateTime>

meaning return all items of resource with a startDateTime greater than the startDateTime given in the URL.

So now I need some DateTime format, that can be passed by an URL in a human-readable format and is still easy to parse into a Java Date object inside my Play 2 controller. Any hints / best practices on that? Thanks for any hint!

Update: Even better would be if Play would do the parsing for me. For java.util.Date in the routes configuration I am getting the error

 No QueryString binder found for type java.util.Date. Try to implement an implicit QueryStringBindable for this type. 

Is there anything predefined to parse a Date?

Update:

Expected input: Could be e.g.

http://site.com/resource/20121231-141557 # 2012/12/31 14:15:57

or sth. else, easy readable - I don't care as long as it can be transfered using an URL and is easy to parse into a Date object.

Upvotes: 1

Views: 1047

Answers (3)

Julien Lafont
Julien Lafont

Reputation: 7877

You can check native Play2 Path binders here : https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/play/api/mvc/Binders.scala#L251

Currently, there is nothing to handle Date in parameters.

But you can write your own PathBinder on top of DateTime (JodaTime), with the ISO 8601 format (use ISODateTimeFormat)

I think it will be a good Pull request ;)

Upvotes: 0

Kim Stebel
Kim Stebel

Reputation: 42047

It seems you have two questions here:

  1. How to format and parse dates easily? I think the best library for handling dates in java is Joda Time. It has methods for formatting and parsing dates in different formats.
  2. How to define a route with a custom parser? For that, you need to define your own QueryStringBindable. Look at this answer about Doubles for an example.

Upvotes: 0

Alexander Kj&#228;ll
Alexander Kj&#228;ll

Reputation: 4336

There is an ISO standard for dates, number 8601.

http://en.wikipedia.org/wiki/ISO_8601

Date and time values are organized from the most to the least significant: year, month (or week), day, hour, minute, second, and fraction of second.

Upvotes: 1

Related Questions