从一个使用不同时区(如EEST和EET)的国家获取偏移量的方法是什么?

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

How to get Offset from a Time Zone of a country which uses different time zone like EEST and EET

问题

I understand your request. Here's the translated code part:

我理解您的请求。以下是翻译好的代码部分:

// 获取设备所在国家的时区偏移量
public int getCountryTimeZoneOffset(String deviceTimeZone) {
    // 根据设备存储的时区(例如,EET)获取国家信息
    String country = getCountryFromDeviceTimeZone(deviceTimeZone);

    // 根据国家信息确定当前时区和是否使用夏令时(DST)
    int offset = calculateOffsetBasedOnCountry(country);

    // 返回正确的偏移量
    return offset;
}

// 从设备时区信息中提取国家信息
private String getCountryFromDeviceTimeZone(String deviceTimeZone) {
    // 在这里实现从设备时区信息中提取国家的逻辑
    // 例如,从 "EET" 提取出 "Finland"
    // 您可能需要使用数据映射或其他方法来实现这一点
    // 返回国家名称
    return country;
}

// 根据国家信息计算正确的时区偏移量
private int calculateOffsetBasedOnCountry(String country) {
    // 在这里实现根据国家信息确定正确偏移量的逻辑
    // 根据国家和当前日期时间信息,确定是使用 EET 还是 EEST 以及是否启用夏令时
    // 返回正确的偏移量(+2 或 +3,根据情况)
    return offset;
}

请注意,代码中的 getCountryFromDeviceTimeZonecalculateOffsetBasedOnCountry 方法需要根据您的具体需求来实现,以提取国家信息和计算正确的偏移量。这只是代码的基本结构,您需要根据您的环境和数据源进行进一步的实现。

英文:

I want to get the offset of a time zone from a country where a device is in. Let's take an example of a device which is in Finland, Finland is currently using EEST, but for some reason my device has stored the
time zone as EET. Now we know EEST and EET have different offset's (+3 and +2 respectively). So I want a java code which will give me the correct offset by taking the time zone in EET, determining if the current time zone used in this country is actually EET or EEST (or determine if DST is active or no), and returning me the correct offset.

Note: EEST and EET is just an example I have used, I would like the code to work with any country which uses more than one Time zone at different time of the year.

I cannot use Continent/city format since the Only information I have from the device is EET timezone.

答案1

得分: 2

简要概述

> 确定夏令时是否激活

ZoneId
.of("Europe/Helsinki")
.getRules()
.isDaylightSavings(
    Instant.now()
)

详细信息

EEST和EET不是时区,它们仅仅提供了一个关于可能的时区以及夏令时(DST)是否生效的模糊提示。这些伪时区没有标准化,甚至不是唯一/独立的。它们仅应在本地化呈现给用户时使用,绝不应用于数据存储、数据交换或业务逻辑。

您提到:

> 现在我们知道EEST和EET有不同的偏移量(分别为+3和+2)

不,我们不知道。对于EEST和EET暗示的偏移量没有官方/标准的定义。由EEST和EET暗示的时区在某些日期上可能有不同的偏移量,因为其政府可能会在不同的日期开始/结束夏令时。或者政府可能会在夏令时之外引入其他异常情况;政府是不可预测的。

您提到:

> 我想获取设备所在国家/地区的时区偏移。

关于通过位置确定时区,这是主观的。唯一关心呈现的时区是用户预期/期望的时区。您可以猜测可能的时区。但在关键时刻,您必须与用户明确确认。

您提到:

> Calendar calendar = …

您正在使用严重缺陷的日期时间类,这些类早已被JSR 310中定义的现代java.time类所取代。

您提到:

> 确定当前国家/地区中使用的时区是否实际上是EET或EEST(或确定夏令时是否激活)

首先,确定一个真实的时区。真实的时区名称格式为Continent/Region。例如,Africa/CasablancaPacific/Auckland。对于芬兰,唯一的时区是Europe/Helsinki

ZoneId z = ZoneId.of("Europe/Helsinki");

获取该时区定义的规则。

ZoneRules rules = z.getRules();

将当前时刻捕获为以UTC零小时-分钟-秒为偏移量的时刻。

Instant now = Instant.now();

查询该时刻的规则,询问夏令时是否生效。

boolean isDST = rules.isDaylightSavings(now);

您提到:

> 我不能使用大陆/城市格式,因为我从设备上只获得了EET时区。

那么您拥有不完整的信息,您的任务是不可能完成的。

这就像要求“美元”的当前货币汇率,但您不知道这是澳大利亚元、加拿大元、港元还是美元。

英文:

tl;dr

> determine if DST is active or no

ZoneId
.of( "Europe/Helsinki" ) 
.getRules()
.isDaylightSavings(
    Instant.now()
)

Details

