0

My code:

  val pattern = "MM-dd-yy"
  val t = DateTime.parse("07-01-86", DateTimeFormat.forPattern(pattern)).toDateTime(DateTimeZone.forID("GMT"))
  val z = t.getMillis.asInstanceOf[Long]
  println("ms= "+z) // expected output: 520560000000  actual output: 520578000000

Several online GMT date converters give a different millis output than DateTime. Anyone know why?

Greg
  • 10,696
  • 22
  • 68
  • 98

1 Answers1

0

In your solution your local time zone is implicitly used when parsing the date time. You should use

val t = DateTime.parse("07-01-86", DateTimeFormat.forPattern(pattern).withZoneUTC())

to force the DateTime to be created in the UTC zone. Then, the millis is 520560000000. No need to execute toDateTime(DateTimeZone) on it any more.

Otherwise, with your construction

val t = DateTime.parse("07-01-86", DateTimeFormat.forPattern(pattern)).toDateTime(DateTimeZone.forID("GMT"))

the DateTime will be first created in your local TZ (ie. midnight of 07-01-86 in your TZ) and then "cast" to UTC, but preserving the timestamp (ie. it will be the same timestamp, but interpreted in UTC, so the time part and the day part will change depending on your local TZ offset).

Example (my TZ is +02:00):

DateTime.parse("07-01-86", DateTimeFormat.forPattern(pattern)) // --> 1986-07-01 00:00 (+02:00)
.toDateTime(DateTimeZone.forID("GMT"))                         // --> 1986-06-30 22:00 (UTC)

I assumed you are OK with using UTC over GMT but there's also

DateTimeFormat.forPattern(pattern).withZone(...)

where you can provide your desired zone.

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102