0

I have the following situation:
To plan an activity that started on a particular date and ends on the next day, an end-date/time value is provided exceeding the 24u hour constraint.
For e.g: start-date = 2013-01-01 23:22:00 and end-date = 2013-01-01 26:22:00

I thought the solution was, using the lenient methods provided by JodaTime. But it behaved not the way I expected. So I provided some test-code to, explain my confusion:

public static void main(String[] args){

    String lenientDate = "2013-01-01 26:22:00";
    String format = "YYYY-MM-DD hh:mm:ss";
    try{

            System.out.println("Lenient Date:" + lenientDate);
            DateFormat sdf = new SimpleDateFormat(format);
            sdf.setLenient(true);
            System.out.println("Java.Date output:" + sdf.parse(lenientDate));

            DateTime dt = new DateTime(sdf.parse(lenientDate));
            System.out.println("hybrid calls:" + dt.toString());

            DateTimeFormatter parseFormat = DateTimeFormat.forPattern(format);
            Chronology lenient = LenientChronology.getInstance(GregorianChronology.getInstance());
            parseFormat = parseFormat.withChronology(lenient);

            DateTime dateTime = parseFormat.parseDateTime(lenientDate);

            System.out.println("All Joda calls:" + dateTime.toString());

            System.out.println("My expected values: " + dateTime.minusHours(12).toString());

    }catch(Exception e){
        System.out.println("Something went wrong!");
    }

} 

And the outcome is:

Lenient Date:2013-01-01 26:22:00
Java.Date output:Mon Dec 31 02:22:00 CET 2012
hybrid calls:2012-12-31T02:22:00.000+01:00
All Joda calls:2013-01-02T14:22:00.000+01:00
My expected values: 2013-01-02T02:22:00.000+01:00

Library: Joda 2.2; Java: JDK 1.7; Platform: Mac OSX

Can somebody explain the right behaviour of Leniency in date/time conversions?

4

1 回答 1

0

You have invalid date pattern at "hh:mm:ss". Date format should be:

String format = "yyyy-MM-dd HH:mm:ss";    

DateTimeFormat documentation

h - clockhour of halfday (1~12)
H - hour of day (0~23)

With this date pattern all examples works as expected

于 2013-09-24T12:40:17.943 回答