Find ZoneId by current time in this zone

What is the best way to find ZoneId by expected hours in this ZoneId to UTC time?

For example,

Current time in UTC:
2021-10-22T11:40:37.808Z

It’s needed to find any ZoneId, where current hours in the ZoneId would be:
17

17-11 = 6
So we are looking for any ZoneId, which has:
UTC+06:00

For example, it’s:
Asia/Kashgar (UTC+06:00)

Answer

You can get all ZoneIds and the corresponding offsets.

import java.time.*;
import java.util.*;

class Main {  
  public static void main(String args[]) { 
    System.out.println(zoneIdToOffsetMap()); 
  }

private static Map<String, String> zoneIdToOffsetMap() {
        Map<String, String> result = new HashMap<>();

        LocalDateTime localDateTime = LocalDateTime.now();

        for (String zoneId : ZoneId.getAvailableZoneIds()) {
            ZoneId id = ZoneId.of(zoneId);
            // LocalDateTime -> ZonedDateTime
            ZonedDateTime zonedDateTime = localDateTime.atZone(id);
            // ZonedDateTime -> ZoneOffset
            ZoneOffset zoneOffset = zonedDateTime.getOffset();
            //replace Z to +00:00
            String offset = zoneOffset.getId().replaceAll("Z", "+00:00");

            result.put(id.toString(), offset);
        }

        return result;
    }
}

This prints a map like:

{Asia/Aden=+03:00, America/Cuiaba=-04:00, ...}

If you really want all the ZoneIds for a given offset, you can change the above code slightly to return a map of String -> List<String> or just a List<String> for a single offset as shown below.

private static List<String> getZoneIds(String target) {
        List<String> zoneIds = new ArrayList<>();

        LocalDateTime localDateTime = LocalDateTime.now();

        for (String zoneId : ZoneId.getAvailableZoneIds()) {
            ZoneId id = ZoneId.of(zoneId);
            // LocalDateTime -> ZonedDateTime
            ZonedDateTime zonedDateTime = localDateTime.atZone(id);
            // ZonedDateTime -> ZoneOffset
            ZoneOffset zoneOffset = zonedDateTime.getOffset();
            //replace Z to +00:00
            String offset = zoneOffset.getId().replaceAll("Z", "+00:00");

            if (offset.equals(target)) {
                zoneIds.add(id.toString());
            }
        }

        return zoneIds;
    }
}

This prints:

[Asia/Kashgar, Etc/GMT-6, Asia/Almaty, Asia/Dacca, Asia/Omsk, Asia/Dhaka, Indian/Chagos, Asia/Qostanay, Asia/Bishkek, Antarctica/Vostok, Asia/Urumqi, Asia/Thimbu, Asia/Thimphu]