Subtract Dates in Java
-
Use
java.util.Date
to Subtract Two Dates in Java -
Use
java.time.Duration
andjava.time.Period
to Subtract Two Dates in Java -
Use
java.time.temporal.ChronoUnit
to Subtract Two Dates in Java -
Use
java.time.temporal.Temporal
until()
to Subtract Two Dates in Java
This article explains how we can subtract two dates or get the difference between two dates in Java.
Use java.util.Date
to Subtract Two Dates in Java
The java.text.SimpleDateFormat
class is used to format and parse dates according to the specified pattern. We calculate the absolute value of the time difference between the two dates in milliseconds.
The convert()
method of the TimeUnit
class accepts two parameters, which are time duration and unit of the time duration. We create a TimeUnit
object time
and convert the milliseconds into days using the convert()
method.
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date firstDate = sdf.parse("04/22/2020");
Date secondDate = sdf.parse("04/27/2020");
long diff = secondDate.getTime() - firstDate.getTime();
TimeUnit time = TimeUnit.DAYS;
long diffrence = time.convert(diff, TimeUnit.MILLISECONDS);
System.out.println("The difference in days is : "+diffrence);
}
}
Output
The difference in days is : 5
Use java.time.Duration
and java.time.Period
to Subtract Two Dates in Java
The Duration
class measures an amount of time in seconds and nanoseconds, whereas the Period
class measures time in years, months, and days. The method atStartofDay()
appends the midnight time to the local date.
We obtain the Period
object as the difference between two dates, while we get the difference between two instants as the Duration
object using the between()
method. Duration
is preferred for shorter amounts of time.
The duration diff
is converted into days using toDays()
. Similarly, we can get the date units of Period
using getYears()
, getMonths
, and getDays()
.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.Duration;
import java.time.Period;
public class Main {
public static void main(String[] args) throws Exception {
LocalDate d1 = LocalDate.parse("2020-05-06", DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate d2 = LocalDate.parse("2020-05-30", DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate d3 = LocalDate.parse("2018-05-06", DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate d4 = LocalDate.parse("2020-01-23", DateTimeFormatter.ISO_LOCAL_DATE);
Duration diff = Duration.between(d1.atStartOfDay(), d2.atStartOfDay());
Period period = Period.between(d3, d4);
long diffDays = diff.toDays();
int years = Math.abs(period.getYears());
int months = Math.abs(period.getMonths());
int days = Math.abs(period.getDays());
System.out.println("Diffrence between dates is : "+diffDays + "days");
System.out.println("Diffrence is : "+years+" year, "+months+" months, "+days+" days");
}
}
Output
Diffrence between dates is : 24days
Diffrence is : 1 year, 8 months, 17 days
Use java.time.temporal.ChronoUnit
to Subtract Two Dates in Java
In Java 8, the Time API
represents a unit of date-time using the TemporalUnit
interface. Each unit provides an implementation of the method named between()
. This method calculates the amount of time between two temporal objects.
ChronoUnit
is the standard set of date-time units that provides unit-based access to manipulate a date, time, or date-time.
import java.time.temporal.ChronoUnit;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) throws Exception {
LocalDate dBefore = LocalDate.parse("2018-05-06", DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate dAfter = LocalDate.parse("2018-05-30", DateTimeFormatter.ISO_LOCAL_DATE);
long diff = ChronoUnit.DAYS.between(dBefore, dAfter);
System.out.println("difference is : "+diff);
}
}
Output
difference is : 24
Use java.time.temporal.Temporal
until()
to Subtract Two Dates in Java
The until()
method calculates the amount of time until another temporal in terms of the specified unit. The result will be negative if the end is before the start.
import java.time.temporal.Temporal;
import java.time.temporal.ChronoUnit;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) throws Exception {
LocalDate dBefore = LocalDate.parse("2018-05-21", DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate dAfter = LocalDate.parse("2018-05-30", DateTimeFormatter.ISO_LOCAL_DATE);
long diff = dBefore.until(dAfter,ChronoUnit.DAYS);
System.out.println("difference is : "+diff +" days");
}
}
Output
difference is : 9 days