Why does Joda DateTime give a different result than Java Date?

I tested Joda DateTime against java.util.Datewith UTC timezone , and I came across an interesting case:

import org.joda.time.DateTime;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

public class Main {

    public static void main(String[] args) throws ParseException {
        String dt = "2011-06-11T12:00:00Z";
        String format = "yyyy-MM-dd'T'hh:mm:ss'Z'";

        DateFormat df = new SimpleDateFormat(format);
        df.setTimeZone(TimeZone.getTimeZone("UTC"));

        Date d = df.parse(dt);
        DateTime joda = new DateTime(dt);

        // Output Sat Jun 11 05:00:00 PDT 2011
        System.out.println(joda.toDate());

        // Output Fri Jun 10 17:00:00 PDT 2011
        System.out.println(d);
    }
}

I wonder if this is a mistake for one or did I miss something important here?

+4
source share
1 answer

(Edit: a clearer and more correct answer)

I believe this is because JODA and the Java date format handle “12” in a 12-hour presentation in different ways. One of them treats him like midnight, and the other treats him like noon.

Change your input to 2011-06-11T01:00:00Zto prove my hypothesis. Strike>

, JODA, DateTime, ISO, 24- : , JODA, DateFormat, Java-, hh , 12- . "12" -: 12- , 24 - .

- yyyy-MM-dd'T'HH:mm:ss'Z', 24- , .

+6

Source: https://habr.com/ru/post/1523462/


All Articles