如何在Linux操作系统中使用Java获取服务器时区信息

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

How to get Server TimeZone Information using Java in linux operating system

问题

我们在MS SQL Server中有一个名为[GetServerTimeZoneInformation]的用户定义函数,该函数返回DaylightName或StandardName、ActiveTimeBias、Bias、DaylightBias、IsDST,它使用Windows注册表键来获取这些信息。但是在使用Linux(CentOS)操作系统的情况下,我必须编写代码,使用Java来获取这些信息。有哪些Java API可以提供这些值?

英文:

We have [GetServerTimeZoneInformation] used defined function in MS SQL Server which returns DaylightName or StandardName, ActiveTimeBias, Bias, DaylightBias, IsDST It uses Windows registry key to get these information but I have to write the code where linux(CentOs) operating system is being used and I have to get these information using Java. What is the Java API available which can give these values?

答案1

得分: 1

TimeZone 类提供了访问所有或大部分这些数据的方法,例如:

TimeZone tz = TimeZone.getDefault();
String daylightName = tz.getDisplayName(true, TimeZone.LONG);
String standardName = tz.getDisplayName(false, TimeZone.LONG);
int rawOffset = tz.getRawOffset();
int daylightOffset = tz.getDSTSavings();
boolean isInDSTNow = tz.inDaylightTime(new Date());

不过需要注意的是,Java 使用自己版本的 tzdb,而不是系统范围内安装的 tzdb,因此 Java 报告的内容可能与从本地操作系统工具获取的内容存在差异,特别是对于最近发生过变更的时区。

英文:

The TimeZone class gives you access to all or most of these data, for example:

TimeZone tz = TimeZone.getDefault();
String daylightName = tz.getDisplayName(true, TimeZone.LONG);
String standardName = tz.getDisplayName(false, TimeZone.LONG);
int rawOffset = tz.getRawOffset();
int daylightOffset = tz.getDSTSavings();
boolean isInDSTNow = tz.inDaylightTime(new Date());

Note however, that Java uses its own version of tzdb, and not the system-wide installed tzdb, so there may be differences between what Java reports and what you would get from native operating system tools, especially for time zones that have been changed recently.

huangapple
  • 本文由 发表于 2020年7月26日 22:31:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63101516.html
匿名

发表评论

匿名网友

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

确定