Calculating Date Periods with Java

JavaJavaBeginner
Practice Now

Introduction

The Java until() method is used to get the period between two dates as a period object. It calculates the period between two dates in terms of years, months, and days. In this lab, you will learn how to use the until() method and get the period between two dates as a period object.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/date -.-> lab-117854{{"`Calculating Date Periods with Java`"}} java/packages_api -.-> lab-117854{{"`Calculating Date Periods with Java`"}} java/identifier -.-> lab-117854{{"`Calculating Date Periods with Java`"}} java/data_types -.-> lab-117854{{"`Calculating Date Periods with Java`"}} java/operators -.-> lab-117854{{"`Calculating Date Periods with Java`"}} java/output -.-> lab-117854{{"`Calculating Date Periods with Java`"}} java/strings -.-> lab-117854{{"`Calculating Date Periods with Java`"}} java/variables -.-> lab-117854{{"`Calculating Date Periods with Java`"}} java/system_methods -.-> lab-117854{{"`Calculating Date Periods with Java`"}} end

Add the necessary imports

Add the following import statements to the "DateDemo.java" file:

import java.time.LocalDate;
import java.time.Period;

Create a LocalDate object

Create a LocalDate object for the start date using the LocalDate.of() method, and assign it to a variable named startDate. The date can be any valid date you want.

LocalDate startDate = LocalDate.of(2002, 01, 10);

Get the period between the two dates

Use the until() method to get the period between the start date and end date. The method takes an argument of ChronoLocalDate type which can be any class object that implements it. In this example, we will be using another LocalDate object for the end date.

Period period = startDate.until(LocalDate.of(2005, 10, 12));

Print the period

Print the period between the two dates using System.out.println(). The period will be printed in the format PnYnMnD (n represents the number of years, months, and days).

System.out.println("Period between start and end date: " + period);

Get individual values of the period

Use the getYears(), getMonths(), and getDays() methods to get individual values of the period (i.e., number of years, months, and days).

int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();

System.out.println("Years: " + years);
System.out.println("Months: " + months);
System.out.println("Days: " + days);

Compile and run the program

Compile the program using the following command in the terminal:

javac DateDemo.java

Run the program using the following command:

java DateDemo

Summary

The until() method is a powerful tool for calculating the period between two dates in terms of years, months, and days. By using this method, you can easily get the period between two dates and perform calculations based on the period. In this lab, you learned how to use the until() method to get the period between two dates as a period object, and how to get individual values of the period.

Other Java Tutorials you may like