Along with lambda expression, virtual methods, streams and many other nice features, Java 8 has also updated a new Date and Time API which is maintained under the JSR-310. One of the greatest on this new API is that all the date and time related APIs are unified under the same package java.time. This example demonstrates how to use one of the new class java.time.LocalDate defined under that date and time package.
If you have any clarifications, please write it in the comments section or post it in our facebook page. You can read more articles on Java 8 here.
LocalDate
A LocalDate object is an immutable class that represents just a date. The date can be stored as year-month-day. Also Other date fields, like day-of-year, day-of-week and week-of-year, can also be accessed. For instance, the value “12th October 2010” can be stored in a LocalDate. LocalDate does not store or represent time or time zone (There is another class LocalTime that is used for storing the time and time zone).
This class defines several methods that are useful for fetching the date related values from the stored object. This example represents few important methods that are useful for the programmers in normal scenarios.
- Class Name : LocalDate
- Package : java.time
- Version Released : Java 1.8
LocalDate – Few Methods
- now() – This static method gets the current date from system clock with the default time zone.
- now(ZoneId zone) – This static method gets the current date from the system clock with the specified time zone.
- of(int year, int month, int dayOfMonth) – This static method gets the instance of LocalDate from the specified year,month and day.
- ofYearDay(int year, int dayOfYear) – This static method gets the instance of LocalDate for the specified day in that given year.
- parse(CharSequence text) – This static method parses the plain string and retruns the LocalDate instance if the text is in the correct format. Otherwise returns DateTimeParseException.
- plusDays(long daysToAdd) – This instance method returns the new instance of LocalDate by adding the number of days specified to the existing LocalDate instance.
LocalDate – Example
package javabeat.net; import java.time.LocalDate; import java.time.ZoneId; /** * LocalDate Examples * @author www.javabeat.net * */ public class LocalDateExample { public static void main(String[] args) { LocalDate localDateToday = LocalDate.now(); LocalDate localDateZone = LocalDate.now(ZoneId.of("America/Los_Angeles")); LocalDate localDate = LocalDate.of(2015, 3, 3); LocalDate ofYearDay = LocalDate.ofYearDay(2015, 2); LocalDate parse = LocalDate.parse("2015-04-03"); LocalDate plusDays = localDateToday.plusDays(3); System.out.println("LocalDate Example Demo"); System.out.println("----------------------"); System.out.println("Today's Date : "+localDateToday); System.out.println("Today's Date at Zone America/Los_Angeles : "+localDateZone); System.out.println("LocalDate.of() : "+localDate); System.out.println("LocalDate.ofYearDay() : "+ofYearDay); System.out.println("LocalDate.parse() : "+parse); System.out.println("LocalDate().plusDays() : "+plusDays); } }
Output for the above program will be:
LocalDate Example Demo ---------------------- Today's Date : 2015-04-09 Today's Date at Zone America/Los_Angeles : 2015-04-08 LocalDate.of() : 2015-03-03 LocalDate.ofYearDay() : 2015-01-02 LocalDate.parse() : 2015-04-03 LocalDate().plusDays() : 2015-04-12