从org.joda.time.LocalDateTime转换为java.sql.date

从org.joda.time.LocalDateTime转换为java.sql.date,java,datetime,jodatime,java-time,localdatetime,Java,Datetime,Jodatime,Java Time,Localdatetime,我有这个密码 LocalDateTime localDateTime = LocalDateTime.parse("1777-11-11T00:00:00.000"); System.out.println("DateTimeZone.getDefault default" + DateTimeZone.getDefault()); System.out.println("Milliseconds UTC" + localDateTim

我有这个密码

LocalDateTime localDateTime = LocalDateTime.parse("1777-11-11T00:00:00.000");
System.out.println("DateTimeZone.getDefault default" + DateTimeZone.getDefault());
System.out.println("Milliseconds UTC" + localDateTime.toDateTime(DateTimeZone.UTC).getMillis());
System.out.println("Milliseconds Europe rome" + localDateTime.toDateTime(DateTimeZone.forID("Europe/Rome")).getMillis());
java.sql.Timestamp t = new java.sql.Timestamp(localDateTime.toDateTime(DateTimeZone.forID("Europe/Rome")).getMillis());
System.out.println("End Date: "+  new java.sql.Timestamp(localDateTime.toDateTime(DateTimeZone.forID("Europe/Rome")).getMillis()));
        
其中,我将日期从org.joda.time.LocalDateTime转换为java.sql.date

输出是

DateTimeZone.getDefault defaultEurope/Rome
Milliseconds UTC   -6063292800000
Milliseconds Europe rome  -6063295796000
End Date: 1777-11-11 00:10:04.0
所以分和秒是错误的。 为什么会这样?我怎样才能解决这个问题

谢谢

所以分和秒是错误的。为什么会这样

发生这种情况是因为在意大利直到1893-10-31T23:49之前,区域偏移量分别为
49
分钟和
56

查看更多信息

此外,请注意以下有关的事实:

应用时区时,本地日期时间可能受以下因素影响: 夏时制。在夏时制间隙中,当当地时间 不存在,此方法将引发异常。夏时制 重叠,当相同的本地时间出现两次时,此方法返回 当地时间的第一次出现

下面的代码演示:

  • 直到
    1893-10-31T23:49的
    LocalDateTime
    将转换为
    DateTime
    ,偏移量为
    49
    分钟和
    56
  • 1893-10-31T23:50到
    1893-10-31T23:59的
    LocalDateTime
    将引发上述异常
  • 1893-11-01T00:00开始的
    LocalDateTime
    将转换为
    DateTime
    ,偏移量为
    +01:00
    小时
  • 演示:

    import org.joda.time.DateTimeZone;
    import org.joda.time.LocalDateTime;
    
    public class Main {
        public static void main(String[] args) {
            // Test date-time strings
            String[] arr = { "1893-10-31T23:49:00.000", "1893-11-01T00:00:00.000", "1893-10-31T23:50:00.000" };
            
            for (String dt : arr) {
                LocalDateTime localDateTime = LocalDateTime.parse(dt);
                System.out.println(localDateTime);
                System.out.println(localDateTime.toDateTime(DateTimeZone.forID("Europe/Rome")));
            }
        }
    }
    
    1893-10-31T23:49:00.000
    1893-10-31T23:49:00.000+00:49:56
    1893-11-01T00:00:00.000
    1893-11-01T00:00:00.000+01:00
    1893-10-31T23:50:00.000
    Exception in thread "main" org.joda.time.IllegalInstantException: Illegal instant due to time zone offset transition (daylight savings time 'gap'): 1893-10-31T23:50:00.000 (Europe/Rome)
        at org.joda.time.chrono.ZonedChronology.localToUTC(ZonedChronology.java:157)
        at org.joda.time.chrono.ZonedChronology.getDateTimeMillis(ZonedChronology.java:122)
        at org.joda.time.chrono.AssembledChronology.getDateTimeMillis(AssembledChronology.java:133)
        at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:257)
        at org.joda.time.DateTime.<init>(DateTime.java:532)
        at org.joda.time.LocalDateTime.toDateTime(LocalDateTime.java:753)
        at Main.main(Main.java:12)
    
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    
    public class Main {
        public static void main(String[] args) {
            // Test date-time strings
            String[] arr = { "1893-10-31T23:49:00.000", "1893-11-01T00:00:00.000", "1893-10-31T23:50:00.000" };
            ZoneId zoneId = ZoneId.of("Europe/Rome");
    
            for (String dt : arr) {
                LocalDateTime localDateTime = LocalDateTime.parse(dt);
                System.out.println(localDateTime);
                System.out.println(localDateTime.atZone(zoneId));
            }
        }
    }
    
    1893-10-31T23:49
    1893-10-31T23:49+00:49:56[Europe/Rome]
    1893-11-01T00:00
    1893-11-01T00:00+01:00[Europe/Rome]
    1893-10-31T23:50
    1893-11-01T00:00:04+01:00[Europe/Rome]
    
    输出:

    import org.joda.time.DateTimeZone;
    import org.joda.time.LocalDateTime;
    
    public class Main {
        public static void main(String[] args) {
            // Test date-time strings
            String[] arr = { "1893-10-31T23:49:00.000", "1893-11-01T00:00:00.000", "1893-10-31T23:50:00.000" };
            
            for (String dt : arr) {
                LocalDateTime localDateTime = LocalDateTime.parse(dt);
                System.out.println(localDateTime);
                System.out.println(localDateTime.toDateTime(DateTimeZone.forID("Europe/Rome")));
            }
        }
    }
    
    1893-10-31T23:49:00.000
    1893-10-31T23:49:00.000+00:49:56
    1893-11-01T00:00:00.000
    1893-11-01T00:00:00.000+01:00
    1893-10-31T23:50:00.000
    Exception in thread "main" org.joda.time.IllegalInstantException: Illegal instant due to time zone offset transition (daylight savings time 'gap'): 1893-10-31T23:50:00.000 (Europe/Rome)
        at org.joda.time.chrono.ZonedChronology.localToUTC(ZonedChronology.java:157)
        at org.joda.time.chrono.ZonedChronology.getDateTimeMillis(ZonedChronology.java:122)
        at org.joda.time.chrono.AssembledChronology.getDateTimeMillis(AssembledChronology.java:133)
        at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:257)
        at org.joda.time.DateTime.<init>(DateTime.java:532)
        at org.joda.time.LocalDateTime.toDateTime(LocalDateTime.java:753)
        at Main.main(Main.java:12)
    
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    
    public class Main {
        public static void main(String[] args) {
            // Test date-time strings
            String[] arr = { "1893-10-31T23:49:00.000", "1893-11-01T00:00:00.000", "1893-10-31T23:50:00.000" };
            ZoneId zoneId = ZoneId.of("Europe/Rome");
    
            for (String dt : arr) {
                LocalDateTime localDateTime = LocalDateTime.parse(dt);
                System.out.println(localDateTime);
                System.out.println(localDateTime.atZone(zoneId));
            }
        }
    }
    
    1893-10-31T23:49
    1893-10-31T23:49+00:49:56[Europe/Rome]
    1893-11-01T00:00
    1893-11-01T00:00+01:00[Europe/Rome]
    1893-10-31T23:50
    1893-11-01T00:00:04+01:00[Europe/Rome]
    
    输出:

    import org.joda.time.DateTimeZone;
    import org.joda.time.LocalDateTime;
    
    public class Main {
        public static void main(String[] args) {
            // Test date-time strings
            String[] arr = { "1893-10-31T23:49:00.000", "1893-11-01T00:00:00.000", "1893-10-31T23:50:00.000" };
            
            for (String dt : arr) {
                LocalDateTime localDateTime = LocalDateTime.parse(dt);
                System.out.println(localDateTime);
                System.out.println(localDateTime.toDateTime(DateTimeZone.forID("Europe/Rome")));
            }
        }
    }
    
    1893-10-31T23:49:00.000
    1893-10-31T23:49:00.000+00:49:56
    1893-11-01T00:00:00.000
    1893-11-01T00:00:00.000+01:00
    1893-10-31T23:50:00.000
    Exception in thread "main" org.joda.time.IllegalInstantException: Illegal instant due to time zone offset transition (daylight savings time 'gap'): 1893-10-31T23:50:00.000 (Europe/Rome)
        at org.joda.time.chrono.ZonedChronology.localToUTC(ZonedChronology.java:157)
        at org.joda.time.chrono.ZonedChronology.getDateTimeMillis(ZonedChronology.java:122)
        at org.joda.time.chrono.AssembledChronology.getDateTimeMillis(AssembledChronology.java:133)
        at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:257)
        at org.joda.time.DateTime.<init>(DateTime.java:532)
        at org.joda.time.LocalDateTime.toDateTime(LocalDateTime.java:753)
        at Main.main(Main.java:12)
    
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    
    public class Main {
        public static void main(String[] args) {
            // Test date-time strings
            String[] arr = { "1893-10-31T23:49:00.000", "1893-11-01T00:00:00.000", "1893-10-31T23:50:00.000" };
            ZoneId zoneId = ZoneId.of("Europe/Rome");
    
            for (String dt : arr) {
                LocalDateTime localDateTime = LocalDateTime.parse(dt);
                System.out.println(localDateTime);
                System.out.println(localDateTime.atZone(zoneId));
            }
        }
    }
    
    1893-10-31T23:49
    1893-10-31T23:49+00:49:56[Europe/Rome]
    1893-11-01T00:00
    1893-11-01T00:00+01:00[Europe/Rome]
    1893-10-31T23:50
    1893-11-01T00:00:04+01:00[Europe/Rome]
    
    了解有关java.time API的更多信息


    *无论出于何种原因,如果您必须坚持使用Java 6或Java 7,您都可以使用哪些后端端口将大部分Java.time功能移植到Java 6和7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。

    我建议您既不要使用
    java.sql.Date
    也不要使用
    java.sql.Timestamp
    。这两个类的设计都很糟糕,而且早已过时。例如,使用来自的
    LocalDate
    LocalDateTime
    。此外,java.time已经取代了Joda-time,因此对于更简单和现代的代码,请全部使用java.time。那么您可能根本不需要任何转换。