Reputation: 1390
Actually i am stuck into minor issue but not getting any proper solution. I have two dates in same format now i want difference between but the format would be (Year - Month - Days - Hours - Mints - Sec) and here is my code
public static void getDifferent(Date startDate, Date endDate) {
//milliseconds
long different = endDate.getTime() - startDate.getTime();
System.out.println("startDate : " + startDate);
System.out.println("endDate : " + endDate);
System.out.println("different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long monthInMili = daysInMilli * 30;
long elapsedMonths = different / monthInMili;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
Log.d("Difference", "startDate: " + startDate.toString() + " endDate: " + endDate + " Months : " + elapsedMonths + " Days :" + elapsedDays + " Hours :" +
elapsedHours + " Mint :" + elapsedMinutes + " Seconds :" + elapsedSeconds);
}
This is code is not perfect due to this line
long monthInMili = daysInMilli * 30;
So please guide me. I am not getting proper solution from long time.
Upvotes: 4
Views: 3304
Reputation: 2838
Edit: To get the months difference, you can use Calender class. Otherwise, use JodaTime library(linked at bottom of answer).
Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);
int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
int diffDay = endCalendar.get(Calendar.DAY_OF_MONTH) - startCalendar.get(Calendar.DAY_OF_MONTH);
Note that if your dates are 2013-01-31 and 2013-02-01, you get a distance of 1 month this way, which may or may not be what you want. To correct this, refer to this answer.
Before Edit
Just subtract the dates and calculate the milliseconds according to your needs
// Start Date : 01/14/2012 09:29:58
// End Date : 01/15/2012 10:31:48
public static void getDifference(Date d1, Date d2){
// HH converts hour in 24 hours format (0-23), day calculation
// must match with your date format
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
//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.");
} catch (Exception e) {
e.printStackTrace();
}
}
You can also use joda-time-library and follow this tutorial's second step to implement it.
Upvotes: 4
Reputation: 378
you have to write code like this
long different = todayDate.getTime() - meetingDate.getTime();
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long mothsInMilli = daysInMilli * 30;
long yearInMilli = mothsInMilli * 12;
long elapsedYear = different / yearInMilli;
different = different % yearInMilli;
System.out.println("--------- elapsedYear : " + elapsedYear);
long elapsedMonths = different / mothsInMilli;
different = different % mothsInMilli;
System.out.println("--------- elapsedMonths : " + elapsedMonths);
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
System.out.println("--------- elapsedDays : " + elapsedDays);
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
System.out.println("--------- elapsedHours : " + elapsedHours);
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
System.out.println("--------- elapsedMinutes : " + elapsedMinutes);
long elapsedSeconds = different / secondsInMilli;
System.out.println("--------- elapsedSeconds : " + elapsedSeconds);
Upvotes: -1