Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Joda Time

I am building an app that uses time. I decided to use Joda time. I am trying to add joda time to my app. I have no experience with adding .jar files to the libs. I followed these steps(see bottom), or so I thought. My project is showing no errors, but when I run a simple test: DateTime test = new DateTime(); I get a force close and the following error:

 Could not find class 'org.joda.time.DateTime', referenced from method xxxx
 java.lang.NoClassDefFoundError: org.joda.time.DateTime    

These are the steps I followed:

  1. Create your android project in eclipse.
  2. Download Joda Time
  3. In Eclipse look for your project at package explorer and right click on it then call it New -> Folder -> libs. Note: I did not do this because I already had a folder named libs
  4. Copy (or) drag & drop the joda-time-2.1.jar into the newly created libs folder.
  5. Right click on your project again (in package explorer) then Properties -> Java Build Path -> Libraries -> Add Jars -> joda-time-2.1.jar.

Now you can test with this code :

DateTime test = new DateTime();

After importing:

import org.joda.time.DateTime;
like image 402
Chad Bingham Avatar asked Mar 28 '13 05:03

Chad Bingham


People also ask

Is Joda time included in Java 8?

Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.

What is Joda Time in Java?

Joda-Time provides a quality replacement for the Java date and time classes. Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java. time (JSR-310).

What is Joda Time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.


1 Answers

Since it is an Android project, just referencing the Joda jar file won't do. You'll need to bundle that jar file along with your apk which can be done like this:-

  1. Properties -> Java Build Path -> Order & Export

  2. Check on the joda-time-*-*.jar and click Ok. You're done.


Explanation: Referencing the jar alone will only solve the compilation issues in Eclipse (or any other IDE), but when the apk is created the "joda-time" jar file won't be exported/bundled along with your "apk". That is the reason why it throws the NoClassDefFoundError.

But when you check the joda-time jar in Order & Export, it'll make sure that the jar is also bundled inside/along with your "apk" whenever it is created.

like image 142
Rahul Avatar answered Sep 30 '22 03:09

Rahul