210

I want a Java program that calculates days between two dates.

  1. Type the first date (German notation; with whitespaces: "dd mm yyyy")
  2. Type the second date.
  3. The program should calculates the number of days between the two dates.

How can I include leap years and summertime?

My code:

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class NewDateDifference {

    public static void main(String[] args) {

        System.out.print("Insert first date: ");
        Scanner s = new Scanner(System.in);
        String[] eingabe1 = new String[3];

        while (s.hasNext()) {
            int i = 0;
            insert1[i] = s.next();
            if (!s.hasNext()) {
                s.close();
                break;
            }
            i++;
        }

        System.out.print("Insert second date: ");
        Scanner t = new Scanner(System.in);
        String[] insert2 = new String[3];

        while (t.hasNext()) {
            int i = 0;
            insert2[i] = t.next();
            if (!t.hasNext()) {
                t.close();
                break;
            }
            i++;
        }

        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(insert1[0]));
        cal.set(Calendar.MONTH, Integer.parseInt(insert1[1]));
        cal.set(Calendar.YEAR, Integer.parseInt(insert1[2]));
        Date firstDate = cal.getTime();

        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(insert2[0]));
        cal.set(Calendar.MONTH, Integer.parseInt(insert2[1]));
        cal.set(Calendar.YEAR, Integer.parseInt(insert2[2]));
        Date secondDate = cal.getTime();


        long diff = secondDate.getTime() - firstDate.getTime();

        System.out.println ("Days: " + diff / 1000 / 60 / 60 / 24);
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3025448
  • 2,125
  • 2
  • 12
  • 7

17 Answers17

306

UPDATE: The original answer from 2013 is now outdated because some of the classes have been replaced. The new way of doing this is using the new java.time classes.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MM yyyy");
String inputString1 = "23 01 1997";
String inputString2 = "27 04 1997";

try {
    LocalDateTime date1 = LocalDate.parse(inputString1, dtf);
    LocalDateTime date2 = LocalDate.parse(inputString2, dtf);
    long daysBetween = Duration.between(date1, date2).toDays();
    System.out.println ("Days: " + daysBetween);
} catch (ParseException e) {
    e.printStackTrace();
}

Note that this solution will give the number of actual 24 hour-days, not the number of calendar days. For the latter, use

long daysBetween = ChronoUnit.DAYS.between(date1, date2)

Original answer (outdated as of Java 8)

You are making some conversions with your Strings that are not necessary. There is a SimpleDateFormat class for it - try this:

SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
String inputString1 = "23 01 1997";
String inputString2 = "27 04 1997";

try {
    Date date1 = myFormat.parse(inputString1);
    Date date2 = myFormat.parse(inputString2);
    long diff = date2.getTime() - date1.getTime();
    System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
    e.printStackTrace();
}

EDIT: Since there have been some discussions regarding the correctness of this code: it does indeed take care of leap years. However, the TimeUnit.DAYS.convert function loses precision since milliseconds are converted to days (see the linked doc for more info). If this is a problem, diff can also be converted by hand:

float days = (diff / (1000*60*60*24));

