Another new feature by Java 8 is the DateTime API. Some of the important classes in the DateTime API are the LocalDate, LocalTime and LocalDateTime. In this article, I will be explaining the Java 8 LocalDate class in detail.
The LocalDate class represents a date. It is present in the java.time package. It has several methods that allow you to perform date manipulation easily.
There are several static methods on the LocalDate class that allow you to obtain a LocalDate instance. The following code demonstrates this:
public class LocalDateDemo {
public static void main(String[] args) {
LocalDate date1 = LocalDate.now();
System.out.println("Today's date is "+date1);
LocalDate date2 = LocalDate.parse("2019-07-15");
System.out.println("Date2 is "+date2);
LocalDate date3 = LocalDate.of(2017,05,17);
System.out.println("Date3 is "+date3 );
LocalDate date4 = LocalDate.ofYearDay(2017,8);
System.out.println("Date4 is "+date4);
}
}
So when this code is executed, it will print the following output:
Today's date is 2019-06-26
Date2 is 2019-07-15
Date3 is 2017-05-17
Date4 is 2017-01-08
In addition to these methods, there are several other methods in the LocalDate class that you can use to create a Date. You can check them out via the API documentation.
The LocalDate allows you to easily perform date arithmetic. The following code demonstrates this:
LocalDate date = LocalDate.parse("2019-07-15");
LocalDate date2 = date.plusDays(5);//add 5 days
System.out.println("Date "+date+" plus 5 days is "+date2);
date2 = date.minusMonths(4); //subtract 4 months
System.out.println("Date "+date+" minus 4 months is "+date2);
date2 = date.plusWeeks(3);
System.out.println("Date "+date+" plus 3 weeks is "+date2);
date2 = date.withYear(2019);//year set to 2019
System.out.println("Date "+date+" with year set to 2019 is "+date2);
When you execute this code, it will print the following output:
Date 2019-07-15 plus 5 days is 2019-07-20
Date 2019-07-15 minus 4 months is 2019-03-15
Date 2019-07-15 plus 3 weeks is 2019-08-05
Date 2019-07-15 with year set to 2019 is 2019-07-15
In addition to these methods, there are several methods that allow you to perform Date arithmetic easily. You can refer to the API documentation
The LocalDate class allows you to easily extract information from the Date object. The following code demonstrates this:
LocalDate date = LocalDate.parse("2019-07-15");
int day = date.getDayOfYear();
System.out.println("DayOfYear="+day);
int dayOfmonth = date.getDayOfMonth();
System.out.println("DayOfmonth="+dayOfmonth);
Month month = date.getMonth();
System.out.println("Month="+month);
DayOfWeek dayOfWeek = date.getDayOfWeek();
System.out.println("DayOfWeek="+dayOfWeek);
When you execute this code, it will print the following output:
DayOfYear=196
DayOfmonth=15
Month=JULY
DayOfWeek=MONDAY
In addition to these methods, there are several methods that allow you to easily extract the information you need from the LocalDate object. You can refer to the API documentation
The LocalDate class allows you to easily perform Date comparison. The following code demonstrates this:
LocalDate date1 = LocalDate.parse("2017-05-17");
LocalDate date2 = LocalDate.parse("2014-09-14");
boolean isAfter= date1.isAfter(date2);
System.out.println("Is after:"+isAfter);
LocalDate date3 = LocalDate.of(2017, 05, 17);
boolean isEqual = date1.isEqual(date3);
System.out.println("Is Equal:"+isEqual);
boolean isLeapYear = date1.isLeapYear();
System.out.println("Is LeapYear:"+isLeapYear);
When you execute this code, it will print the following output:
Is after:true
Is Equal:true
Is LeapYear:false
In addition to these methods, there are several methods that allow you to perform Date comparison. You can refer to the API documentation
The LocalDate class allows you to easily calculate the duration between dates. The following code demonstrates this:
LocalDate date1 = LocalDate.parse("2017-05-17");
LocalDate date2 = LocalDate.parse("2017-08-17");
Period period = date1.until(date2);
System.out.println("Duration between "+date1+" and "+date2+" is "+ period.getMonths()+" months");
The LocalDate.untill method returns the duration between 2 dates as a Period object. Period is another class in java.time. It has several methods which can be used to extract the desired information. Here the period.getMonths method is used which returns the duration in months. So when you run this code, it will print the following output:
Duration between 2017-05-17 and 2017-08-17 is 3 months
So, in this article, we learnt about the Java 8 LocalDate class. We learnt how to create a LocalDate, how to perform date arithmetic and date comparison.