Java LocalDate Class | until() Method with Example

LocalDate Class parse() method: Here, we are going to learn about the parse() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on June 03, 2020

LocalDate Class parse() method

Syntax:

public Period until(ChronoLocalDate t_date);
public long until(Temporal temp, TemporalUnit t_unit);
  • until() method is available in java.time package.
  • until(ChronoLocalDate t_date) method is used to return the Period determined by the value between this LocalDate and the given ChronoLocalDate.
  • until(Temporal temp, TemporalUnit t_unit) method is used to determine the amount of time until another date according to the given unit.
  • These methods may throw an exception at the time of representing the Date.
    • DateTimeException: This exception may throw when getting any error during formatting.
    • UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.
    • ArithmeticException: This exception may throw when the calculated result exceeds the limit.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first cases, until(ChronoLocalDate t_date),
    • ChronoLocalDate t_date – represents the target date that can be in any chronology.
  • In the second cases, until(Temporal temp, TemporalUnit t_unit),
    • Temporal temp – represents the object to convert to a date.
    • TemporalUnit t_unit – represents the unit to measure in amount.

Return value:

In the first case, the return type of the method is Period, it returns Period between this date and the given date.

In the second case, the return type of the method is long, it returns the amount of time between this date and the given date.

Example:

// Java program to demonstrate the example 
// of until() method of LocalDate

import java.time.*;
import java.time.temporal.*;

public class UntilOfLocalDate {
    public static void main(String args[]) {
        // Instantiates two LocalDate
        LocalDate date1 = LocalDate.parse("2005-10-05");
        LocalDate date2 = LocalDate.parse("2007-04-05");

        // Display date1,date2 
        System.out.println("LocalDateTime date1,date2 : ");
        System.out.println("date1: " + date1);
        System.out.println("date2: " + date2);

        System.out.println();

        // Here, until(Temporal, TemporalUnit)
        // calculates the amount of time of this
        // date(date1) until another date(date2)
        // in the given unit 
        long until = date1.until(date2, ChronoUnit.DAYS);

        // Display until
        System.out.println("date1.until(date2,ChronoUnit.DAYS): " + until);

        // Here, until(ChronoLocalDate)
        // calculates the period between
        // this date(date1) until another
        // date(date2)
        Period p = date1.until(date2);

        // Display p
        int per_in_mon = p.getMonths();
        System.out.println("date1.until(date2): " + per_in_mon);
    }
}

Output

LocalDateTime date1,date2 : 
date1: 2005-10-05
date2: 2007-04-05

date1.until(date2,ChronoUnit.DAYS): 547
date1.until(date2): 6


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.