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
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] 삭제
몇 마디 만하겠습니다