3

I found some kind of misbehavior in JodaTime when parsing a time instant.

 DateTimeFormatter dateStringFormat =
                DateTimeFormat.forPattern("EEE MMM dd kk:mm:ss 'GMT'Z yyyy");
        DateTime startTime = dateStringFormat.withOffsetParsed().
                parseDateTime("Tue Jan 01 09:30:00 GMT+2000 2013");
        DateTime endTime = dateStringFormat.withOffsetParsed().
                parseDateTime("Tue Jan 05 10:31:00 GMT+2000 2013");


        Period period = new Period(startTime, endTime);

        PeriodFormatter formatter = new PeriodFormatterBuilder().appendDays()
                .appendSuffix(" day ", " days")
                .appendHours()
                .appendSuffix(" hour ", " hours")
                .appendMinutes()
                .appendSuffix(" minute", " minutes")
                .toFormatter();


        System.out.println(formatter.print(period));

Output is: "1 hour 1 minute"
Where is the "days" part ?


Basil Bourque's version of this code…

The day-of-month is inexplicably being parsed as 01 rather than 05 in the second DateTime.

DateTimeFormatter dateStringFormat = DateTimeFormat.forPattern("EEE MMM dd kk:mm:ss 'GMT'Z yyyy");
DateTime startTime = dateStringFormat.withOffsetParsed().parseDateTime("Tue Jan 01 09:30:00 GMT+2000 2013");
DateTime endTime = dateStringFormat.withOffsetParsed().parseDateTime("Tue Jan 05 10:31:00 GMT+2000 2013");

Dump to console…

System.out.println( "startTime: " + startTime );
System.out.println( "endTime: " + endTime );

When run…

startTime: 2013-01-01T09:30:00.000+20:00
endTime: 2013-01-01T10:31:00.000+20:00
juan.facorro
  • 9,791
  • 2
  • 33
  • 41
Roman Mandeleil
  • 306
  • 4
  • 12
  • I have confirmed the problem. But you can delete all the Period and "formatter" code from the last half of your example code. The problem is in the parsing, where the date in both DateTime instances is 01 but the 2nd DateTime should be 05. I will paste simpler code into your question. You can delete or edit if you wish. – Basil Bourque Jan 13 '14 at 09:41
  • 2
    I can confirm both answers [by LaurentG](http://stackoverflow.com/a/21088329/642706) and [by PopoFibo](http://stackoverflow.com/a/21088302/642706) are correct. Changing `Tue` to `Sat` in the second DateTime fixes the problem. That date format is nasty! Stick with [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601) standard format whenever possible: `2013-01-05T10:31:00+20:00` – Basil Bourque Jan 13 '14 at 10:08

2 Answers2

3

Both dates are given as Tuesday. If you correct this and set the 5th January 2013 as a Saturday (as it was), then it works:

DateTime startTime = dateStringFormat.withOffsetParsed().parseDateTime("Tue Jan 01 09:30:00 GMT+2000 2013");
DateTime endTime = dateStringFormat.withOffsetParsed().parseDateTime("Sat Jan 05 10:31:00 GMT+2000 2013");

It looks like there is no way to check that the date is valid with Joda Time:

Community
  • 1
  • 1
LaurentG
  • 11,128
  • 9
  • 51
  • 66
2

This is happening because the endTime in this case is representing incorrect date - meaning Jan 05 2013 was not a Tuesday but was in fact a Saturday.

This works:

DateTimeFormatter dateStringFormat = DateTimeFormat.forPattern("EEE MMM dd kk:mm:ss 'GMT'Z yyyy");
DateTime startTime = dateStringFormat.withOffsetParsed().parseDateTime("Tue Jan 01 09:30:00 GMT+2000 2013");
DateTime endTime = dateStringFormat.withOffsetParsed().parseDateTime("Sat Jan 05 10:31:00 GMT+2000 2013");

System.out.println(dateStringFormat.print(startTime));
System.out.println(dateStringFormat.print(endTime));

Output:

Tue Jan 01 09:30:00 GMT+2000 2013 
Sat Jan 05 10:31:00 GMT+2000 2013

It looks like Joda time is using the day for parsing. Switching Tuesday to Saturday flips the date to Jan 05, regardless of the order within the formatting pattern

DateTimeFormatter dateStringFormat = DateTimeFormat.forPattern("MMM dd EEE kk:mm:ss 'GMT'Z yyyy");
DateTime endTime = dateStringFormat.withOffsetParsed().parseDateTime("Jan 01 Sat 10:31:00 GMT+2000 2013");
DateTime startTime = dateStringFormat.withOffsetParsed().parseDateTime("Jan 01 Tue 09:30:00 GMT+2000 2013");

Output:

Jan 01 Tue 09:30:00 GMT+2000 2013
Jan 05 Sat 10:31:00 GMT+2000 2013
StoopidDonut
  • 8,547
  • 2
  • 33
  • 51