简体   繁体   English

网址兼容且易于解析的日期时间格式?

[英]URL-compatible and easy to parse date-time format?

Using Play 2 I want to create a REST API, which shall include 我想使用Play 2创建一个REST API,其中应包括

/resource/<startDateTime>

meaning return all items of resource with a startDateTime greater than the startDateTime given in the URL. 意思是返回startDateTime大于URL中给定的startDateTime所有资源项。

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. 因此,现在我需要一些DateTime格式,该格式可以通过URL以人类可读的格式传递,并且仍然很容易解析为Play 2控制器内部的Java Date对象。 Any hints / best practices on that? 有任何提示/最佳做法吗? Thanks for any hint! 感谢您的提示!

Update: Even better would be if Play would do the parsing for me. 更新:如果Play可以帮我解析,那就更好了。 For java.util.Date in the routes configuration I am getting the error 对于路由配置中的java.util.Date我遇到了错误

 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 eg 预期的输入:例如

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. 否则,易于阅读-我不在乎,只要它可以使用URL进行传输并且很容易解析为Date对象即可。

There is an ISO standard for dates, number 8601. 日期有一个ISO标准,编号8601。

http://en.wikipedia.org/wiki/ISO_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. 日期和时间值的排列顺序从最高到最低:年,月(或周),日,时,分,秒和秒的一部分。

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 . 我认为用Java处理日期的最佳库是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. 为此,您需要定义自己的QueryStringBindable。 Look at this answer about Doubles for an example. 看看这个关于双打的答案

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 您可以在此处检查原生Play2路径活页夹: 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 ) 但是您可以使用ISO 8601格式在DateTime(JodaTime)之上编写自己的PathBinder(使用ISODateTimeFormat

I think it will be a good Pull request ;) 我认为这将是一个很好的Pull请求;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM