Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

Java - how to simply calculate difference between two Dates in days?

0 contributions
8 points

1. Overview

In java we can calculate difference between two Dates with TimeUnit class.

2. Example of how to calculate difference between two Dates in days

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class CalculateDifferenceBetweenDays {

    public static void main(String[] args) {

        LocalDateTime of1 = LocalDateTime.of(2019, 10,  3, 16, 20, 0, 0);
        LocalDateTime of2 = LocalDateTime.of(2019, 10, 12, 16, 20, 0, 0);

        Date from = Date.from(of1.toInstant(ZoneOffset.UTC));
        Date now  = Date.from(of2.toInstant(ZoneOffset.UTC));

        long differenceDays = calculateDifferenceBetweenTwoDatesInDays(from, now);

        System.out.println("Diff in days: ");
        System.out.println(differenceDays); // 9

    public static long calculateDifferenceBetweenTwoDatesInDays(Date from, Date now) {
        long millisecondsDiff = now.getTime() - from.getTime();
        return TimeUnit.DAYS.convert(millisecondsDiff, TimeUnit.MILLISECONDS);


Diff in days:


  1. LocalDateTime - JavaDoc
  2. Date - JavaDoc
  3. TimeUnit - JavaDoc
  4. ZoneOffset - JavaDoc
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more