Chris Stewart
Chris Stewart

Reputation: 1689

Not parsing an hour from a joda DateTime

I'm trying to parse a string that contains a DateTime

  def parseDateTime(str : String) : DateTime = {
    //need to parse date time of this format
    //2015-05-22T05:10:00.305308666Z

    DateTime.parse(str,DateTimeFormat.forPattern(dateTimePattern))
  }

  def dateTimePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSZ"

and here is my test case trying to parse the date time

 "MarshallerUtil" must "parse a date time correctly from blockcypher" in {
    val str = "2015-05-22T05:10:00.305308666Z"
    val dateTime = parseDateTime(str)
    dateTime.getYear must be (2015)
    dateTime.getMonthOfYear must be (DateTimeConstants.MAY)
    dateTime.getDayOfMonth must be (22)
    dateTime.getHourOfDay must be (5)
    dateTime.getMinuteOfHour must be (10)
  }

and it fails to get the correct hour

[info] - must parse a date time correctly from blockcypher *** FAILED ***
[info]   0 was not equal to 5 (MarshallerUtilTest.scala:17)

What is incorrect on my pattern?

Upvotes: 0

Views: 112

Answers (1)

James
James

Reputation: 2764

That's because it parses the date time as UTC and when you invoke the getHourOfDay, it returns the time unit with local timezone. For example the same program printed '10' here, because my local timezone is '+05:30' and so, 05:10 and a 05:30 is 10:40. I hope this helps.

Update:

Z is a placeholder/matcher that is used in the date time pattern to match a timezone. A timezone has the form '+HH:mm' or '-HH:mm', for example '+05:30' means that the timezone is 5 hours and 30 mins ahead of the UTC time.

Upvotes: 1

Related Questions