Reputation: 366
we have the below date having offset -07, which belong to "US/Pacific"
time zone.
String startTime = "2020-06-12T09:30:00.000-07:00";
I want to retrieve timezone info ("US/Pacific"
) from above date object.
Upvotes: 2
Views: 537
Reputation: 18588
You can parse the String
to an OffsetDateTime
(knows offset only), extract the ZoneOffset
from it, use it to compare a ZonedDateTime
(knows zone and offset) for every available ZoneId
with this offset and store the ones with equal offsets.
The following method gives you a list of possible ZoneId
s for an OffsetDateTime
parsed from the String
you have:
private static SortedSet<String> getAllZoneIdsFor(OffsetDateTime offsetDateTime) {
// initialize a data structure for the result
SortedSet<String> result = new TreeSet<>();
// extract the zone offset from the given OffsetDateTime
ZoneOffset zoneOffset = offsetDateTime.getOffset();
// then check all available zone ids for a matching offset
for (String zone : ZoneId.getAvailableZoneIds()) {
// create a ZoneId object from the string
ZoneId zoneId = ZoneId.of(zone);
// create a ZonedDateTime from the given LocalDateTime adding the zone
ZonedDateTime zonedDateTime = offsetDateTime.atZoneSameInstant(zoneId);
// and check if the extracted offset equals the one of the current zone
if (zonedDateTime.getOffset().equals(zoneOffset)) {
// and add the zone id to the result if it does
result.add(zoneId.toString());
}
}
return result;
}
You would use it in a main()
like this
public static void main(String[] args) {
// take the offset dateime string
String bookingStartTime = "2020-06-12T09:30:00.000-07:00";
// parse it to an OffsetDateTime
OffsetDateTime offsetDateTime = OffsetDateTime.parse(bookingStartTime);
// get the zones with the offset of the given OffsetDateTime
SortedSet<String> matchingZones = getAllZoneIdsFor(offsetDateTime);
// and print them all
matchingZones.forEach(System.out::println);
}
and see the output
America/Creston
America/Dawson
America/Dawson_Creek
America/Ensenada
America/Fort_Nelson
America/Hermosillo
America/Los_Angeles
America/Phoenix
America/Santa_Isabel
America/Tijuana
America/Vancouver
America/Whitehorse
Canada/Pacific
Canada/Yukon
Etc/GMT+7
Mexico/BajaNorte
PST8PDT
SystemV/MST7
SystemV/PST8PDT
US/Arizona
US/Pacific
US/Pacific-New
Please note you won't be able to find a particular ZoneId
just providing an offset. You would need more information (e.g. the system locale) in order to determine a specific zone for a given offset.
Upvotes: 2
Reputation: 328913
You can get a list of zones that have a -7h offset at that particular moment in time, but it will not be unique. For example you could use this code:
String bookingStartTime = "2020-06-12T09:30:00.000-07:00";
OffsetDateTime odt = OffsetDateTime.parse(bookingStartTime);
Set<String> allZones = new HashSet<>();
for (String z : ZoneId.getAvailableZoneIds()) {
ZoneId id = ZoneId.of(z);
ZonedDateTime zdt = odt.atZoneSameInstant(id);
if (zdt.getOffset().equals(odt.getOffset())) allZones.add(z);
}
for (String z : allZones) System.out.println(z);
And the output is:
US/Pacific-New
America/Tijuana
SystemV/PST8PDT
US/Arizona
America/Santa_Isabel
Canada/Yukon
Canada/Pacific
America/Creston
America/Phoenix
America/Dawson_Creek
America/Los_Angeles
America/Whitehorse
America/Ensenada
America/Dawson
PST8PDT
America/Hermosillo
America/Vancouver
SystemV/MST7
Etc/GMT+7
America/Fort_Nelson
US/Pacific
Mexico/BajaNorte
Upvotes: 4