0

I have to compare 2 dates in Java 8 to get the difference of minutes. Now I am using java.utils.Date to store my dates but I am unable to find out how to get the difference in minutes. Also, the dates to be compared might be in 2 different time zones which should be taken into account if present. How do I proceed with this?

Example dates to be compared :

Mon Oct 12 12:20:00 IST 2020
Mon Oct 05 09:56:57 GMT 2020
  • 4
    I recommend you don’t use `jaca.util.Date`. That class is poorly designed and long outdated. Instead use `ZonedDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 05 '20 at 04:43
  • 1
    Does this answer your question? [Java - Time difference in minutes](https://stackoverflow.com/questions/40285248/java-time-difference-in-minutes) – Eklavya Oct 05 '20 at 04:52
  • 1
    I would think not, @Eklavya. This question is about two different time zone. That other question has an answer with two `ZonedDateTime` objects, but they are both in the same time zone. – Ole V.V. Oct 05 '20 at 05:40

1 Answers1

2

The time zone difference itself does not pose any problem. Java handles that nicely. You have a problem in IST being ambiguous, though, it may stand for Irish Summer Time, Israel Standard Time, India Standard Time or something else.

java.time

I recommend you use java.time, the modern Java date and time API, for your date and time work.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz uuuu", Locale.ENGLISH);
    
    String aDateTimeString = "Mon Oct 12 12:20:00 IST 2020";
    String anotherDateTimeString = "Mon Oct 05 09:56:57 GMT 2020";
    
    ZonedDateTime aDateTime = ZonedDateTime.parse(aDateTimeString, formatter);
    ZonedDateTime anotherDateTime = ZonedDateTime.parse(anotherDateTimeString, formatter);
    long differenceInMinutes = ChronoUnit.MINUTES.between(anotherDateTime, aDateTime);
    
    System.out.format("The times are %s and %s%n", aDateTime, anotherDateTime);
    System.out.format("Difference is %d minutes%n", differenceInMinutes);

Output is:

The times are 2020-10-12T12:20Z[Atlantic/Reykjavik] and 2020-10-05T09:56:57Z[GMT]
Difference is 10223 minutes

Java has interpreted IST as Icelandic time. You might not have intended that. But the calculation of difference across time zones works.

I provide a link below to how to control how Java interprets IST.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thank you so much for your helpful answer! I also read your other answer to control the IST interpretation. Should work fine. But I have another question, what if I can convert both dates to a standard time zone (like UTC) before comparison? That would be better right? If yes, how can I do that? – Brijesh Choudhary Oct 05 '20 at 05:24
  • 1
    I honestly don’t see how converting to a common standard would be better. You may of course. `Instant anInstant = aDateTime.toInstant();`. And similarly for the other date and time. The `ChronoUnit.MINUTES.between` method that I am using will accept two `Instant` objects too and will produce the same result. – Ole V.V. Oct 05 '20 at 05:36
  • Hello Ole V.V. Is there a way to get timezone from a unique 3 letter city code? I mean like airline city codes. for example if I have LON as the departure city, and I need the time in that particular zone, how can I get it based on "LON" ? (Since the timezone codes are a bit ambigous (eg. IST) as discussed before). – Brijesh Choudhary Oct 08 '20 at 03:56
  • 1
    Not that I know of, @BrijeshChoudhary. There may exist a database somewhere. You may try searching. – Ole V.V. Oct 09 '20 at 04:58