Reputation: 8494
I'm using Jodatime for android and got this :
LocalDate.now() //returns 2015-12-17, which is today
new LocalDate(LocalDate.now().toDate().getTime()); //returns 2015-12-16, which is yesterday
That's really unexpected !
Is that
I store dates in my database as long and create them later, there doesn't seem to be any problem though.
Upvotes: 1
Views: 849
Reputation: 8494
I finally got the answer, in the app method where you call init, you just have to write one more line :
JodaTimeAndroid.init(this);
DateTimeZone.setDefault(DateTimeZone.forTimeZone(TimeZone.getDefault()));
This way you set the default of jodatime to the default of your smartphone.
I'm really surprised this cannot be found anywhere.
Upvotes: 2
Reputation: 2409
This part is specifying that point.
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.
And for your question(how can I store my dates as long using jodatime ? ), you can use miliseconds to store your date as long.
Try like this;
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());
}
And the output is;
local zone = America/Caracas
utc zone = UTC
local: 18.Ara.2015 05:06:00
utc: 18.Ara.2015 09:36:00
millis: 1450431360816
Upvotes: 0