i have some problem while finding difference between times, if i try to find difference in todays time (say t1 = \"08:00:00\" and t2 = \"10:00:00\" then it is giving correct
See some options here
such as :
LocalDate today = LocalDate.now()
LocalDate yeasterday = today.minusDays(1);
Duration oneDay = Duration.between(today, yesterday);
Duration.between(today.atTime(0, 0), yesterday.atTime(0, 0)).toDays() // another option
String time1 = "08:00:00";
String time2 = "17:00:00";
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = format.parse(time1);
Date date2 = format.parse(time2);
long difference = date2.getTime() - date1.getTime();
Not using Java 8 or Joda-Time, and ensuring that local time zone DST is not involved.
public static void printDiff(String time1, String time2) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
int seconds = (int)(df.parse(time2).getTime() - df.parse(time1).getTime()) / 1000;
if (seconds < 0)
seconds += 24 * 60 * 60;
int minutes = seconds / 60;
int hours = minutes / 60;
seconds %= 60;
minutes %= 60;
System.out.printf("There are %d hours %d minutes %d seconds from %s to %s%n",
hours, minutes, seconds, time1, time2);
}
Test
public static void main(String[] args) throws Exception {
printDiff("08:00:00", "10:00:00");
printDiff("20:00:00", "08:00:00");
printDiff("15:47:22", "11:12:38");
printDiff("08:00:00", "07:59:59");
printDiff("08:00:00", "08:00:00");
}
Output
There are 2 hours 0 minutes 0 seconds from 08:00:00 to 10:00:00
There are 12 hours 0 minutes 0 seconds from 20:00:00 to 08:00:00
There are 19 hours 25 minutes 16 seconds from 15:47:22 to 11:12:38
There are 23 hours 59 minutes 59 seconds from 08:00:00 to 07:59:59
There are 0 hours 0 minutes 0 seconds from 08:00:00 to 08:00:00
Any date/time manipulation/calculation should be done though the use of well defined and tested APIs like Java 8's Time API or Joda Time
public class TestTime {
public static void main(String[] args) {
// Because of the ability for the time to roll over to the next
// day, we need the date component to make sense of it, for example
// 24:00 is actually 00:00 of the next day ... joy
LocalDateTime t1 = LocalTime.of(20, 00).atDate(LocalDate.now());
LocalDateTime t2 = LocalTime.of(12, 00).atDate(LocalDate.now());
LocalDateTime t3 = LocalTime.MIDNIGHT.atDate(LocalDate.now()).plusDays(1);
if (t1.isAfter(t2)) {
System.out.println("Before");
Duration duration = Duration.between(t2, t3);
System.out.println(format(duration));
} else {
System.out.println("After");
Duration duration = Duration.between(t2, t1);
System.out.println(format(duration));
}
}
public static String format(Duration duration) {
long hours = duration.toHours();
duration = duration.minusHours(hours);
return String.format("%02d hours %02d minutes", hours, duration.toMinutes());
}
}
Which outputs
12 hours 00 minutes
The question I can't seem to answer in your code is why you did this s4 = s2+s3;
, basically adding 12:00
to 24:00