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.

Important features

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

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.

Setting up Environment

  • Create your java project in eclipse.

  • Download the latest JodaTime .tar.gz file, 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();

Example:

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

Output

Current Day: Monday
Current Month: August
Current Year: 2018
Current Year is Leap Year: false
2018-08-06T13:12:16.672
core java programming joda-time

Subscribe For More Content