简体   繁体   English

joda datetime处理首字母缩略词和显式时区

[英]joda datetime handle both acronym and explicit timezones

I'm using this pattern to try and handle a datetime submission from a client: 我正在使用此模式尝试处理来自客户端的日期时间提交:

E MMM dd yyyy HH:mm:ss 'GMT'Z (z)

and this works for me on my local ubuntu machine. 这对我在我当地的ubuntu机器上工作。 A friend of mine tried submitting a form from his windows machine which produced 我的一个朋友试图从他生产的Windows机器上提交一份表格

Wed Jun 24 2015 13:34:22 GMT-0500 (Central Daylight Time)

as the timestamp. 作为时间戳。 This is obviously different than the pattern that I have - but I need to be able to handle the formatting above AND accomodate for dates like this on my ubuntu machine: 这明显不同于我的模式 - 但我需要能够处理上面的格式并在我的ubuntu机器上适应这样的日期:

Wed Jun 24 2015 13:42:03 GMT-0500 (CDT)

how can I handle this in a pattern using jodatime? 我如何使用jodatime处理这种模式?

EDIT: 编辑:

Here is the form I am using in the Playframework - it might be relevant. 这是我在Playframework中使用的表单 - 它可能是相关的。

  val form  = Form(mapping(
    "beginDate" -> jodaDate("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)"),
    "endDate" -> jodaDate("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)")) )

The stringified version of the JavaScript date object ( Date.prototype.toString ) is implementation dependent, can vary greatly, and shouldn't be used. JavaScript日期对象( Date.prototype.toString )的字符串化版本是依赖于实现的,可以有很大差异,不应该使用。

The most robust way is to return the Coordinated Universal Time (UTC) from the client side. 最强大的方法是从客户端返回协调世界时(UTC)。 This will not have any timezone issue either. 这也没有任何时区问题。 Replace the following: 替换以下内容:

new Date()

with: 有:

new Date().getTime();    

(see this SO thread for more about that line). (有关该行的更多信息,请参阅此SO线程 )。

Then you can take in the epoch as a Long: 然后你可以把这个时代作为一个龙:

val form  = Form(mapping(
  "beginDate" -> longNumber,
  "endDate" -> longNumber
)

You can add verifying to verify it if you like, for example: 如果您愿意,可以添加verifying以进行验证,例如:

... longNumber.verifying("Invalid date", _ > DateTime.now().minusYears(2).getMillis)

Then in your bindFromRequest (or wherever it is you're taking in the data), you can do the following: 然后在bindFromRequest (或者您正在接收数据的任何地方),您可以执行以下操作:

new DateTime(data.beginDate)

which'll produce the DateTime that you want. 这将产生你想要的DateTime

It should all then be perfect whether your friend is in Tokyo or New York. 无论你的朋友在东京还是在纽约,它都应该是完美的。 :) :)

You can use DateTimeFormatterBuilder for building a parser using aggregated formats. 您可以使用DateTimeFormatterBuilder来使用聚合格式构建解析器。 If one fails, it tries to parse it with the next one. 如果一个失败,它会尝试用下一个解析它。

You could use Apache DateUtils Parsedate method - 你可以使用Apache DateUtils Parsedate方法 -

https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html#parseDate%28java.lang.String,%20java.lang.String[]%29 https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html#parseDate%28java.lang.String,%20java.lang.String []%29

To format date we need to have a fair idea of the format it would come in. So parseDate with a list of patterns can be used to format a date to a string. 要格式化日期,我们需要对它的格式有一个合理的了解。所以带有模式列表的parseDate可用于将日期格式化为字符串。

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

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