日期时间和简单日期时间格式结果不同

Date time and simple date time format results are different

我有以下代码片段:

        DateTime dateTime = new DateTime().withZone(DateTimeZone.forID("America/Chicago"))
                .withYear(2016)
                .withMonthOfYear(8)
                .withDayOfMonth(25)
                .withHourOfDay(12)
                .withMinuteOfHour(37);

        System.out.println("DateTime: ");
        System.out.println(dateTime.toDate().getTime());

        String str = "2016-8-25 12:37 AM CST";
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-M-dd hh:mm a z");

        System.out.println("SDF: ");
        try {
            System.out.println(sdf.parse(str).getTime()+"");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

根据我的理解,两者代表相同的日期(因此纪元也应该相同)。但是我得到的结果是:

DateTime: 
1472146669119
SDF: 
1451198220000

如果我将时区更改为 CDT,那么我会得到:

DateTime: 
1472146668746
SDF: 
1451194620000

所以我希望有好心人能开导我(帮助这个可怜的灵魂)。

谢谢

更新:

我正在使用以下修改后的代码:

        DateTime dateTime = new DateTime(2016, 8, 25, 12, 37, DateTimeZone.forID("America/Chicago"));

        System.out.println("DateTime: ");
        System.out.println(dateTime.toDate().getTime());

        String str = "2016-8-25 12:37 AM CDT";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm a z");

        System.out.println("SDF: ");
        try {
            System.out.println(sdf.parse(str).getTime()+"");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

现在我得到:

DateTime: 
1472146620000
SDF: 
1472103420000

这里:

System.out.println(sdf.parse(str).getTime()+"");

您正在使用不包括秒数的格式化字符串,并将其解析回日期。有关它是哪一秒的信息丢失,因此时间四舍五入为:12:37:00.

在您发布的第二段代码中,这个 date/time:

new DateTime(2016, 8, 25, 12, 37, DateTimeZone.forID("America/Chicago"));
//                         ^ - midday

...是 2016 年 8 月 25 日中午过后 37 分钟。

这个日期:

String str = "2016-8-25 12:37 AM CDT";

...是 2016 年 8 月 25 日午夜后 37 分钟。

对于匹配的日期,我希望第一个应该是:

new DateTime(2016, 8, 25, 0, 37, DateTimeZone.forID("America/Chicago"));
//                        ^ - midnight