如何从时区获取偏移量

huangapple go评论92阅读模式
英文:

How to get offset from TimeZone

问题

我只是想从时区中获取偏移量(+02:00),但似乎对于IST、JST、EST和BET不起作用。

TimeZone exchangeTimeZone = banker.getTimeZone();
String timeZone = ZonedDateTime.now(ZoneId.of(exchangeTimeZone.getID())).getOffset().getId();

它返回错误:"Unknown time-zone ID: EST"。我手头没有可用的日期对象。

英文:

I just wanted to get offset(+02:00) from TimeZone but seems like it is not working for IST, JST, EST, and BET.

TimeZone exchangeTimeZone = banker.getTimeZone();	
String timeZone = ZonedDateTime.now(ZoneId.of(exchangeTimeZone.getID())).getOffset().getId();

It is returning error "Unknown time-zone ID: EST". Date object is not avilable with me.

答案1

得分: 5

使用 ZoneId.SHORT_IDS

ZonedDateTime.now(ZoneId.of(exchangeTimeZone.getID(), ZoneId.SHORT_IDS))
             .getOffset().getId();
英文:

Use ZoneId.SHORT_IDS

ZonedDateTime.now(ZoneId.of(exchangeTimeZone.getID(), ZoneId.SHORT_IDS))
             .getOffset().getId();

答案2

得分: 3

你可以尝试找到一个缩写的映射,如下所示:

public static ZoneId getFromAbbreviation(String abbreviation) {
    return ZoneId.of(ZoneId.SHORT_IDS.get(abbreviation));
}

你可以像这样获取偏移量:

public static void main(String[] args) {
    ZoneId istEquivalent = getFromAbbreviation("IST");
    ZoneId estEquivalent = getFromAbbreviation("EST");
    ZoneId jstEquivalent = getFromAbbreviation("JST");
    ZoneId betEquivalent = getFromAbbreviation("BET");
    ZonedDateTime istNow = ZonedDateTime.now(istEquivalent);
    ZonedDateTime estNow = ZonedDateTime.now(estEquivalent);
    ZonedDateTime jstNow = ZonedDateTime.now(jstEquivalent);
    ZonedDateTime betNow = ZonedDateTime.now(betEquivalent);
    System.out.println("IST --> " + istEquivalent + " with offset " + istNow.getOffset());
    System.out.println("EST --> " + estEquivalent + " with offset " + estNow.getOffset());
    System.out.println("JST --> " + jstEquivalent + " with offset " + jstNow.getOffset());
    System.out.println("BET --> " + betEquivalent + " with offset " + betNow.getOffset());
}

输出为:

IST --> Asia/Kolkata with offset +05:30
EST --> -05:00 with offset -05:00
JST --> Asia/Tokyo with offset +09:00
BET --> America/Sao_Paulo with offset -03:00

正如你所看到的,EST 简单地没有一个时区名称,只有一个偏移量。

英文:

You could try to find a mapping for an abbreviation you got:

public static ZoneId getFromAbbreviation(String abbreviation) {
    return ZoneId.of(ZoneId.SHORT_IDS.get(abbreviation));
}

You could get the offsets like in this main:

public static void main(String[] args) {
	ZoneId istEquivalent = getFromAbbreviation("IST");
	ZoneId estEquivalent = getFromAbbreviation("EST");
	ZoneId jstEquivalent = getFromAbbreviation("JST");
	ZoneId betEquivalent = getFromAbbreviation("BET");
	ZonedDateTime istNow = ZonedDateTime.now(istEquivalent);
	ZonedDateTime estNow = ZonedDateTime.now(estEquivalent);
	ZonedDateTime jstNow = ZonedDateTime.now(jstEquivalent);
	ZonedDateTime betNow = ZonedDateTime.now(betEquivalent);
	System.out.println("IST --> " + istEquivalent + " with offset " + istNow.getOffset());
	System.out.println("EST --> " + estEquivalent + " with offset " + estNow.getOffset());
	System.out.println("JST --> " + jstEquivalent + " with offset " + jstNow.getOffset());
	System.out.println("BET --> " + betEquivalent + " with offset " + betNow.getOffset());
}

the output is

IST --> Asia/Kolkata with offset +05:30
EST --> -05:00 with offset -05:00
JST --> Asia/Tokyo with offset +09:00
BET --> America/Sao_Paulo with offset -03:00

As you can see, EST simply doesn't have a zone name, just an offset.

答案3

得分: 3

TimeZone.toZoneId()

// 不要在您的代码中这样做:仅用于演示目的获取一个 ID 为 EST 的 TimeZone。
TimeZone tz = TimeZone.getTimeZone("EST");

String currentOffsetString = ZonedDateTime.now(tz.toZoneId())
        .getOffset()
        .getId();
System.out.println(currentOffsetString);

运行时的输出:

> -05:00

与您在代码中使用的单参数 ZoneId.of 方法相反,TimeZone.toZoneId() 方法处理了已弃用的三个字母缩写(这可能是优势或劣势,取决于您的情况和喜好)。因此,上述代码也适用于许多此类三个字母缩写,包括 EST。

我只是犹豫是否包含上面的第一行代码。这行代码有几个问题:我们不应在代码中创建过时的 TimeZone 对象,而应依赖于现代的 ZonedId 和相关类,这些类来自于 Java 的现代日期和时间 API。我们也不应依赖于三个字母的时区缩写。它们已被弃用,未标准化,并且通常含糊不清。例如,EST 可能表示澳大利亚东部标准时间或北美东部标准时间。为了增加混淆,有些人希望您在夏季获取带有夏令时 (DST) 的东部时间。但是使用 TimeZone 不会这样。您得到的是一种全年使用标准时间的时区。对于 AST、PST 和 CST 来说情况并非如此。

然而,您通常无法控制从 API 中获得什么。如果不幸得到一个带有 ID 为 EST 的过时 TimeZone 对象,上面的代码向您展示了进行转换以进入 java.time 领域的方法。

英文:

TimeZone.toZoneId()

	// Never do this in your code: Get a TimeZone with ID EST for demonstration only.
	TimeZone tz = TimeZone.getTimeZone("EST");
	
	String currentOffsetString = ZonedDateTime.now(tz.toZoneId())
			.getOffset()
			.getId();
	System.out.println(currentOffsetString);

Output when running just now:

> -05:00

Contrary to the one-arg ZoneId.of method that you used in your code, TimeZone.toZoneId() does handle the deprecated three letter abbreviations (which may be considered an advantage or a disadvantage depending on your situation and your taste). So the above code works with many such three letter abbreviations too, including EST.

I am only hesitatingly including the first code line above. There are several things wrong with it: We should not create old-fashioned TimeZone objects in our code but rely on the modern ZonedId and related classes from java.time, the modern Java date and time API. We should not rely on three letter time zone abbreviations either. They are deprecated, not standardized and typically ambiguous. EST, for example, may mean Australian Eastern Standard Time or North American Eastern Standard Time. To increase confusion some will expect you to get Eastern Time with summer time (DST) in summer. With TimeZone you don’t. You get a time zone that uses standard time all year. The same is not true for AST, PST nor CST.

However you often cannot control what you get from an API. And if you’re unlucky enough to get an old-fashioned TimeZone object with ID EST, the above code shows you the conversion you need to get into the realm of java.time.

huangapple
  • 本文由 发表于 2020年10月15日 18:28:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64369600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定