CodesJava

Easy learning with example program codes

Java 8 LocalDate class


The java.time.LocalDate class is an immutable class without a time-zone in the ISO-8601 calendar system, such as 2018-04-03.

Java LocalDate class methods

Method Description
LocalDateTime atTime(int hour, int minute) It is used to combine this date with a time to create a LocalDateTime.
int compareTo(ChronoLocalDate other) It is used to compares this date to another date.
boolean equals(Object obj) It is used to check if this date is equal to another date.
String format(DateTimeFormatter formatter) It is used to format this date using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this date as an int.
boolean isLeapYear() It is used to check if the year is a leap year, according to the ISO proleptic calendar system rules.
LocalDate minusDays(long daysToSubtract) It is used to return a copy of this LocalDate with the specified number of days subtracted.
LocalDate minusMonths(long monthsToSubtract) It is used to return a copy of this LocalDate with the specified number of months subtracted.
static LocalDate now() It is used to obtain the current date from the system clock in the default time-zone.
LocalDate plusDays(long daysToAdd) It is used to return a copy of this LocalDate with the specified number of days added.
LocalDate plusMonths(long monthsToAdd) It is used to return a copy of this LocalDate with the specified number of months added.

Example

package com.codesjava;
 
import java.time.LocalDate;
 
public class TestExample {
	public static void main(String args[]){
	    LocalDate date = LocalDate.now();  
	    LocalDate yesterday = date.minusDays(1);  
	    LocalDate tomorrow = yesterday.plusDays(2);  
	    System.out.println("Today: "+date);  
	    System.out.println("Yesterday: "+yesterday);  
	    System.out.println("Tommorow: "+tomorrow);  
 
	    LocalDate date1 = LocalDate.of(2018, 1, 16);  
	    System.out.println(date1.isLeapYear());  
	    LocalDate date2 = LocalDate.of(2016, 9, 23);  
	    System.out.println(date2.isLeapYear());  
	}  
}

Output

Today: 2018-04-15
Yesterday: 2018-04-14
Tommorow: 2018-04-16
false
true
Posted in Date   
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki