Joda API in Java 8 DateTime API

multiple-timezones

Until Java 1.7 version, we had below classes in java to handle the date and time:

  • Date
  • Calendar
  • Timestamp and few more

But these classes we not much convenient and also lacked in performance. Most of the methods in the above-said classes are depreciated.

To overcome these problems, Java 1.8 has introduced new Date Time API, also known as Joda Time API; classes present in these API can handle the Date and Time effectively.

Joda Time APi is present under the java.time package.

joda.org has developed this new date and time API; this is the reason why it is called as Joda Time API.
Joda was named after the powerful Starwars Jedi Master Yoda. Yoda's image is below if you have not watched StarWars.

iodine-time-api

Sample code to get the current date and time of the system:
import java.time.LocalDate;
import java.time.LocalTime;

public class DateTimeExample {
	public static void main(String[] args) {
		LocalDate currentDate = LocalDate.now();
		System.out.println(currentDate);
		
		LocalTime currentTime = LocalTime.now();
		System.out.println(currentTime);
	}
}

localdate-time-joda-time-api-java

LocalDate and LocalTime in java

We can extract the different parts of the Date and Time objects using the methods present in the Joda time API.

In the below program, we are extracting the components of the Date.

public static void main(String[] args) {
	LocalDate date = LocalDate.now();
	System.out.println("complete Date : "+date);
	int day = date.getDayOfMonth();
	int month = date.getMonthValue();
	int year = date.getYear();
	System.out.println(day+"/"+month+"/"+year);
}

date-compoenents-joda-time-api-java

Sample code for getting Time components:

public static void main(String[] args) {
	LocalTime time = LocalTime.now();
	int hour = time.getHour();
	int minute = time.getMinute();
	int second = time.getSecond();
	int nanoSecond = time.getNano();
	System.out.println("Complete time : "+time);
	System.out.println(hour+"->"+minute+"->"+second+"->"+nanoSecond);
}

time-components-joda-time-api-java

LocalDateTime Class:

We can use LocalDateTime class Instead of writing different classes for fetching the date and time. All the method names are the same as those two different classes.

public static void main(String[] args) {
	LocalDateTime dt = LocalDateTime.now();
	System.out.println("complete date-time : "+dt);
	int day = dt.getDayOfMonth();
	int month = dt.getMonthValue();
	int year = dt.getYear();
	int hour = dt.getHour();
	int minute = dt.getMinute();
	int second = dt.getSecond();
	int nanoSecond = dt.getNano();
	System.out.println(day+"/"+month+"/"+year+"::::"+hour+"."+minute+"."+second+"."+nanoSecond);
}

date-time-joda-time-api-java

Date time object for a particular date

Previously we have seen how to create objects for the current date and the current time. Sometimes we will be creating for some date which is not current in such cases we can use the of() method from the LocalDateTime class

public static void main(String[] args) {
	LocalDateTime dtWithMinutes = LocalDateTime.of(1995, 06, 26, 7, 56);
	LocalDateTime dtWithSeconds = LocalDateTime.of(1995, 06, 26, 7, 56, 34);
	LocalDateTime dtWithNanoSeconds = LocalDateTime.of(1995, 06, 26, 7, 56, 34, 345);
	System.out.println("date object with minutes : "+dtWithMinutes);
	System.out.println("date object with seconds : "+dtWithSeconds);
	System.out.println("date object with nono seconnds : "+dtWithNanoSeconds);
}

date-of-past-objects-joda-api-java

Are you thinking, when we have all those date details why we need to create an object for it rather than directly using?

Just guess what would be the result when I add 10 + 10, yes you might have guessed it correctly that answer is 20. But what if you are adding October + 10Months which is also internally 10 +10, so we do not want to get 20 as an answer, for this reason, we are creating Date time object.

Offset the Date time:

We might want to add a couple of days or a couple of months to the date and time; in such cases, all the operation should not happen based arithmetic, but it should happen according to date time.

Joda Time API provides few methods to handle the offset of the date and time, and we can get offset using months, weeks, days, and more.

offset does not alter the original date time object

public static void main(String[] args) {
	LocalDateTime dtWithMinutes = LocalDateTime.of(1995, 06, 26, 7, 56);
	LocalDateTime plusOffset = dtWithMinutes.plusDays(300);
	LocalDateTime minusOffset = dtWithMinutes.minusDays(250);
	System.out.println("Original date time " +dtWithMinutes);
	System.out.println("Plus Offset date time " +plusOffset);
	System.out.println("Minus Offset date time " +minusOffset);
}

offset-date-time-joda-api

There a few other offset methods:

  • plusDays(), minusDays()
  • plusMonths(), minusMonths()
  • plusHours(), minusHours()
  • plusWeeks(), minusWeeks()
  • plusSeconds(), minusSeconds()
  • plusNanoSeconds(), minusNanoSeconds()

Difference between dates/ Period Class

Sometimes, you might need to find the difference between the dates, in such cases, you can use the Joda Time API to get the difference between dates.

Joda API provides a class called Period. The Period class has a method called between() which accepts two dates and provides a date-time Object, based on this object we can get the difference of years, months, and days

public static void main(String[] args) {
	LocalDate dtWithMinutes = LocalDate.of(1995, 06, 26);
	LocalDate currentDate = LocalDate.now();
	Period p = Period.between(dtWithMinutes, currentDate);
	System.out.println(p.getYears()+" years, "+p.getMonths()+" months, "+p.getDays()+" days");
}

period-class-joda-api-java

Time Zones in Joda API

The world is split with timezones, and we can handle this kind of variance using the Joda Time API.

time-zone-joda-time-api-java8

We can use the systemDefault() method to get the default time zone of the system.

public static void main(String[] args) {
	ZoneId zone = ZoneId.systemDefault();
	System.out.println(zone);
}
//output
Asia/Calcutta
Handle different Time zone:

We can get the time of the different times zones with java8 Time API/ joda. We can use the now() method present under the class ZoneDateTime

public static void main(String[] args) {
	ZoneId zone = ZoneId.of("Europe/London");
	ZonedDateTime zt = ZonedDateTime.now(zone);
	System.out.println("current time at "+zone +" is " +zt);
}

zoneid-joda-java8-time-api

Format the date with java 8 Joda Time API

We can format the date using the DateTimeFormatter class, format() class present in the LocalDateTime class will accept an object of DateTimeFormatter

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeExample {
	public static void main(String[] args) {
		LocalDateTime date = LocalDateTime.now();
		DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd MMMM yyyy :: hh mm ss");
		String str = date.format(fmt);
		System.out.println(str);
		
		DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("dd MM yyyy :: hh mm ss");
		String str1 = date.format(fmt1);
		System.out.println(str1);
	}
}

date-time-joda-format-java8-api

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions