JODA-Time

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 :

import java.time.*;

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.

  1. 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.

  2. 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
    
  3. 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
    
  4. 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);
    
  5. Setting up Environment



    1. Create your java project in eclipse.
    2. Download the latest JodaTime .tar.gz file Click Here, and extract its contents.
    3. In Eclipse, look for your project at package explorer and right click on it then call it New -> Folder -> libs
    4. Copy/Drag joda-time-2.1.jar into the new 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
    6. Now you can test with this code :
      DateTime test = new DateTime();

    Basic Example

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // functions of JODA time
    import org.joda.time.DateTime;
    import org.joda.time.LocalDateTime;
    public class JodaTime {
        public static void main(String[] args)
        {
            DateTime now = new DateTime();
            System.out.println("Current Day: " + now.dayOfWeek().getAsText());
            System.out.println("Current Month: " + now.monthOfYear().getAsText());
            System.out.println("Current Year: " + now.year().getAsText());
            System.out.println("Current Year is Leap Year: " + now.year().isLeap());
      
            // get current date and time
            LocalDateTime dt = LocalDateTime.now();
      
            System.out.println(dt);
        }
    }

    chevron_right

    
    

    Output

    Current Day: Monday
    Current Month: August
    Current Year: 2018
    Current Year is Leap Year: false
    2018-08-06T13:12:16.672
    

    Advantages:

    • 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.

    Disadvantages:

    • Requires installation of package and perhaps updates from Joda.org.

    References
    Hierarchy For Package org.joda.time
    http://www.joda.org/joda-time/
    http://www.joda.org/joda-time/quickstart.html

    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.




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.