0

I have two strings containing Dates in different formats.

For example :

String Date1 = "Fri, 25 Jan 2019 11:34:11 GMT";
String Date2 = "Thu, 24 Jan 2019 17:14:21 EST";

In JAVA, how do I calculate the time difference between them given that both of them have different timezone (like GMT and EST) above?

Also, note that Date1 and Date2 can be in any of the below-given formats :

private static final String[] formats = {
            "EEE, d MMM yyyy HH:mm:ss Z", "EEEE, MMMM d, yyyy, HH:mm Z",
            "EEE, d MMM yyyy HH:mm:ss z",
            "yyyy-MM-dd'T'HH:mm:ss'Z'",   "yyyy-MM-dd'T'HH:mm:ssZ",
            "yyyy-MM-dd'T'HH:mm:ss",      "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
            "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm:ss",
            "MM/dd/yyyy HH:mm:ss",        "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'",
            "MM/dd/yyyy'T'HH:mm:ss.SSSZ", "MM/dd/yyyy'T'HH:mm:ss.SSS",
            "MM/dd/yyyy'T'HH:mm:ssZ",     "MM/dd/yyyy'T'HH:mm:ss",
            "yyyy:MM:dd HH:mm:ss",        "yyyyMMdd", };

Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
  • It cannot be done very reliably. Three and four letter time zone abbreviations are ambiguous, so you won’t always know which time zone is the right one. EST, for example, may be Australian Eastern Standard Time or North American Eastern Standard Time. Also what time zone should be assumed of none is in the string? – Ole V.V. Jan 25 '19 at 12:35
  • Welcome to Stack Overflow. Stack Overflow is ill-suited for open-ended questions like “how do I xxx?” Better to search and research first, attempt something. Then if you encounter a problem, you can ask a much more specific question, which is what works really great here. I am voting to close this question as too broad. I recommend you study [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) (once more). – Ole V.V. Jan 25 '19 at 12:43
  • I have tried using the Date and SimpleDateFormat classes for solving this problem but I am getting unexpected solutions. For some time variables, the difference is coming negative. Moreover, I am looking for a more optimal approach to solving this. Hence the reason for posting this question on stackoverflow. – Sukhy Anand Jan 25 '19 at 12:55
  • Then better to ask about your unexpected solutions. People here are very often kind enough to also point out if there are better solutions than the one you tried. Like for example: I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 25 '19 at 13:04
  • Possible duplicate of [Java determine one dateformat from several dateformats](https://stackoverflow.com/questions/48678978/java-determine-one-dateformat-from-several-dateformats) and [Calculating the difference between two Java date instances](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Ole V.V. Jan 25 '19 at 13:29

0 Answers0