joda-time-android

Additional

Language
Java
Version
v2.10.2 (Jul 4, 2019)
Created
Jan 9, 2014
Updated
Jul 12, 2019
Owner
Daniel Lew (dlew)
Contributors
Artem Zinnatullin :slowpoke: (artem-zinnatullin)
ligi
FUJI Goro (gfx)
hamen
Daniel Lew (dlew)
plastiv
rlac
scottdweber
josiahneuberger
elevenfive
raskasa
lucasxas
vladad
JimVanG
jonatbergn
janbolat
16
Activity
Badge
Generate
Download
Source code
APK file

Advertising

joda-time-android

This library is a version of Joda-Time built with Android in mind.

Why Joda-Time?

Android has built-in date and time handling - why bother with a library? If you've worked with Java's Date and Calendar classes you can probably answer this question yourself, but if not, check out Joda-Time's list of benefits.

For Android developers Joda-Time solves one critical problem: stale timezone data. Built-in timezone data is only updated when the OS is updated, and we all know how often that happens. Countries modify their timezones all the time; being able to update your own tz data keeps your app up-to-date and accurate.

Why This Library?

I know what you are thinking: Joda-Time is a great library and it's just a single JAR, so why make things more complex by wrapping it in an Android library?

There is a particular problem with the JAR setup on Android: due to its usage of ClassLoader.getResourceAsStream(), it greatly inflates its memory footprint on apps. (For more details, see this blog post.) This library avoids the problem for Android by loading from resources instead of a JAR.

This library also has extra utilities designed for Android. For example, see DateUtils, a port of Android's DateUtils.

Usage

Add the following dependency to build.gradle:

dependencies {
    implementation 'net.danlew:android.joda:2.10.2'
}

Once that's done, you must initialize the library before using it by calling JodaTimeAndroid.init(). I suggest putting this code in Application.onCreate():

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        JodaTimeAndroid.init(this);
    }
}

Troubleshooting

Q: My build fails with an error about a duplicate file

Duplicate files copied in APK META-INF/LICENSE.txt

or

Duplicate files copied in APK META-INF/NOTICE.txt

A: We can safely exclude thoses files from our build. You need to specify these two excludes in your build.gradle file and you will be good to go:

android {
    ...
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

or

B: We can safely choose to add those files to our build. You need to specify these two merges in your build.gradle file and you will be good to go:

android {
    ...
    packagingOptions {
        merge '**/LICENSE.txt'
        merge '**/NOTICE.txt'
    }
}