Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从带有偏移值的日期的日期获取时区信息_Java_Date_Time_Zone - Fatal编程技术网

Java 从带有偏移值的日期的日期获取时区信息

Java 从带有偏移值的日期的日期获取时区信息,java,date,time,zone,Java,Date,Time,Zone,我们有下面的日期,偏移量为-07,属于“美国/太平洋”时区 String startTime = "2020-06-12T09:30:00.000-07:00"; 我想从上述日期对象中检索时区信息(“US/Pacific”)。您可以获得在特定时刻偏移-7小时的区域列表,但它不是唯一的。例如,您可以使用以下代码: String bookingStartTime = "2020-06-12T09:30:00.000-07:00"; OffsetDateTime odt = OffsetDateTi

我们有下面的日期,偏移量为-07,属于
“美国/太平洋”
时区

String startTime = "2020-06-12T09:30:00.000-07:00";

我想从上述日期对象中检索时区信息(
“US/Pacific”
)。

您可以获得在特定时刻偏移-7小时的区域列表,但它不是唯一的。例如,您可以使用以下代码:

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);
String bookingStartTime=“2020-06-12T09:30:00.000-07:00”;
OffsetDateTime odt=OffsetDateTime.parse(bookingStartTime);
Set allZones=new HashSet();
对于(字符串z:ZoneId.getAvailableZoneIds()){
ZoneId=ZoneId.of(z);
ZoneDateTime zdt=odt.atZoneSameInstant(id);
如果(zdt.getOffset().equals(odt.getOffset()))allZones.add(z);
}
对于(字符串z:allZones)System.out.println(z);
输出为:

美国/太平洋新机场
美国/蒂华纳
SystemV/PST8PDT
美国/亚利桑那州
美国/圣伊莎贝尔
加拿大/育空
加拿大/太平洋
美国/克雷斯顿
美国/凤凰城
美国/Dawson_Creek
美国/洛杉矶
美国/怀特霍斯
美国/恩塞纳达
美国/道森
PST8PDT
美国/Hermosillo
美国/温哥华
SystemV/MST7
Etc/GMT+7
美国/尼尔森堡
美国/太平洋
墨西哥/巴亚诺特


您可以将
字符串
解析为
OffsetDateTime
(仅知道偏移量),从中提取
ZoneOffset
,使用它将每个可用的
zoneDateTime
(知道分区和偏移量)与此偏移量进行比较,并存储偏移量相等的分区

下面的方法为您提供了从
字符串
解析的
OffsetDateTime
可能的
ZoneId
列表:

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;
}
并查看输出

美国/克雷斯顿
美国/道森
美国/道森河
美国/恩塞纳达
美国/尼尔森堡
美国/赫莫西略
美国/洛杉矶
美国/凤凰城
美国/圣奥伊莎贝尔
美国/蒂华纳
美洲/温哥华
美国/怀特霍斯
加拿大/太平洋
加拿大/育空
Etc/GMT+7
墨西哥/巴亚诺特
PST8PDT
SystemV/MST7
SystemV/PST8PDT
美国/亚利桑那州
美国/太平洋
美国/太平洋新机场

请注意,仅提供偏移量,无法找到特定的
ZoneId
。您需要更多信息(如系统区域设置)来确定给定偏移量的特定区域。

不幸的是,您在该
字符串中没有任何关于时区的信息,它只告诉您
-07:00
的偏移量。此偏移量对于
“US/Pacific”
@deHaar不是唯一的或唯一的。如果我想获取所有时区信息,此特定偏移量属于您,您想找到此时具有该偏移量的所有时区吗?@deHaar是的,这很相似:
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);
}