JODA-Time

07 Aug, 2018
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.
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);
Setting up EnvironmentCreate 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 -> libsCopy/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.jarNow you can test with this code :DateTime test = new DateTime();Basic Example// Java program to illustrate// functions of JODA timeimport 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);    }}OutputCurrent 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.ReferencesHierarchy For Package org.joda.timehttp://www.joda.org/joda-time/http://www.joda.org/joda-time/quickstart.htmlMy Personal Notes
arrow_drop_upSave
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.
    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

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

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);

// get current date and time
LocalDateTime dt = LocalTime.now(); 
System.out.println("%s", dt);

Setting up Environment
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 -> libsCopy/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.jarNow you can test with this code :DateTime test = new DateTime();
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();
DateTime test = new DateTime();
Basic Example
// Java program to illustrate// functions of JODA timeimport 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);    }}
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.
ReferencesHierarchy For Package org.joda.timehttp://www.joda.org/joda-time/http://www.joda.org/joda-time/quickstart.html
Java - util package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
List Interface in Java with Examples
Strings in Java
Different ways of Reading a text file in Java
Reverse an array in Java
How to remove an element from ArrayList in Java?
Checked vs Unchecked Exceptions in Java
ArrayList get(index) Method in Java with Examples
ArrayList to Array Conversion in Java : toArray() Methods
Comparator Interface in Java with Examples
Exceptions in Java