Monday, August 27, 2018

Java8 - Top 10 LocalDate examples with tutorials

java.time.LocalDate

LocalDate is introduced in java8 language as part of Date and Time API enhancement. It represents Date without timezone information in ISO format ie YYYY-MM-DD. It is used to store dates like birthday and holidays. Local Date contains only year month and Day.

LocalDate Examples

We are going to see the many usecases of LocalDate class.

Get Today Yesterday and Tomorrows date ?

LocalDate.now() method is used to get current date of a default time zone LocalDate.plus() method is to add given date with adding one using 1 with ChronoUnit.DAYS parameter LocalDate.minusDays() method is to substract days from the given date
LocalDate todayDate = LocalDate.now();
System.out.println(todayDate);
LocalDate tomorrowDate = todayDate.plus(1, ChronoUnit.DAYS);
System.out.println(tomorrowDate);
LocalDate yesterdayDate = tomorrowDate.minusDays(2);
System.out.println(yesterdayDate);
Output is
2018-08-27
2018-08-28
2018-08-26

How to create a LocalDate Object java8? 

There are two methods we can create LocalDate Object LocalDate.now() - returns current dates LocalDate.of() method is to create a LocalDate using year, month and Day
LocalDate giveDate = LocalDate.of(2011,05,5);
System.out.println(giveDate);
LocalDate todayDate = LocalDate.now();
System.out.println(todayDate);
Output is
2011-05-05
2018-08-27

How to get year, month and Day of Week,Month,Year from current date? 

This is an example for calling LocalDate methods usages.
getYear() - return current year
getMonthValue() - returns current number of a year
getDayOfYear() - return day of a Year
getDayOfMonth() - return day of a Month
getDayOfWeek() - return day of a Week
LocalDate todayDate = LocalDate.now();
System.out.println("Date: "+todayDate);
System.out.println("Year: "+todayDate.getYear());
System.out.println("Month: "+todayDate.getMonthValue());
System.out.println("Day of year : "+todayDate.getDayOfYear());
System.out.println("Day of Month : "+todayDate.getDayOfMonth());
System.out.println("Day of Week : "+todayDate.getDayOfWeek());
Output is
Date: 2018-08-27
Year: 2018
Month: 8
Day of year : 239
Day of Month : 27
Day of Week : MONDAY

How to convert java.sql.timestamp to LocalDate in java8?

java.sql.Timestamp class is required to have a date and time values used in JDBC API. Timestamp is combination of Date and time values. toLocalDateTime() returns LocalDateTime, Convert this to LocalDate using toLocalDate() method.
long time = System.currentTimeMillis();
Timestamp timestamp=new Timestamp(time);
System.out.println(timestamp);
System.out.println(timestamp.toLocalDateTime().toLocalDate());

2018-08-27 14:50:58.689
2018-08-27

How to Convert Localdate to Instant Object?

java.time.Instant class used to represents instant time in a timeline which has timezone information and localDate has no timezone information.
LocalDate localDate = LocalDate.now();
Instant instantDate = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
System.out.println(instantDate);
Output is
2018-08-26T18:30:00Z

How to compare LocalDate instances in java8? 

LocalDate has three different methods to do comparision
isEqual method - returns true if this date is equal to given date
isBefore method - returns true if this date is before given date
isAfter method - returns true if this date is a given date
LocalDate localDateNow = LocalDate.now();
LocalDate localDate2016 = LocalDate.of(2016, Month.AUGUST, 10);
LocalDate localDate2015 = LocalDate.of(2015, Month.AUGUST, 10);
LocalDate localDate2014 = LocalDate.of(2014, Month.AUGUST, 10);
System.out.println("now - equals - 2016 " + localDateNow.isEqual(localDate2016));
System.out.println("2015 - After -2016 " + localDate2015.isAfter(localDate2016));
System.out.println("2014 - After -2015 " + localDate2014.isAfter(localDate2015));
System.out.println("2014 - Before -2015 " + localDate2014.isBefore(localDate2015));
Output is
now - equals - 2016 false
2015 - After -2016 false
2014 - After -2015 false
2014 - Before -2015 true

How to Convert String to LocalDate Object with example in java8?

LocalDate provides parse() method. DateTimeFormatter is class for formatting date time objects. The below examples has a date string in different formats ISO_LOCAL_DATE and other formats
System.out.println(LocalDate.parse("2018-08-27",DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println(LocalDate.parse("2018/08/27",DateTimeFormatter.ofPattern("yyyy/MM/dd")));
System.out.println(LocalDate.parse("27-08-2018",DateTimeFormatter.ofPattern("dd-MM-yyy")));
System.out.println(LocalDate.parse("27-Aug-2018",DateTimeFormatter.ofPattern("dd-MMM-yyy")));
System.out.println(LocalDate.parse("Aug 27, 2018",DateTimeFormatter.ofPattern("MMM dd, yyyy")));
2018-08-27
2018-08-27
2018-08-27
2018-08-27
2018-08-27
date is unable to parse it, Compiler throws Exception in thread "main" java.time.format.DateTimeParseException: Text '28/08/2018' could not be parsed at index 0

How to Convert LocalDate to String Object example in java8?

LocalDate provides format() method which takes DateTimeFormatter object. DateTimeFormatter holds output date in string format
System.out.println(LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd-MM-yyy")));
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd-MMM-yyy")));
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("MMM dd, yyyy")));
Output is
2018-08-27
2018/08/27
27-08-2018
27-Aug-2018
Aug 27, 2018

How to convert LocalDate to java.sql.Date in java8?

LocalDate provides valueOf() method which returns java.sql.Date object.
LocalDate currentDate = LocalDate.now();
Date sqlDate = Date.valueOf(currentDate); 
System.out.println(sqlDate);
Output is
2018-08-27

How to convert java.util.Date to LocalDate?

java.util.Date is to s pecify the time on timeline which includes date and time with timezone information. First covnert to Instance object using toInstant. To convert to localdate, use default timezone ZoneId.systemDefault() and finally call toLocalDate() to convert it.
Date utilDate = new Date();
System.out.println(utilDate); // Mon Aug 27 15:47:04 IST 2018
LocalDate localDate = utilDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println(localDate); //2018-08-27 
Output is
Mon Aug 27 15:47:04 IST 2018
2018-08-27

Related article


EmoticonEmoticon