1

Is there an option in JodaTime, where once a ISO valid string is parsed with time zone offset, I can get back the time zone?

String dateTime = "2012-11-29T23:08:56.23-04:00"

where -04:00 is the time zone offset

Once this is parsed, is there any built-in Joda API that can give me the time zone for the above parsed string? Eg. America/Dominica

Or is this even a valid problem to begin with , based on the discussion here

Community
  • 1
  • 1
Gowrav
  • 289
  • 1
  • 4
  • 20
  • More importantly, a given time zone may have more than one offset depending on the day (due to daylight savings) - meaning that the result of your conversion will depend on the date itself, which seems counter-intuitive and error prone... – assylias Jul 27 '16 at 14:45
  • @assylias: So are you saying converting from an offset to a time zone may be error prone? – Gowrav Jul 27 '16 at 15:03
  • I'm saying that, for example, you may say `+00:00` converts to `Europe/London` on January 1st. But `Europe/London` is `+01:00` on July 1st. You should explain what you are trying to achieve as this sounds like an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) at the moment. – assylias Jul 27 '16 at 15:57
  • @assylias :I am just trying to see given a iso valid datetime string with a time zone offset, whether or not i can be able to retrieve the time zone from the time zone offset . Since form the post [here](http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices) says **"One can determine the correct offset, given a time zone and a datetime. But one cannot determine the correct time zone given just an offset." ** – Gowrav Jul 27 '16 at 16:04
  • true I made it a XY problem, i will rephrase the question . thanks ,without knowing i fell into the hole as well – Gowrav Jul 27 '16 at 17:03
  • You have rephrased but you still don't explain *why* you want to convert an offset to a timezone - what do you plan to do with the time zone after that? This is crucial to define the correct approach. – assylias Jul 27 '16 at 17:46
  • @assylias I want to convert an offset to a timezone, so that I can validate the zone against a set of available enterprise time zone domain – Gowrav Jul 27 '16 at 20:39

1 Answers1

1

You cannot get a single time zone since there can be (and usually are) multiple time zones with that offset at a given point in time. But you can get a list of them:

public void printZonesByOffset() {
    DateTime dateTime = DateTime.parse("2012-11-29T23:08:56.23-04:00");
    Set<DateTimeZone> matchingTimeZones = getTimeZonesByOffset(dateTime);
    System.out.println(matchingTimeZones);
}

public Set<DateTimeZone> getTimeZonesByOffset(DateTime dateTime) {
    int dateTimeOffset = dateTime.getZone().getOffset(dateTime);

    Set<String> timeZoneIds = DateTimeZone.getAvailableIDs();
    Set<DateTimeZone> matchingTimeZones = new HashSet<>();
    for (String timeZoneId : timeZoneIds) {
        DateTimeZone timeZone = DateTimeZone.forID(timeZoneId);
        int offset = timeZone.getOffset(dateTime);
        if (dateTimeOffset == offset) {
            matchingTimeZones.add(timeZone);
        }
    }
    return matchingTimeZones;
}

prints

[America/Blanc-Sablon, America/Puerto_Rico, America/Goose_Bay, America/Guyana, America/Moncton, America/Porto_Velho, America/Halifax, America/Anguilla, America/Kralendijk, America/Curacao, America/Thule, America/Grenada, America/Martinique, America/Guadeloupe, Atlantic/Bermuda, Etc/GMT+4, America/Manaus, America/Antigua, America/Eirunepe, America/Tortola, America/Rio_Branco, America/Boa_Vista, America/St_Thomas, America/Port_of_Spain, America/Aruba, America/St_Vincent, America/Montserrat, America/Marigot, America/Santo_Domingo, America/La_Paz, America/St_Kitts, America/Barbados, America/Glace_Bay, America/Lower_Princes, America/St_Lucia, America/Dominica, America/St_Barthelemy]

Watch out for "synthetic" time zones like Etc/GMT+4

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102