Share & learn

Through Innovative Digital Library

What is LocalDate class in Java?

Explain about LocalDate class in Java. Provide sample code for this class.
 



Answers

Posted by Nathan on March 5th 2018 15:07 PM

java.time.LocalDate Class in Java : LocalDate is a class in Java which by default returns date in 'yyyy-MM-dd' format. LocalDate class is immutable i.e. contents of this class cannot be changed. Declaration : public final class LocalDate extends Object implements Temporal, ChronoLocalDate, TemporalAdjuster, Serializable Methods of LocalDate Class : LocalDate class has following methods : 1. int compareTo(ChronoLocalDate other): This method is used to compare the dates. 2. LocalDate minusDays(long daysToSubtract) : This method returns copy of this LocalDate by subtracting the provided number of days. 3. LocalDate minusMonths(long monthsToSubtract) : This method returns copy of this LocalDate by subtracting the provided number of months. 4. LocalDateTime atTime(int hour, int minute) : This method is used to combine this date and a time resulting in LocalDateTime. 5. static LocalDate now() : It is used to get the current date from the system clock for the timezone which is set by default. 6. LocalDate plusDays(long daysToAdd) : Add particular number of days to LocalDate. 7. LocalDate plusMonths(long monthsToAdd) : Add particular number of days to LocalDate. 8. boolean equals(Object obj) : This method checks to dates for equality. 9. String format(DateTimeFormatter formatter) : This method is used to format this date with provided formatter. 10. int get(TemporalField field) : This method is used to get the value of the specified field from this date as an int. 11. boolean isLeapYear() : This method is used to check whether the provided year is a leap year or not. Let's see few examples of LocalDate class : Here we will write a code to print today's date, yesterday's date and tomorrow's date. For this code we will use minusDays() and plusDays() methods. In the same code we will check whether present year is leap year or not. Here is the code.

import java.time.LocalDate;

public class 
LocalDateDemo {

    public static 
void main(String[] args) {
        
// TODO Auto-generated method stub

        //create object of LocalDate
        //calling now() method
        
LocalDate date LocalDate.now();  
        
        
//another object to print yesterda's date
        
LocalDate yesterday date.minusDays(1);  
        
        
//tomorrow to print tomorrow's date
        
LocalDate tomorrow date.plusDays(1);
        
        
//print the dates
        
System.out.println("Today's date is  "+date);  
        
System.out.println("Yesterday's date was "+yesterday);  
        
System.out.println("Tommorow's date is "+tomorrow);  
        
        
//code to check leap year
        //i will store isLeapYear() value 
        
boolean idate.isLeapYear();  
        if (
==false)
        {
            
System.out.println("This year isn't Leap Year");
        }
        else
            
System.out.println("Present year is Leap Year");
    }  
        
    }

Posted by Nathan on March 5th 2018 15:08 PM

Let's take a look at another piece of code. Here we will print today's date, date before 3 months and date after 3 months. Also in the same code we will create LocalDate by providing input arguments.

package dateDemo;

import java.time.LocalDate;
import java.time.Month;

public class 
LocalDateDemo {

    public static 
void main(String[] args) {
        
// TODO Auto-generated method stub

        //create object of LocalDate
        //calling now() method
        
LocalDate date LocalDate.now();  
        
        
//another object to print date
        //before 3 months
        
LocalDate date2 date.minusMonths(3);  
        
        
//tomorrow to print date
        //after 3 months
        
LocalDate date3 date.plusMonths(3);
        
        
//print the dates
        
System.out.println("Today's date is  "+date);  
        
System.out.println("Date before 3 months is  "+date2);  
        
System.out.println("Date after 3 months is "+date3);  
        
        
//Creating LocalDate by providing input arguments
        
LocalDate firstDay_2015 LocalDate.of(2015Month.JANUARY1);
        
System.out.println("Specific Date="+firstDay_2015);
        
    }  
        
    }
Answer Question

Your email address will not be published. Required fields are marked *

  • Minimum 20 character
8V7Q

related questions

Most liked questions

Most answered questions

Login
8V7Q Refresh
Register
8V7Q Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Reply
Minimum 20 character
8V7Q Refresh