3

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. A friend of mine tried submitting a form from his windows machine which produced

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:

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

how can I handle this in a pattern using jodatime?

EDIT:

Here is the form I am using in the 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)")) )
Chris Stewart
  • 1,641
  • 2
  • 28
  • 60

3 Answers3

2

The stringified version of the JavaScript date object (Date.prototype.toString) is implementation dependent, can vary greatly, and shouldn't be used.

The most robust way is to return the Coordinated Universal Time (UTC) from the client side. 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).

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:

... 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:

new DateTime(data.beginDate)

which'll produce the DateTime that you want.

It should all then be perfect whether your friend is in Tokyo or New York. :)

Community
  • 1
  • 1
bjfletcher
  • 11,168
  • 4
  • 52
  • 67
  • this did not work,I made the changes you wanted so my inputs are now populated with numbers like this `1435367479841` and I am still getting an error on submission for both of the fields on the form. I also changed the server side validation to be `"beginDate" -> jodaDate, "endDate" -> jodaDate` – Chris Stewart Jun 27 '15 at 01:15
  • also, here is the error list generated by the Play Framework `List(FormError(beginDate,List(error.date),List()), FormError(endDate,List(error.date),List()))` – Chris Stewart Jun 27 '15 at 01:25
  • lastly I am trying to bind these things to something of type `org.joda.time.DateTime` not just `jodaDate` as it says in the play framework. Not sure if this matters – Chris Stewart Jun 27 '15 at 01:27
  • Hi Chris, just had a look and my apologies, jodaTime mapper doesn't take in the epoch by default. I'll edit my answer. – bjfletcher Jun 27 '15 at 01:49
  • See edited answer, which I've tested to work here. :) Just replace jodaTime with longNumber then in your bindFromRequest code, do a new DateTime(data.beginDate) to produce the jodaDate object. – bjfletcher Jun 27 '15 at 01:58
  • It seems like there has to be a better answer to this question. The joda types are implemented in the Playframework for a reason. – Chris Stewart Jun 29 '15 at 13:45
1

You can use DateTimeFormatterBuilder for building a parser using aggregated formats. If one fails, it tries to parse it with the next one.

sinuhepop
  • 20,010
  • 17
  • 72
  • 107
0

You could 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 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.

Paul John
  • 1,626
  • 1
  • 13
  • 15
  • Well it is coming from the `new Date` function from javascript. Then placing that date into a input field using jquery ala `$("#dateInput").val(new Date())` – Chris Stewart Jun 24 '15 at 19:30
  • cool. so to parse it on the server side, you could use this method. Right? – Paul John Jun 24 '15 at 19:31
  • I cannot. I'm using a framework that builds forms. I need to have ONE 1 `org.joda.time.format.DateTimeFormat` that defines my pattern – Chris Stewart Jun 24 '15 at 19:36
  • Also kind of confused, it seems like the pattern I provided above should work since `z` represents the timezone explicitly spelled out or abbreviated. – Chris Stewart Jun 24 '15 at 19:38