How calculate difference between 23 to 1 (am) programmatically ?
Asked
Active
Viewed 2,215 times
-3
-
change the time in milliSecond – Pankaj Jun 12 '15 at 06:32
-
3possible duplicate of [Calculate Difference between two times in Android](http://stackoverflow.com/questions/14110621/calculate-difference-between-two-times-in-android) – Bidhan Jun 12 '15 at 06:41
-
Possible duplicate of [Calculate difference between two times android](https://stackoverflow.com/questions/18908738/calculate-difference-between-two-times-android) – Basil Bourque Jul 16 '17 at 05:53
3 Answers
2
You can do something like this:
Date date1, date2; // Insert value in date1 & date2 as your need
long dateDiff = date1.getTime() - date2.getTime();
dateDiff = ((dateDiff / (1000 * 60 * 60)) + 1) / 24;

Anshuman Borah
- 538
- 2
- 10
0
You could do something like this:
String firstTime = "14:29:50";
String secondTime = "09:12:43";
//create dates from the strings (if you have strings not dates)
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = null;
Date date2 = null;
try {
date1 = format.parse(firstTime);
date2 = format.parse(secondTime);
} catch (ParseException e) {
e.printStackTrace();
}
//get difference between the two times and then convert it in seconds, minutes or hours (what you need)
long diff = date1.getTime() - date2.getTime();
long diffSeconds = diff / 1000;
long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
System.out.println("Time as difference in seconds: " + diffSeconds + " seconds.");
System.out.println("Time as difference in minutes: " + diffMinutes + " minutes.");
System.out.println("Time as difference in hours: " + diffHours + " hours.");

Gabriella Angelova
- 2,985
- 1
- 17
- 30
-
@GabriellaAngelova but what if first time is 2:10:24 of next day . Then will we get negative diff? – dhami_ji Jan 01 '18 at 17:06
-
@dhami_ji , since the first time is in the future compared to the second time, the algorithm should work, else they should be reversed. – Gabriella Angelova Jan 02 '18 at 13:31
-
0
How says @Bidhan A, the next time searches a little is a typical question... Then if you want to calculate a datetime or difference between two dates, as first step you need to convert date in milliseconds. The theory is 1 s is 1000 ms, 1 m is 60*1000 ms, 1 hour is 60*60*1000 ms, etc... The following code calculates the difference in days,hours,minutes or seconds between actual date and a posterior date and return string in this case as result:
public String TimeDifferenceCalculating(String EarlierDate) {
//Declaration of variables
String Result = "";
Date cDate = new Date();
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cDate);
String[] array1 = date.split(" ");
String[] array_YMD1 = array1[0].split("-");
String[] array_HMS1 = array1[1].split(":");
int ActualDate_Years = Integer.parseInt(array_YMD1[0]);
int ActualDate_Months = Integer.parseInt(array_YMD1[1]);
int ActualDate_Days = Integer.parseInt(array_YMD1[2]);
int ActualDate_Hours = Integer.parseInt(array_HMS1[0]);
int ActualDate_Minutes = Integer.parseInt(array_HMS1[1]);
int ActualDate_Seconds = Integer.parseInt(array_HMS1[2]);
String[] array2 = EarlierDate.split(" ");
String[] array_YMD2 = array2[0].split("-");
String[] array_HMS2 = array2[1].split(":");
int EarlierDate_Years = Integer.parseInt(array_YMD2[0]);
int EarlierDate_Months = Integer.parseInt(array_YMD2[1]);
int EarlierDate_Days = Integer.parseInt(array_YMD2[2]);
int EarlierDate_Hours = Integer.parseInt(array_HMS2[0]);
int EarlierDate_Minutes = Integer.parseInt(array_HMS2[1]);
int EarlierDate_Seconds = Integer.parseInt(array_HMS2[2]);
Calendar calendar1 = new GregorianCalendar(ActualDate_Years, ActualDate_Months-1, ActualDate_Days,ActualDate_Hours,
ActualDate_Minutes,ActualDate_Seconds);
long ActualDate_MiliSeconds = calendar1.getTimeInMillis();
Calendar calendar2 = new GregorianCalendar(EarlierDate_Years, EarlierDate_Months-1, EarlierDate_Days,EarlierDate_Hours,
EarlierDate_Minutes,EarlierDate_Seconds);
long EarlierDate_MiliSeconds = calendar2.getTimeInMillis();
long diff = ActualDate_MiliSeconds - EarlierDate_MiliSeconds;
if(diff >= 0) {
if (diff < (24 * 60 * 60 * 1000)) {
if (diff < (60 * 60 * 1000)) {
if (diff < (60 * 1000)) {
if (diff == 1000) {
Result = "1 second";
} else {
long diffSeconds = diff / (1000);
Result = String.valueOf(diffSeconds) + " seconds";
}
} else {
if (diff < (120 * 1000)) {
Result = "1 minute";
} else {
long diffMinutes = diff / (60 * 1000);
Result = String.valueOf(diffMinutes) + " minutes";
}
}
} else {
if (diff < (120 * 60 * 1000)) {
Result = "1 hour";
} else {
long diffHours = diff / (60 * 60 * 1000);
Result = String.valueOf(diffHours) + " hours";
}
}
} else {
if (diff < (48 * 60 * 60 * 1000)) {
Result = "1 day";
} else {
long diffDays = diff / (24 * 60 * 60 * 1000);
Result = String.valueOf(diffDays) + " days";
}
}
}
else{
Result = "";
}
return Result;
}
Tell me if I helped you and good programming!

Merlí Escarpenter Pérez
- 2,569
- 3
- 26
- 49