Alex Rivera | Logout

Android Java - Joda Date is slow

Asked 2011-02-20T20:03:20.903
57

Using Joda 1.6.2 with Android

The following code hangs for about 15 seconds.

DateTime dt = new DateTime();

Originally posted this post Android Java - Joda Date is slow in Eclipse/Emulator -

Just tried it again and its still not any better. Does anyone else have this problem or know how to fix it?

Edit
Report

1 Answer

2

I found solution for me. I load UTC and default time zone. So it's loads very fast. And I think in this case I need catch broadcast TIME ZONE CHANGE and reload default time zone.

public class FastDateTimeZoneProvider implements Provider {
    public static final Set<String> AVAILABLE_IDS = new HashSet<String>();
    static {
        AVAILABLE_IDS.add("UTC");
        AVAILABLE_IDS.add(TimeZone.getDefault().getID());
    }

    public DateTimeZone getZone(String id) {
        int rawOffset = 0;

        if (id == null) {
            return DateTimeZone.getDefault();
        }

        TimeZone tz = TimeZone.getTimeZone(id);
        if (tz == null) {
            return DateTimeZone.getDefault();
        }

        rawOffset = tz.getRawOffset();

        //sub-optimal. could be improved to only create a new Date every few minutes
        if (tz.inDaylightTime(new Date())) {
            rawOffset += tz.getDSTSavings();
        }
        return DateTimeZone.forOffsetMillis(rawOffset);
    }

    public Set getAvailableIDs() {
        return AVAILABLE_IDS;
    }
}
answered 2012-11-26T18:33:21.050

Your Answer