Java – Get Different Days Between Two Days

If you are looking for how to get different days between two days in Java, this post will show you a simple way for getting it.

To do this in Java, we simply minus two days. Let’s take a look into the below code example for more details.

Example.java
package com.bytenota;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Example {
    
    public static String DATE_FORMAT = "yyyy-MM-dd";
    
    public static void main(String[] args) throws Exception {
        
        // convert date string to Date object
        Date date1 = (new SimpleDateFormat(DATE_FORMAT)).parse("2022-01-01");
        Date date2 = (new SimpleDateFormat(DATE_FORMAT)).parse("2022-01-16");
        
        // get different days between date1 and date 2
        long diff = date1.getTime() - date2.getTime();
        long days = (diff / (1000*60*60*24));
        int diffDays = Math.abs((int) days);
        
        System.out.println(diffDays); // output: 15
    }
}

In the above code example, we convert to Date object from string using SimpleDateFormat, then minus date1 and date2 to get different days.

For reuse, we can move it to a helper class:

Example.java
package com.bytenota;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeHelper {
    
    // method 1: input are in date string
    public static int getDiffDaysFromDateString(String dateStr1, String dateStr2, String dateFormat) throws ParseException {
        Date date1 = convertStringToDate(dateStr1, dateFormat);
        Date date2 = convertStringToDate(dateStr2, dateFormat);
        
        long diff = date1.getTime() - date2.getTime();
        long days = (diff / (1000*60*60*24));
        int diffDays = Math.abs((int) days);
        return diffDays;
    }
    
    // method 2: input are in Date object
    public static int getDiffDaysFromDateObject(Date date1, Date date2) throws ParseException {
        long diff = date1.getTime() - date2.getTime();
        long days = (diff / (1000*60*60*24));
        int diffDays = Math.abs((int) days);
        return diffDays;
    }
    
    // convert string to Date object
    public static Date convertStringToDate(String strDate, String dateFormat) throws ParseException {
        Date date = (new SimpleDateFormat(dateFormat)).parse(strDate);
        return date;
    }
}

Then we can use our DateTimeHelper class in other classes:

AnotherExample.java
package com.bytenota;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AnotherExample {
    
    public static String DATE_FORMAT = "yyyy-MM-dd";
   
    public static void main(String[] args) throws Exception {
        // test helper class
        int diffDays = DateTimeHelper.getDiffDaysFromDateString("2022-01-01", "2022-01-20", DATE_FORMAT);
        System.out.println(diffDays); // output: 19
    }
}

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x