Note that this is a float value, not necessarily an int.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
influjensbahr
  • 3,978
  • 1
  • 12
  • 12
  • For some reasons it is returning -1 – Shajeel Afzal May 24 '15 at 20:55
  • 47
    This is a bad implementation that doesn't account leap years properly. – Groovy Ed May 27 '15 at 00:20
  • @GroovyEd - Can you explain more on why this is a bad implementation? – Jugal Shah Dec 28 '15 at 03:24
  • 4
    TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)); <3 – Guihgo Jan 03 '16 at 01:07
  • Do I need a JAR file to use TimeUnit? – Erick Mar 06 '16 at 19:06
  • 3
    @GroovyEd From what I have tested it seems that this code has no problem with leap years. Please take note that TimeUnit.Days.convert() will ignore remaining units eg converting 999 milliseconds to seconds results in 0. This means that if you use new Date() as one of the Date objects you might get one day less so take care – Klitos G. Jun 03 '16 at 11:38
  • This code predicts 93 days between the two dates, but there are 94. Try it out: https://www.timeanddate.com/date/dateadded.html?m1=1&d1=23&y1=1997&type=add&ay=&am=&aw=&ad=94&rec= Also verified in python: In [3]: x = dt.date(year=1997, day=23, month=1) In [4]: x Out[4]: datetime.date(1997, 1, 23) In [5]: y = dt.date(year=1997, day=27, month=04) In [6]: y-x Out[6]: datetime.timedelta(94) – Adam Hughes Sep 29 '16 at 15:10
  • 3
    It works for leap years too. check this `String inputString1 = "28 2 2016"; String inputString2 = "1 3 2016";` ans: 2 – Rohit Gaikwad Nov 29 '16 at 08:51
  • this answer is wrong. It's returning 364 days between 1985/01/01 and 1986/01/01 – Rodrigo de Bona Sartor Feb 09 '17 at 11:39
  • 14
    I believe this works correctly for leap years, but it messes up daylight savings time. If your locale has daylight savings time, then every year, there's one day that has 23 hours and one day that has 25 hours. This solution incorrectly assumes that every day has 24 hours. It therefore gives the wrong answer for any period that starts during non-daylight savings and ends during daylight savings. DO NOT USE THIS SOLUTION - there are better ways. – Dawood ibn Kareem Aug 24 '17 at 10:30
  • this must be accepted answer for all question about this in stack over flow +1 – Erfan Apr 09 '18 at 07:53
  • If my current date is 13th Jan 23:55:00 and new date is 14th Jan 00:59:00 than the expected output is 1 day (because day changed) but here we will get 0 :( – Jayesh Dhandha Jan 13 '20 at 07:05
  • Hey @JayeshDhandha, see my last edit where you can calculate float days yourself. In your case you'd have to round up, so days = Math.ceil(...) – influjensbahr Jan 14 '20 at 08:22
  • Hi @BasilBourque, thanks for letting me know. My answer is from 2013 so it's no surprise it was a bit outdated. I have updated the answer above. – influjensbahr Mar 29 '20 at 11:33
  • 1
    @jens108 Better, but `LocalDateTime` is the wrong class to use here as we have no time-of-day involved. Use `LocalDate` as seen in other answers. Also, the legacy date-time classes have not yet been officially deprecated. They *should* be deprecated. – Basil Bourque Mar 29 '20 at 16:10
  • 1
    `LocalDate.parse()` returns a LocalDate, not a `LocalDateTime` – Ojonugwa Jude Ochalifu Jul 06 '21 at 11:37
143

Simplest way:

public static long getDifferenceDays(Date d1, Date d2) {
    long diff = d2.getTime() - d1.getTime();
    return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
}
bart
  • 1,463
  • 1
  • 9
  • 2
  • 8
    Well, basically this is the same as [current best answer](http://stackoverflow.com/a/20165708/2821954), though this answer provides it as a function. – Andrew T. May 12 '15 at 07:52
  • 18
    Note, that this count only when time interval between two dates are larger than 24 hours (pretty obvious from the code), thus `getDifferenceDays(11PM, 4 AM nextday) == 0` – ReDetection Aug 07 '15 at 07:56
  • 1
    this implementation takes the last day as today. For instance, if I run the program with d1 = today, and d2 = yesterday , returns 0 days.. – karvoynistas Oct 28 '16 at 13:58
  • I added (1) for current day. – Sumon Bappi Apr 24 '17 at 22:31
  • There seems to be a bug in the Java util Date library. I used `Date d1 = new Date(2016,05,31,00,00,00);Date d2 = new Date(2016,06,1,00,00,00);` and both `d2.getTime()` and `d1.getTime()` returned `61425455400000`. – Nav May 24 '17 at 15:02
  • 1
    @Nav that's because there are 30 days in June. – Dawood ibn Kareem Aug 24 '17 at 10:31
  • 3
    This answer is incorrect. It doesn't deal with daylight savings time correctly. Don't use it if you want correct results. – Dawood ibn Kareem Aug 24 '17 at 10:31
107

In Java 8, you could accomplish this by using LocalDate and DateTimeFormatter. From the Javadoc of LocalDate:

LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day.

And the pattern can be constructed using DateTimeFormatter. Here is the Javadoc, and the relevant pattern characters I used:

Symbol - Meaning - Presentation - Examples

y - year-of-era - year - 2004; 04

M/L - month-of-year - number/text - 7; 07; Jul; July; J

d - day-of-month - number - 10

Here is the example:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Java8DateExample {
    public static void main(String[] args) throws IOException {
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MM yyyy");
        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        final String firstInput = reader.readLine();
        final String secondInput = reader.readLine();
        final LocalDate firstDate = LocalDate.parse(firstInput, formatter);
        final LocalDate secondDate = LocalDate.parse(secondInput, formatter);
        final long days = ChronoUnit.DAYS.between(firstDate, secondDate);
        System.out.println("Days between: " + days);
    }
}

Example input/output with more recent last:

23 01 1997
27 04 1997
Days between: 94

With more recent first:

27 04 1997
23 01 1997
Days between: -94

Well, you could do it as a method in a simpler way:

public static long betweenDates(Date firstDate, Date secondDate) throws IOException
{
    return ChronoUnit.DAYS.between(firstDate.toInstant(), secondDate.toInstant());
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 5
    Excellent example. This automatically accounts for leap year. If you checked 1991 and 1992 (leap year) it calculates correctly. Perfect! – woahguy Mar 29 '16 at 07:08
  • Great use of standard library. – dieresys Mar 16 '17 at 22:37
  • This should be the current accepted answer. Standard library use and accounts for leap years and daylight savings wont be a problem. – Pehmolelu Aug 29 '17 at 12:26
  • 3
    This is the current/modern answer (the others are outdated). It also accounts for summer time (DST). To use on Java 6 or 7, get [ThreeTen Backport](http://www.threeten.org/threetenbp/). On not-new Android [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Apr 29 '18 at 16:18
  • it doesn't take into account daylight saving time – Alex Oct 25 '19 at 15:46
69

Most / all answers caused issues for us when daylight savings time came around. Here's our working solution for all dates, without using JodaTime. It utilizes calendar objects:

public static int daysBetween(Calendar day1, Calendar day2){
    Calendar dayOne = (Calendar) day1.clone(),
            dayTwo = (Calendar) day2.clone();

    if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) {
        return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR));
    } else {
        if (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) {
            //swap them
            Calendar temp = dayOne;
            dayOne = dayTwo;
            dayTwo = temp;
        }
        int extraDays = 0;

        int dayOneOriginalYearDays = dayOne.get(Calendar.DAY_OF_YEAR);

        while (dayOne.get(Calendar.YEAR) > dayTwo.get(Calendar.YEAR)) {
            dayOne.add(Calendar.YEAR, -1);
            // getActualMaximum() important for leap years
            extraDays += dayOne.getActualMaximum(Calendar.DAY_OF_YEAR);
        }

        return extraDays - dayTwo.get(Calendar.DAY_OF_YEAR) + dayOneOriginalYearDays ;
    }
}
Pim Beers
  • 3
  • 3
John Leehey
  • 22,052
  • 8
  • 61
  • 88
  • handles daylight saving time switches nicely – Ravi Sanwal Jun 22 '15 at 19:43
  • 1
    +1 for the answer, however, want to add that we need the `Calendar dayOne = (Calendar) day1.clone(), dayTwo = (Calendar) day2.clone();` lines since they ensure that the original calendar values aren't overridden. I deleted these lines thinking them redundant and wasted an hour over the fact that my original object's values were getting overwritten from inside this function. – Rohan Kandwal Aug 17 '15 at 12:49
  • 2
    Don't forget that Month value is 0-based in Calendar Class. calendar.set(2015, 11, 30, 0, 00, 00); actually means 30/12/2015 – Dmitry Jan 05 '17 at 14:30
24

The best way, and it converts to a String as bonus ;)

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        //Dates to compare
        String CurrentDate=  "09/24/2015";
        String FinalDate=  "09/26/2015";

        Date date1;
        Date date2;

        SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy");

        //Setting dates
        date1 = dates.parse(CurrentDate);
        date2 = dates.parse(FinalDate);

        //Comparing dates
        long difference = Math.abs(date1.getTime() - date2.getTime());
        long differenceDates = difference / (24 * 60 * 60 * 1000);

        //Convert long to String
        String dayDifference = Long.toString(differenceDates);

        Log.e("HERE","HERE: " + dayDifference);
    }
    catch (Exception exception) {
        Log.e("DIDN'T WORK", "exception " + exception);
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SoVinceble
  • 486
  • 5
  • 10
22

Use:

public int getDifferenceDays(Date d1, Date d2) {
    int daysdiff = 0;
    long diff = d2.getTime() - d1.getTime();
    long diffDays = diff / (24 * 60 * 60 * 1000) + 1;
    daysdiff = (int) diffDays;
    return daysdiff;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
saidesh kilaru
  • 740
  • 2
  • 10
  • 18
  • 1
    Does this account for leap years and daylight savings? – Max Alexander Hanna Apr 12 '17 at 20:58
  • 1
    @MaxAlexanderHanna it accounts for leap years correctly, but not daylight savings. It only gives the correct answer whenever a period starts during non-daylight savings time but ends during daylight savings. In all other cases, it's off by one. – Dawood ibn Kareem Aug 24 '17 at 10:35
  • 1
    @saidesh_kilaru What is the "+ 1"? I think you should remove it. – Alisa May 03 '18 at 15:55
  • I had a problem that casting to INT resulted in 4 and casting to float resulted in 4.9, so thats not really what I wanted; Maybe not clearly enough described the cases for date1 at 23:59 and date2 at 00:01 and what the expected result would be. – EricG Nov 28 '18 at 12:15
14

Java date libraries are notoriously broken. I would advise to use Joda Time. It will take care of leap year, time zone and so on for you.

Minimal working example:

import java.util.Scanner;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateTestCase {

    public static void main(String[] args) {

        System.out.print("Insert first date: ");
        Scanner s = new Scanner(System.in);
        String firstdate = s.nextLine();
        System.out.print("Insert second date: ");
        String seconddate = s.nextLine();

        // Formatter
        DateTimeFormatter dateStringFormat = DateTimeFormat
                .forPattern("dd MM yyyy");
        DateTime firstTime = dateStringFormat.parseDateTime(firstdate);
        DateTime secondTime = dateStringFormat.parseDateTime(seconddate);
        int days = Days.daysBetween(new LocalDate(firstTime),
                                    new LocalDate(secondTime)).getDays();
        System.out.println("Days between the two dates " + days);
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Julien
  • 1,302
  • 10
  • 23
  • 4
    This answer could be improved in a few ways. (a) Specify time zone rather than rely on the JVM's default. So when when creating that DateTimeFormatter, add a call to `withZone( DateTimeZone.forID( "Europe/Berlin" ) )`. (b) Why use `LocalDate` in the `daysBetween` call? Just pass DateTime objects (firstTime, secondTime). For full days, call `withTimeAtStartOfDays`. (c) I would use variable names `firstDateTime` rather then `firstTime` to avoid ambiguity between date, time, and date-time objects. (d) Add some try-catch to handle bad data input that does not match our expected format. – Basil Bourque Mar 14 '14 at 06:08
9
String dateStart = "01/14/2015 08:29:58";
String dateStop = "01/15/2015 11:31:48";

//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

Date d1 = null;
Date d2 = null;

d1 = format.parse(dateStart);
d2 = format.parse(dateStop);

//in milliseconds
long diff = d2.getTime() - d1.getTime();

long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);

System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
Gautam Viradiya
  • 517
  • 7
  • 11
7

want to get just days(no times) you can use ChronoUnit

ChronoUnit.DAYS.between(date1.toLocalDate(), date2.toLocalDate());
YMG
  • 498
  • 6
  • 15
4

We can make use of LocalDate and ChronoUnit java library, Below code is working fine. Date should be in format yyyy-MM-dd.

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
class Solution {
    public int daysBetweenDates(String date1, String date2) {
        LocalDate dt1 = LocalDate.parse(date1);
        LocalDate dt2= LocalDate.parse(date2);

        long diffDays = ChronoUnit.DAYS.between(dt1, dt2);

        return Math.abs((int)diffDays);
    }
}
3

When I run your program, it doesn't even get me to the point where I can enter the second date.

This is simpler and less error prone.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test001 {

    public static void main(String[] args) throws Exception {

        BufferedReader br = null;

        br = new BufferedReader(new InputStreamReader(System.in));
        SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy");

        System.out.println("Insert first date : ");
        Date dt1 = sdf.parse(br.readLine().trim());

        System.out.println("Insert second date : ");
        Date dt2 = sdf.parse(br.readLine().trim());

        long diff = dt2.getTime() - dt1.getTime();

        System.out.println("Days: " + diff / 1000L / 60L / 60L / 24L);

        if (br != null) {
            br.close();
        }
    }
}
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
1
// date format, it will be like "2015-01-01"
private static final String DATE_FORMAT = "yyyy-MM-dd";

// convert a string to java.util.Date
public static Date convertStringToJavaDate(String date)
        throws ParseException {
    DateFormat dataFormat = new SimpleDateFormat(DATE_FORMAT);
    return dataFormat.parse(date);
}

// plus days to a date
public static Date plusJavaDays(Date date, int days) {
    // convert to jata-time
    DateTime fromDate = new DateTime(date);
    DateTime toDate = fromDate.plusDays(days);
    // convert back to java.util.Date
    return toDate.toDate();
}

// return a list of dates between the fromDate and toDate
public static List<Date> getDatesBetween(Date fromDate, Date toDate) {
    List<Date> dates = new ArrayList<Date>(0);
    Date date = fromDate;
    while (date.before(toDate) || date.equals(toDate)) {
        dates.add(date);
        date = plusJavaDays(date, 1);
    }
    return dates;
}
Dayong
  • 5,614
  • 1
  • 14
  • 6
1

The following works perfectly well for me:

public int daysBetween(LocalDate later, LocalDate before) {
        SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
        int daysBetween = 0;
        try {
            Date dateBefore = myFormat.parse(localDateToString(before));
            Date dateAfter = myFormat.parse(localDateToString(later));
            long difference = dateAfter.getTime() - dateBefore.getTime();
            daysBetween = (int) (difference / (1000 * 60 * 60 * 24));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return daysBetween;
    }

    public String localDateToString(LocalDate date) {
        DateTimeFormatter myFormat = DateTimeFormatter.ofPattern("dd MM yyyy");
        return date.format(myFormat).toString();
    }
Edor Linus
  • 826
  • 8
  • 9
1

All the other answers had lots of scary things, here's my simple solution:

public int getDaysDiff(Date dateToCheck) 
{
    long diffMilliseconds = new Date().getTime() - dateToCheck.getTime();
    double diffSeconds = diffMilliseconds / 1000;
    double diffMinutes = diffSeconds / 60;
    double diffHours = diffMinutes / 60;
    double diffDays = diffHours / 24;
        
    return (int) Math.round(diffDays);
}
0
public class TestCode {

    public static void main(String[] args) {        
        String date1 = "23-04-2021";
        String date2 = "24-05-2021";
        System.out.println("NDays: " + nDays_Between_Dates(date1, date2));      
    }
    
    public static int nDays_Between_Dates(String date1, String date2) {
        int diffDays = 0;
        try {
            SimpleDateFormat dates = new SimpleDateFormat("dd-MM-yyyy");
            Date startDate = dates.parse(date1);
            Date endDate = dates.parse(date2);
            long diff = endDate.getTime() - startDate.getTime();
            diffDays = (int) (diff / (24 * 60 * 60 * 1000));            
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Math.abs(diffDays);
    }
}

Output: NDays: 31

Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25
0

Use following to covert the timestamp from sql Timestamp values and calculate the date differances.

        SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy");

        //Setting dates
        Date ExDate = dates.parse(dates.format(Timestamp.valueOf(ex_date)));
        Date today = dates.parse(dates.format(new Timestamp(System.currentTimeMillis())));

        //Comparing dates
        long differenceDates = Math.abs(ExDate.getTime() - today.getTime()) / (24 * 60 * 60 * 1000);

        //Convert long to String
        String dayDifference = Long.toString(differenceDates); 
        System.out.println(dayDifference);
Sandun Susantha
  • 1,010
  • 1
  • 10
  • 11
-1
public static String dateCalculation(String getTime, String dependTime) {
    //Time A is getTime that need to calculate.
    //Time B is static time that Time A depend on B Time and calculate the result.

    Date date = new Date();
    final SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd H:mm:ss");
    Date dateObj = null;
    Date checkDate = null;

    try {
        dateObj = sdf.parse(getTime);
    } catch (ParseException e) {
        e.printStackTrace();
        return "0";
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    String checkInDate = dateFormat.format(dateObj).toString();
    Date defaultTime = null;
    try {
        defaultTime = dateFormat.parse(dependTime);
        checkDate = dateFormat.parse(checkInDate);
    } catch (ParseException e) {
        e.printStackTrace();
        return "0";
    }

    try {
        if (dateFormat.parse(dateFormat.format(date)).after(defaultTime)) {
            long diff = checkDate.getTime() - defaultTime.getTime();
            Log.e("Difference", "onBindViewHolder: Difference: " + dateObj + " : " + defaultTime + " : " + diff);
            if (diff > 0) {
                long diffSeconds = diff / 1000 % 60;
                long diffMinutes = diff / (60 * 1000) % 60;
                long diffHours = diff / (60 * 60 * 1000);

                return "Late: " + diffHours + " Hour, " + diffMinutes + " Minutes, " + diffSeconds + " Sec";
            } else {
                return "0";
            }
        }
    } catch (ParseException e) {
        e.printStackTrace();
        return "0";
    }
    return "0";
}