Joda datetime handles both abbreviation and explicit time intervals

I am using this template to try and process a datetime view from a client:

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

      

and this works for me on my local ubuntu machine. A friend of mine tried to imagine a form from his window machine, which produced

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

      

as a timestamp. This is obviously different from the template I have - but I need to be able to handle the formatting above AND post dates like this on my ubuntu machine:

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

      

how can I handle this in a template using jodatime?

EDIT:

Here is the form I'm using in Playframework - it might be relevant.

  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)")) )

      

+3


source to share


3 answers


The string version of the JavaScript ( Date.prototype.toString

) date object is implementation dependent, can vary greatly, and should not be used.

The most reliable way is to return Coordinated Universal Time (UTC) from the client side. It also won't have time zone issues. Replace with the following:

new Date()

      

from:

new Date().getTime();    

      

(see this SO thread for more information on this line).

Then you can take the epoch as Long:



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

      

You can add verifying

to test this if you like, for example:

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

      

Then in your bindFromRequest

(or wherever you get the data) you can do the following:

new DateTime(data.beginDate)

      

which will create the DateTime

one you want.

Then everything should be perfect, whether your friend will be in Tokyo or New York. :)

+2


source


You can use DateTimeFormatterBuilder to create a parser using aggregated formats. If someone fails, they try to take it apart next.



+1


source


You can use Apache DateUtils Parsedate method -

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 a date, we need to be clear about the format in which it will appear. Thus, parseDate with a list of patterns can be used to format the date into a string.

0


source







All Articles