0

String date1 = "2020/05/08 16.38.37" String date2 = "2020/04/08 20.18.10"

I wish to subtract complete date1 with complete date2 (date and time both included) and get the result in hours in java . How to proceed ?

  • 2
    Start by reading the [DateTimeFormatter](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html) documentation. Have you ever worked with Java’s date/time classes before? – VGR May 29 '20 at 18:16
  • Does this answer your question? [Find total hours between two Dates](https://stackoverflow.com/questions/2003521/find-total-hours-between-two-dates)? Maybe in combination with this? [How to parse/format dates with LocalDateTime? (Java 8)](https://stackoverflow.com/questions/22463062/how-to-parse-format-dates-with-localdatetime-java-8) – Ole V.V. May 29 '20 at 18:27
  • @pafauk. Not a duplicate of [that linked Question](https://stackoverflow.com/questions/2003521/find-total-hours-between-two-dates). That question involves `java.util.Date` objects (moments) as inputs, whereas this Question involves strings as inputs representing date-time values without time zone or offset (not moments). – Basil Bourque May 29 '20 at 23:54
  • 2
    Does this answer your question? [Find total hours between two Dates](https://stackoverflow.com/questions/2003521/find-total-hours-between-two-dates) – RMPR May 30 '20 at 08:20
  • yes it did help –  Jun 04 '20 at 08:55

2 Answers2

2

Knowing how many hours are between 2 given timestamps is not possible unless you tell me where on the planet you want to do the math for. For example, if you ask me the minutes between 01:30 and 03:30, the answer would seem to be 120, but if due to daylight savings that so happens to be exactly the night where the clocks are moved an hour forward, the actual correct answer'd be 60.

If you never want that kind of adjustment, UTC doesn't 'suffer' from weird adjustments like this, so you can always elect to do the math in the UTC zone.

Thus, the steps:

  1. parse your strings (which represent 'local' date/times, given that they include no timezone info at all) into LocalDateTime objects.
  2. Zone these 2 objects by turning them into ZonedDateTime objects at a zone of your choosing.
  3. Now ask the API to calculate the hours, minutes, whichever one you prefer between the two.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH.mm.ss");
ZoneId zone = ZoneId.of("Europe/Amsterdam");

LocalDateTime input1 = LocalDateTime.parse("2020/05/08 16.38.37", formatter);
LocalDateTime input2 = LocalDateTime.parse("2020/04/08 20.18.10", formatter);
ZonedDateTime zoned1 = input1.atZone(zone);
ZonedDateTime zoned2 = input2.atZone(zone);

Duration duration = Duration.between(zoned1, zoned2);

long hours = duration.toHours();
System.out.println(hours);

the above would print -716 (as in, the first stamp is at least 716 hours, and less than 717 hours, before the second.... at least, if you ask someone living in Amsterdam).

NB: If you want to talk about weeks, months, etc - you want Period and not Duration.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Very good point: this is impossible unless we know the time zone. And a good answer all the way through. – Ole V.V. May 29 '20 at 18:59
  • 1
    A tiny detail, the `Period` class will tell us how many years, months and days there are between two dates, but not weeks. We can query the days and divide by 7. Or for the total number of weeks (also if over a month) use `ChronoUnit.WEEKS.between()`. – Ole V.V. May 29 '20 at 19:10
1

java.time

First don’t keep your dates and times as strings in your program. Just as you don’t use strings for keeping your numbers and Boolean values (I hope), you shouldn’t for dates and times either. Use proper date and time types from java.time, the modern Java date and time API.

Assuming that your dates and times are in some well-defined time zone, I suggest using ZonedDateTime for them.

    ZoneId zone = ZoneId.of("America/Montreal");
    ZonedDateTime dateTime1 = ZonedDateTime.of(2020, 5, 8, 16, 38, 37, 0, zone);
    ZonedDateTime dateTime2 = ZonedDateTime.of(2020, 4, 8, 20, 18, 10, 0, zone);

Finding the difference is a one-liner:

    long diffHours = ChronoUnit.HOURS.between(dateTime2, dateTime1);

    System.out.println("Difference in hours: " + diffHours);

Output from this example snippet is:

Difference in hours: 716

Please insert your desired time zone where I put America/Montreal. Choosing a different time zone may cause us to get an hour more or fewer if the transition to or from summer time (DST) lies between the two dates.

Parsing date-time input

If your dates and times are string input, the first thing you need to do with them is parse them. An example:

    DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("uuuu/MM/dd HH.mm.ss");

    String string1 = "2020/05/08 16.38.37";
    String string2 = "2020/04/08 20.18.10";

    ZonedDateTime dateTime1 = LocalDateTime.parse(string1, inputFormatter).atZone(zone);
    ZonedDateTime dateTime2 = LocalDateTime.parse(string2, inputFormatter).atZone(zone);

    System.out.println("Date and time 1: " + dateTime1);
    System.out.println("Date and time 2: " + dateTime2);`
Date and time 1: 2020-05-08T16:38:37-04:00[America/Montreal]
Date and time 2: 2020-04-08T20:18:10-04:00[America/Montreal]

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161