-3

How do I subtract one hour from another as in the example below? (Date not Calendar):

Date date1 = ...
Date date2 = ...
Date dateResult = ...

dateResult = date1 - date2; //(15:00 - 13:30 = 1:30)

...
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Mamga
  • 121
  • 2
  • 12
  • You can get the milliseconds from a `Date` object and then *substract* one from the other (it's perfectly ok) - To display the new date (i.e. the `dateResult` you need to *format* it (check `SimpleDateFormatter` for how to do that) – blurfus Apr 15 '15 at 17:45
  • 1
    `Date dateResult = new Date(date1().getTime() - date2.getTime());` – Luiggi Mendoza Apr 15 '15 at 17:47
  • 1
    Hours shouldn't really be represented by a `Date` object. – RealSkeptic Apr 15 '15 at 18:13
  • I would recommend against using the Date object if possible. The Calendar object would be better, but best would be to use the Java 8 library (assuming you can). Of course, this might not be possible. – Necreaux Apr 15 '15 at 18:19
  • @LocHa Search StackOverflow for endless discussion and examples. Search terms "Java date", "joda" , and "java.time". – Basil Bourque Apr 16 '15 at 00:39
  • @Basil Bourque: I know Java8 has new Date API and Java Joda is an awesome Date/time API but It does not means we should not use Calendar for some simple purposes. It is not deprecated. – LHA Apr 16 '15 at 15:00
  • @LocHa You seem to miss the point that `java.time` was added to Java to supplant java.util.Date & .Calendar (and related classes). Date/Calendar and their brethren are confusing, troublesome, and flawed both in design and implementation. **Avoid them**. Date/Calendar is a Yugo, while java.time/Joda-Time is Acura/Lexus. If you care for details, then bother to search rather than raising a *very* old question of "What's wrong with Calendar". – Basil Bourque Apr 18 '15 at 03:12

1 Answers1

0

Here a working solution (the limitations are described below the code)

// uses a deprecated constructor, so don't do that in production code
// better and more save to use a Calendar instead
Date date1 = new Date(115, 0, 1, 15, 0);
Date date2 = new Date(115, 0, 1, 13, 30);

int millisPerDay = (int) TimeUnit.DAYS.toMillis(1);
int millisPerHour = (int) TimeUnit.HOURS.toMillis(1);
int millisPerMinute = (int) TimeUnit.MINUTES.toMillis(1);
int millisPerSecond = (int) TimeUnit.SECONDS.toMillis(1);

long millisDate1 = date1.getTime();
long millisDate2 = date2.getTime();

long millisDifference = millisDate1 - millisDate2;

int durationDays = (int) millisDifference / millisPerDay;
long remainder = millisDifference - durationDays * millisPerDay;

int durationHours = (int) remainder / millisPerHour;
remainder = remainder - durationHours * millisPerHour;

int durationMinutes = (int) remainder / millisPerMinute;
remainder = remainder - durationMinutes * millisPerMinute;

int durationSeconds = (int) remainder / millisPerSecond;

System.out.printf("%02d:%02d:%02d.%03d%n",
        durationDays,
        durationHours,
        durationMinutes,
        durationSeconds
);

What are the limitations:
- first of all, this piece of code is not tested (beside for your given example)
- the example uses a deprecated constructor of Date (never use that in new code)
- there might be range overflows if the difference between the dates are to big
- the code doesn't know anything about daylight saving times
- the code doesn't know anything about timezones
- the code doesn't know anything about leap seconds

But in following case I would assume it works correct (this needs to be proved by further testing)
- if you are only interested in pure execution duration (both dates would have the same timezone basis and because of it's pure technical aspect the daylight saving might be ignored)

So now it's up to you to decide if you take something from this snippet or better look for another solution.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69