7

I want to get the difference in seconds to find whether the system timezone is ahead or behind the remote timezone. Here the remote timezone value is "GMT" which i fetch from Database. It could be "US/Eastern", which i convert to "America/New_York". But for GMT, im getting ERROR.

ZoneId.systemDefault().getRules().getOffset(Instant.now()).getTotalSeconds() 
    - ZoneId.of("GMT").getRules().getOffset(Instant.now()).getTotalSeconds()

But it gives the following error,

Exception in thread "main" java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format: 
    at java.time.ZoneOffset.of(Unknown Source)
    at java.time.ZoneId.of(Unknown Source)
    at java.time.ZoneId.of(Unknown Source)

How to resolve this error ? What to use in place of GMT ??

7
  • 2
    GMT isn't really a time zone. There's no good name I'm aware of for what it is, but it's "what's observed in the UK for about half the year". Can you provide more information about what you're trying to achieve?
    – Jon Skeet
    Sep 16, 2020 at 8:04
  • Please read the docs docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
    – Eklavya
    Sep 16, 2020 at 8:06
  • @JonSkeet - I have updated the Question..Kindly check.
    – VinodKhade
    Sep 16, 2020 at 8:18
  • "Here the remote timezone value is "GMT" which i fetch from Database" - well that's the first problem, as "GMT" isn't a time zone. UTC isn't either, very strictly speaking, but is usually accepted as one. So you probably just want to special case convert "GMT" to "UTC", assuming that's the actual meaning. But you should work out whether that is what's expected for entries in the database with "GMT" as the value. If they actually mean "UK" then you should use Europe/London instead, but that's significantly different
    – Jon Skeet
    Sep 16, 2020 at 8:23
  • 4
    "GMT" is perfectly valid input for ZoneId.of(String). No need to convert. The problem here is elsewhere. Sep 16, 2020 at 8:37

3 Answers 3

4

Use Etc/UTC

import java.time.Instant;
import java.time.ZoneId;

public class Main {
    public static void main(String args[]) {
        System.out.println(ZoneId.of("Etc/UTC").getRules().getOffset(Instant.now()).getTotalSeconds());
    }
}

Output:

0
2

The official answer is:

Etc/GMT

It’s the same as the Etc/UTC suggested in the other answers except for the name.

For the sake of completeness there are a number of aliases for the same, many of them deprecated, not all. You can find them in the link.

And I am not disagreeing with the comments telling you to prefer UTC. I just wanted to answer the question as asked.

For your case you should not need to ask at all though. ZoneId.of("GMT").getRules().getOffset(Instant.now()).getTotalSeconds() should always yield 0, so there is no need to subtract anything. I would either insert a comment why I don’t or subtract a well-named constant with the value 0.

Link: List of tz database time zones

1

It's ZoneId.of("UTC")...

Here's evidence:

public static void main(String[] args) {
    // define the ZoneId
    ZoneId utc = ZoneId.of("UTC");
    // get the current date and time using that zone
    ZonedDateTime utcNow = ZonedDateTime.now(utc);
    // define a formatter that uses O for GMT in the output
    DateTimeFormatter gmtStyleFormatter = DateTimeFormatter
                                            .ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS O");
    // and print the datetime using the default DateTimeFormatter and the one defined above
    System.out.println(utcNow + " == " + utcNow.format(gmtStyleFormatter));
}

output (some moments ago):

2020-09-16T08:02:34.717Z[UTC] == 2020-09-16T08:02:34.717 GMT

Getting the total zone offset in seconds of this zone by

utc.getRules().getOffset(Instant.now()).getTotalSeconds();

will result in 0, because this zone has no offset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.