How do I convert a Joda Time DateTime object into a String in SQL Server format?

后端 未结 4 1043
悲&欢浪女
悲&欢浪女 2020-12-14 08:43

I am using the Joda-Time library in Java, and have a date and time stored as an org.joda.time.DateTime object.

How can I reliably convert this DateTime object into a

4条回答
  •  孤城傲影
    2020-12-14 09:11

    Be careful to consider time zones. Using new Timestamp() may be tricky since it expects time in milliseconds in GMT.

        DateTime dt = new DateTime(2010, 1, 1, 14, 30, 59, 1, DateTimeZone.forOffsetHoursMinutes(7, 0));
        Timestamp ts = new Timestamp(dt.getMillis());
        System.out.println(dt); // prints 2010-01-01T14:30:59.001+07:00
        System.out.println(ts); // prints 2010-01-01 08:30:59.001
    
        DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        String sqlTimeString = fmt.print(dt);
        System.out.println(sqlTimeString); // prints 2010-01-01 14:30:59
    

提交回复
热议问题