Craig Smith
Craig Smith

Reputation: 66

joda time formatter parsing yyyy-mm-dd with extra hour

I'm using Joda time DateTimeFormatter to create a new datetime object in yyyy-mm-dd string format. When calling DateTime date = formatter.parse(String) I get a DateTime object with an extra hour. I live in UTC +1. How to format a string datetime without hours added.

 String date = "2013-01-28";

 DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd");
 DateTime date = formatter.parseDateTime(date);
 date.ToString() = 2013-01-28T00:01:00.000+01:00
 expected = 2013-01-28T00:00:00.000+01:00

Additionally, later in the code I compare two DateTime objects. This parser is in yymmdd format and it does not add one hour.

String date = "130102"

 DateTimeFormatter format = DateTimeFormat.forPattern("yyMMdd");
 DateTime datetime = format.parseDateTime(date);
 datetime.toString = 2013-01-02T00:00:00.000+01:00

Upvotes: 1

Views: 762

Answers (1)

Meno Hochschild
Meno Hochschild

Reputation: 44071

yyyy-mm-dd uses the minute-of-hour, not the month (pattern symbol M). Please refer to the documentation of pattern symbols on Joda-Time-page.

Parsing "2013-01-28" with your wrong pattern yields "2013-01-28T00:01:00.000+01:00". Do you see the minute equal to 1? And the month of January seems to be a default value if the parser cannot find a month information (in my opinion not smart).

Upvotes: 1

Related Questions