EEST and EET are not time zones. They merely give an ambiguous hint as to what time zone might be intended, with an indication of whether Daylight Saving Time (DST) is in effect. These pseudo-zones are not standardized, and are not even unique/distinct. These should be used only when localizing for presentation to a user. These should never be used for data storage, data exchange, or business logic.

You said:

> Now we know EEST and EET have different offset's (+3 and +2 respectively)

No, we do not know that. There is no official/standard definition for the offsets implied by EEST and EET. The time zones implied by EEST and EET may have different offsets on some dates as their politicians may start/stop DST on different dates. Or the politicians may introduce other anomalies beside DST; politicians are unpredictable.

You said:

>I want to get the offset of a time zone from a country where a device is in.

As for determining time zone by location, that is presumptuous. The only time zone that matters for presentation is the user’s expected/desired time zone. You can guess what that might be. But where critical, you must confirm with the user explicitly.

You said:

>Calendar calendar = …

You are using terribly flawed date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

You said:

> determining if the current time zone used in this country is actually EET or EEST (or determine if DST is active or no)

First, determine a real time zone. Real time zones have a name in format of Continent/Region. For example, Africa/Casablanca or Pacific/Auckland. For Finland, the only time zone is Europe/Helsinki.

ZoneId z = ZoneId.of( "Europe/Helsinki" ) ;

Get the rules defined for that zone.

ZoneRules rules = z.getRules() ;

Capture the current moment as seen with an offset from UTC of zero hours-minutes-seconds.

Instant now = Instant.now() ;

Interrogate the rules for that moment, asking if DST is in effect.

boolean isDST = rules.isDaylightSavings( now ) ;

You said:

> I cannot use Continent/city format since the Only information I have from the device is EET timezone.

Then you have incomplete information, and your task is impossible.

This is like asking for the current money exchange rate for “dollar” but you don’t know if that is the Australia dollar, the Canada dollar, the Hong Kong dollar, or the United States dollar.

答案2

得分: -3

以下是关于时区偏移的教程参考。

时区和偏移类(Java™教程 > 日期时间 > 标准日历)

要获取UTC偏移,您可以使用 TimeZone 类。
要创建新的 TimeZone 对象,可以使用 getTimeZone 方法。

对于这个示例,我将使用 EET,或者 东欧时间

TimeZone timeZone = TimeZone.getTimeZone("EET");

_ TimeZone_ 类提供了 getOffset 方法,该方法接受一组值或Unix纪元时间戳作为参数。
我们可以使用 Calendar 对象来获取 UTC 的时间值和时间戳,使用 getTimeInMillis 方法。

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
long offset = timeZone.getOffset(calendar.getTimeInMillis());

如果我们打印 offset,将会得到以下结果。

10800000

这相当于 3小时,或者3 × 60 × 60 × 1000。

关于 DST,或者 夏令时,您可以使用以下方法:useDaylightTimegetDSTSavings

第一个方法将返回 true 如果时区遵守夏令时,而后一个方法将返回从UTC偏移中减去的毫秒数值。

夏令时在不同地区发生在不同的时间。

您可以使用 Date 对象,通过 Calendar 对象获得,来查询时间戳是否当前正在观察夏令时或不观察夏令时。

TimeZone timeZone = TimeZone.getTimeZone("EET");
Calendar calendar = Calendar.getInstance(timeZone);
boolean dst = timeZone.inDaylightTime(calendar.getTime());
英文:

Here is tutorial in reference to TimeZone offsets.
Time Zone and Offset Classes (The Java™ Tutorials > Date Time > Standard Calendar).

To get the UTC offset you can use the TimeZone class.
To create a new TimeZone object, you can use the getTimeZone method.

For this example, I'll use EET, or Eastern European Time.

TimeZone timeZone = TimeZone.getTimeZone("EET");

The TimeZone class offers the getOffset method, which takes either a set of values, or a Unix-epoch timestamp as a parameter.
We can use a Calendar object to obtain a time value for the UTC, and a timestamp using the getTimeInMillis method.

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
long offset = timeZone.getOffset(calendar.getTimeInMillis());

If we printed the offset we'd get the following.

10800000

Which is equivalent to 3 hours, or 3 × 60 × 60 × 1000.

In terms of DST, or Daylight Saving Time, you can utilize the following methods: useDaylightTime, and getDSTSavings,

The first method will return true if the time-zone observes daylight savings, while the latter will return a value in milliseconds to be negated from the UTC offset.

Daylight Saving Time occurs at different times for different areas.

You can use a Date object, obtainable via a Calendar object, to query whether the timestamp is currently observing DST or not.

TimeZone timeZone = TimeZone.getTimeZone("EET");
Calendar calendar = Calendar.getInstance(timeZone);
boolean dst = timeZone.inDaylightTime(calendar.getTime());

huangapple
  • 本文由 发表于 2023年5月26日 12:57:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337770.html
匿名

发表评论

匿名网友

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

确定