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.time package.
To include we need to import following :
Basic Features of JODA-TIME
- It uses easy field accessors like getYear(), getDayOfWeek(), getDayofYear().
- It supports 7 Calendar Systems like Buddhist, Coptic, Ethiopic, Gregorian, GregorianJulian, Islamic, Julian.
- There is a Provision to create our own Calendar system.
- It Provides rich Set of Methods for date and Time calculations.
- It uses a database for time zones. This database updated manually several times in a year.
- Its Methods executes faster compared to earlier methods of java 7.0; thus, provides better performance
- Its Objects are immutable. So, they are thread-safe
Important Classes in java.time package.
- DateTime : Immutable replacement for JDK Calendar.
DateTime dt = new DateTime();
// which creates a datetime object representing the current date and time in milliseconds as determined by the system clock. It is constructed using the ISO calendar in the default time zone.
- LocalDate :This class represents a date in the form of year-month-day and useful for representing a date without time and time zone.
LocalDate today = LocalDate.now() //gives System date into LocalDate object using now method. System.out.println(today) // 2018-08-03 int d = today.getDayOfMonth(); // 03
- LocalTime : This class represents the time of the day without time zone.
LocalTime time = LocalTime.now(); //gives System time into localTime object System.out.println(time); // 10:19:58
- LocalDateTime : This class handles both date and time without considering the time zone.
// get current date and time LocalDateTime dt = LocalTime.now(); System.out.println("%s", dt);
- Create your java project in eclipse.
- Download the latest JodaTime .tar.gz file Click Here, and extract its contents.
- In Eclipse, look for your project at package explorer and right click on it then call it New -> Folder -> libs
- Copy/Drag joda-time-2.1.jar into the new created libs folder.
- 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();
Setting up Environment
Current Day: Monday Current Month: August Current Year: 2018 Current Year is Leap Year: false 2018-08-06T13:12:16.672
- Similar usage across multiple Java platforms.
- Supports additional calendars such as Buddhist and Ethiopic.
- Self-reported better performance.
- Easy interoperability: The library internally uses a millisecond instant which is identical to the JDK and similar to other common time representations. This makes interoperability easy, and Joda-Time comes with out-of-the-box JDK interoperability.
- Requires installation of package and perhaps updates from Joda.org.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Reference Variable in Java
- FileOutputStream in Java
- Collection Interface in Java with Examples
- Cloneable Interface in Java
- How to Create Classes in Android Studio?
- Adding Images to a Table in PDF using Java
- How to Commit a Query in JDBC?
- How to Temporarily Stop a Thread in Java?
- ConcurrentSkipListMap in Java with Examples
- Java Program to Add Text to an image in OpenCV
- Java Program to Create blank Excel Sheet
- Java Program to Retrieve Contents of a Table Using JDBC connection
- Java Program to Print the ASCII Value
- Java Program to Compute GCD
- Java Program to Convert Fahrenheit into Celsius
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.