0

I have set a time using the hh:mm:ss format in the variable a and set whatever the current time is in the variable in x, same format. And in a if statement if the current time is less then the set time i want it to outprint how many hh:mm:ss it will take to get to the set a value. Thanks

a.setHours(7);
a.setMinutes(45);
a.setSeconds(00);
currenttime.format(x);
if (x.compareTo(a)<0); //x is current time (hh:mm:ss)
{
    //how do you outprint difference between x and a
   System.out.print("You have event in: " x);
}
if (x.compareTo(a)>0 && x.compareTo(a1)<0)
{
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Not knowing the type of `x` or `a` makes it kind of hard to know how to display information about their difference. It seems they are some sort of wrapper around integer hour/minute/second values, so why don't you just extract the hour/minute/seconds of each and do the comparison based on that? – azurefrog Oct 01 '15 at 17:53
  • You could even compare string values such as "14:30:25". – laune Oct 01 '15 at 17:57
  • Go [here](http://stackoverflow.com/questions/4569476/java-difference-between-two-times) for a short answer – DSlomer64 Oct 01 '15 at 18:00

1 Answers1

1

Maybe you should have a look at java.time.Duration as detailed in this post here how-to-find-difference-between-two-joda-time-datetimes-in-minutes

Or you could also use joda-time instead and go with the details presented in this here number-of-days-between-two-dates-in-joda-time

Using the Days class with the withTimeAtStartOfDay method should work

... or in this post here how-to-calculate-difference-between-two-dates-in-years-etc-with-joda-time

... that is unless you're already using Java 8, since

Joda-Time is the de facto standard date and time library for Java. From Java SE 8 onwards, users are asked to migrate to java.time (JSR-310)

Community
  • 1
  • 1
Filip
  • 1,214
  • 10
  • 19