ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.