## How do I calculate difference between two dates?

In this example you’ll learn how to use the `java.time.Period` class (from Java 8) to calculate difference between two dates. Using `Period.between()` method will give us difference between two dates in years, months and days period.

Beside using the `Period` class, we also use the `ChronoUnit` enum to calculate difference between two dates. We use the `ChronoUnit.YEARS`, `ChronoUnit.MONTHS` and `ChronoUnit.DAYS` and call the `between()` method to get the difference between two dates in years, months and days.

Let’s see an example below.

``````package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class DateDifference {
public static void main(String[] args) {
LocalDate birthDate = LocalDate.of(1995, Month.AUGUST, 17);
LocalDate now = LocalDate.now();

// Obtains a period consisting of the number of years, months and days
// between two dates.
Period age = Period.between(birthDate, now);
System.out.printf("You are now %d years, %d months and %d days old.%n",
age.getYears(), age.getMonths(), age.getDays());

// Using ChronoUnit to calculate difference in years, months and days
// between two dates.
long years = ChronoUnit.YEARS.between(birthDate, now);
long months = ChronoUnit.MONTHS.between(birthDate, now);
long days = ChronoUnit.DAYS.between(birthDate, now);

System.out.println("Diff in years  = " + years);
System.out.println("Diff in months = " + months);
System.out.println("Diff in days   = " + days);
}
}
``````

The result of our code snippet above are:

``````You are now 26 years, 2 months and 30 days old.
Diff in years  = 26
Diff in months = 314
Diff in days   = 9588
``````

## How do I know if a given year is a leap year?

The example How do I check if a year is a leap year? use the `java.util.Calendar` object to determine if a given year is a leap year. That was the way to do it using the old API before we have the Date and Time API introduced in Java 8.

Now, in the Java 8 API we can check if a given year is a leap year using a couple of ways. We can determine if a given date is in a leap year by calling the `isLeapYear()` method of the `java.time.LocalDate` class. While using the `java.time.Year` class we can check is the given year if a leap year using the `isLeap()` method.

The following code snippet will show you how to do it:

``````package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.temporal.ChronoField;

public class YearIsLeapExample {
public static void main(String[] args) {
// Using the java.time.LocalDate class.
LocalDate now = LocalDate.now();
boolean isLeap = now.isLeapYear();
System.out.printf("Year %d, leap year = %s%n", now.getYear(), isLeap);

LocalDate date = LocalDate.of(2020, Month.JANUARY, 1);
isLeap = date.isLeapYear();
System.out.printf("Year %d, leap year = %s%n", date.getYear(), isLeap);

// Using the java.time.Year class.
Year year = Year.now();
isLeap = year.isLeap();
System.out.printf("Year %d, leap year = %s%n", year.getValue(), isLeap);

Year anotherYear = Year.of(2020);
isLeap = anotherYear.isLeap();
System.out.printf("Year %d, leap year = %s%n", anotherYear.get(ChronoField.YEAR), isLeap);
}
}
``````

The code snippet will print out the following result:

``````Year 2021, leap year = false
Year 2020, leap year = true
Year 2021, leap year = false
Year 2020, leap year = true
``````

## How do I convert Date to String?

``````package org.kodejava.text;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateToString {
public static void main(String[] args) {
// Create an instance of SimpleDateFormat used for formatting
// the string representation of date (day/month/year)
String pattern = "dd/MM/yyyy";
DateFormat df = new SimpleDateFormat(pattern);

// Get the date today using Calendar object.
Date today = Calendar.getInstance().getTime();

// Using DateFormat format method we can create a string
// representation of a date with the defined format.
String reportDate = df.format(today);

// Print what date is today!
System.out.println("Report Date: " + reportDate);

// Using Java 8.
// Creates a DateTimeFormatter using the ofPattern() method. Get
// the current date by calling the .now() method of LocalDate.
// To convert to string use the format() method of the LocalDate
// and pass the formatter object as argument.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDate now = LocalDate.now();
reportDate = now.format(formatter);
System.out.println("Report Date: " + reportDate);
}
}
``````