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 ?
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 ?
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:
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
.
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.
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]
Oracle tutorial: Date Time explaining how to use java.time.