How to get difference between 2 dates in number of days,hours,minutes and seconds

SpringLearner

I want to get the number of days,hours and minutes between 2 given dates and so far I have tried in 3 different ways but none gives me the correct value.Please let me know how to get number of days,hours and minutes in between 2 given dates.

In all the ways input estdate is Sat Feb 21 12:00:00 IST 2015 1st way

Date estDate=//date object which returns Sat Feb 21 12:00:00 IST 2015
long estDateInLong=estDate.getTime();
    long currentTimeinLong=Calendar.getInstance().getTimeInMillis();
    Long diff=currentTimeinLong-estDateInLong;
    long diffSeconds = diff / 1000 % 60;  //gives 6
    long diffMinutes = diff / (60 * 1000) % 60; //gives 21
    long diffHours=diff/(60*60 * 1000) % 60;//gives 26
    long diffDay=diff/(24*60*60 * 1000) % 60;//gives 16

which is wrong so I tried the following way again

Period p=new Period(new LocalDate(estDate)), new LocalDate(currentDate);
System.out.println(p.getDays());System.out.println(p.getHours());System.out.println(p.getMinutes());

ouput

2
0
0

3rd way

int delayTimeInDays=Days.daysBetween(new LocalDate(estDate), new LocalDate(currentDate).getDays();
    int delayTimeinHours=Hours.hoursBetween(new LocalDate(estDate), new LocalDate(currentDate).getHours();
    int seconds=Seconds.secondsBetween(new LocalDate(estDate), new LocalDate(currentDate)).getSeconds();

which gives me 16,384,1382400

which is again wrong.

expected output

Current time is 2:48 pm 9th march

from 21 feb,2015 12 noon to 22 feb 12 noon: 1 day

from 22 feb,2015 12 noon to 23 feb 12 noon: 2 day

so till today 12 noon number of days=16

number of hours =2

number of minutes =50

Vihar

If I got your question right this is what you are looking for

long estDateInLong=//whatever gives you past date
long currentTimeinLong=Calendar.getInstance().getTimeInMillis();
long diff=(long)(currentTimeinLong-estDateInLong);
long diffDay=diff/(24*60*60 * 1000);
diff=diff-(diffDay*24*60*60 * 1000); //will give you remaining milli seconds relating to hours,minutes and seconds
long diffHours=diff/(60*60 * 1000);
diff=diff-(diffHours*60*60 * 1000); 
long diffMinutes = diff / (60 * 1000);
diff=diff-(diffMinutes*60*1000);
long diffSeconds = diff / 1000;
diff=diff-(diffSeconds*1000);       
System.out.println(diffDay +"\t"+diffHours+"\t"+diffMinutes+"\t"+diffSeconds);

It might be easily done using Joda(may be!) but it can be done this way too

hope this helps! Good luck

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

SQL to get number of working days between two dates

분류에서Dev

How to count fractional days between 2 dates

분류에서Dev

how to get a column with a list of dates between 2 known dates

분류에서Dev

Number of working days between 2 dates according JIRA's configuration

분류에서Dev

How to get the number of weeks and its respective dates between two given dates (VB.NET)

분류에서Dev

Calculating Time Difference between 2 Dates

분류에서Dev

how to receive all days between 2 dates in laravel

분류에서Dev

Elapsed days between two dates always giving one days difference

분류에서Dev

Excel get value between 2 dates

분류에서Dev

How to convert minutes to hours and minutes (without days)

분류에서Dev

pandas series use truncate get specified hours, minutes and seconds?

분류에서Dev

Difference in years between two dates

분류에서Dev

MDX: How to get the weeks between two dates in MDX Query

분류에서Dev

How many days between two dates are in a certain month?

분류에서Dev

Get difference between dates and format with 'DD/MM/YYYY HH:MM:SS'

분류에서Dev

Joda Time - Calculation of seconds between two dates throws an exception.

분류에서Dev

find all dates between startdate and enddate with a gap of certain number of days in php or javascript

분류에서Dev

Seconds, minutes and hours since a date

분류에서Dev

sqlite : time difference between two dates in decimals

분류에서Dev

Difference between two dates in excel 2013

분류에서Dev

Search between 2 dates in webmatrix

분류에서Dev

Difference between this 2 values?

분류에서Dev

Difference between 2 commands

분류에서Dev

Calculating the days between two given dates

분류에서Dev

Taking the Differences Between 2 Dates (R)

분류에서Dev

Reading logs with the word 'ERROR' between 2 dates

분류에서Dev

How to display all the dates between multiple two dates in a table?

분류에서Dev

how to get the difference of two points in a 2D array

분류에서Dev

SQL - Difference between SPACE(2) + '|' + SPACE(2) and ‘ | ‘?

Related 관련 기사

  1. 1

    SQL to get number of working days between two dates

  2. 2

    How to count fractional days between 2 dates

  3. 3

    how to get a column with a list of dates between 2 known dates

  4. 4

    Number of working days between 2 dates according JIRA's configuration

  5. 5

    How to get the number of weeks and its respective dates between two given dates (VB.NET)

  6. 6

    Calculating Time Difference between 2 Dates

  7. 7

    how to receive all days between 2 dates in laravel

  8. 8

    Elapsed days between two dates always giving one days difference

  9. 9

    Excel get value between 2 dates

  10. 10

    How to convert minutes to hours and minutes (without days)

  11. 11

    pandas series use truncate get specified hours, minutes and seconds?

  12. 12

    Difference in years between two dates

  13. 13

    MDX: How to get the weeks between two dates in MDX Query

  14. 14

    How many days between two dates are in a certain month?

  15. 15

    Get difference between dates and format with 'DD/MM/YYYY HH:MM:SS'

  16. 16

    Joda Time - Calculation of seconds between two dates throws an exception.

  17. 17

    find all dates between startdate and enddate with a gap of certain number of days in php or javascript

  18. 18

    Seconds, minutes and hours since a date

  19. 19

    sqlite : time difference between two dates in decimals

  20. 20

    Difference between two dates in excel 2013

  21. 21

    Search between 2 dates in webmatrix

  22. 22

    Difference between this 2 values?

  23. 23

    Difference between 2 commands

  24. 24

    Calculating the days between two given dates

  25. 25

    Taking the Differences Between 2 Dates (R)

  26. 26

    Reading logs with the word 'ERROR' between 2 dates

  27. 27

    How to display all the dates between multiple two dates in a table?

  28. 28

    how to get the difference of two points in a 2D array

  29. 29

    SQL - Difference between SPACE(2) + '|' + SPACE(2) and ‘ | ‘?

뜨겁다태그

보관