jodatime LocalDate.now() 的意外结果

Unexpected result with jodatime LocalDate.now()

我正在使用 Jodatime for android 并得到了这个 :

LocalDate.now() //returns 2015-12-17, which is today
new LocalDate(LocalDate.now().toDate().getTime()); //returns 2015-12-16, which is yesterday

真是出乎意料!

是吗

我将日期存储在我的数据库中,并在以后创建它们,不过似乎没有任何问题。

This part 指定了那个点。

Get the date time as a java.util.Date. The Date object created has exactly the same year, month and day as this date. The time will be set to the earliest valid time for that date.

Converting to a JDK Date is full of complications as the JDK Date constructor doesn't behave as you might expect around DST transitions. This method works by taking a first guess and then adjusting the JDK date until it has the earliest valid instant. This also handles the situation where the JDK time zone data differs from the Joda-Time time zone data.

对于您的问题(如何使用 jodatime 存储我的日期?),您可以使用毫秒来存储您的日期。

这样试试;

public static void main(String[] args) {

    DateTime local = new DateTime();
    DateTime utc = new DateTime(DateTimeZone.UTC);

    System.out.println("local zone = " + local.getZone());
    System.out.println("  utc zone = " + utc.getZone());

    DateTimeFormatter format = DateTimeFormat.mediumDateTime();
    System.out.println(" local: " + format.print(local));
    System.out.println("   utc: " + format.print(utc));
    System.out.println("millis: " + utc.getMillis());
}

输出是;

local zone = America/Caracas
  utc zone = UTC
 local: 18.Ara.2015 05:06:00
   utc: 18.Ara.2015 09:36:00
millis: 1450431360816

终于找到答案了,在调用init的app方法中,再写一行就可以了:

JodaTimeAndroid.init(this);
DateTimeZone.setDefault(DateTimeZone.forTimeZone(TimeZone.getDefault()));

这样你就可以将 jodatime 的默认值设置为智能手机的默认值。

我真的很惊讶这在任何地方都找不到。