在这篇文章中,我们将为您详细介绍java.util.TimeZone日光信息如何更新?的内容。此外,我们还会涉及一些关于.NetTimeZoneInfo.ConvertTimeFromUtc标准时区条
在这篇文章中,我们将为您详细介绍java.util.TimeZone日光信息如何更新?的内容。此外,我们还会涉及一些关于.Net TimeZoneInfo.ConvertTimeFromUtc 标准时区条目的问题、0078 Java与MySQL时间戳传递/存储/协调问题--userLegacyDatetimeCode--userTimezone--serverTimezone、Calendar.getInstance(TimeZone.getTimeZone(“ UTC”))没有返回UTC时间、Django DateTimeRangeField:默认= [timezone.now()]-[timezone.now()] + [10年]的知识,以帮助您更全面地了解这个主题。
本文目录一览:- java.util.TimeZone日光信息如何更新?
- .Net TimeZoneInfo.ConvertTimeFromUtc 标准时区条目的问题
- 0078 Java与MySQL时间戳传递/存储/协调问题--userLegacyDatetimeCode--userTimezone--serverTimezone
- Calendar.getInstance(TimeZone.getTimeZone(“ UTC”))没有返回UTC时间
- Django DateTimeRangeField:默认= [timezone.now()]-[timezone.now()] + [10年]
java.util.TimeZone日光信息如何更新?
我找不到周围类似的问题,所以就到这里。
我需要创建一些警报,并基本上确定夏令时的临近时间,即 下周四夏令时开始
问题 时区如何知道正确的信息?
我之所以要求这样做,是因为,例如,在我们的国家(巴西)中,将日期定义为政府将要发生的日期的前一年。它甚至可能在特定年份不存在。这在其他国家也可能发生。
在Java
Doc上,可以找到以下信息:
如果基础TimeZone实现子类支持历史夏时制时间表和GMT偏移量更改,则此方法将返回历史上正确的偏移量值
它说它返回了 历史上正确的 。但是这种检测时区的方法是否值得信赖?它是否取决于常规的Java更新,例如,它是否可以从Windows中的某个位置读取?
提前致谢!
答案1
小编典典时区更新工具
以下链接说明了使用“ 时区更新程序工具” 在Java中更新时区的官方方法。
https://www.oracle.com/java/technologies/javase/tzupdater-
readme.html
基本上,下载工具罐并运行命令
java -jar tzupdater.jar options
.Net TimeZoneInfo.ConvertTimeFromUtc 标准时区条目的问题
您的代码正在从 IST 转换为 UTC,然后再转换为 EST。 UTC 中间阶段不是必需的,但您似乎在问如何做相反的事情 - 从 EST 转换为 IST。
// Your input value (2021-01-09 10:00:00 AM)
DateTime stDate = new DateTime(2021,1,9,10,0); // Unspecified kind is the default
// The time zone of that input value
TimeZoneInfo eventZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
// The time zone you want to convert to
TimeZoneInfo targetZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
// Do the conversion
DateTime convertedTime = TimeZoneInfo.ConvertTime(stDate,eventZone,targetZone);
// result: 2021-01-09 8:30:00 PM
Working example here.
此代码专门转换为印度标准时间。如果您打算使用用户的本地时区而不是始终使用 IST(如果这是在桌面或移动应用程序中完成的,而不是在网络服务器上完成的),那么您可以将 TimeZoneInfo.Local
替换为目标区域。
0078 Java与MySQL时间戳传递/存储/协调问题--userLegacyDatetimeCode--userTimezone--serverTimezone
00. 基本问题
0.0 版本: 驱动5.1.47和8.0.17 0.1 MySQL驱动5.1有userLegacyDatetimeCode和userTimezone两个参数, 8.0没有 0.2 Java与MySQL间传递时间戳的时候, 传递的是年月日时分秒, 没有时区 0.3 MySQL传递回来的是: MySQL读取到底层存储的时间戳, 按照当前连接(MySQL侧)的时区转为年月日时分秒
0.4 但是, 两个系统时区可能会不同, userLegacyDatetimeCode和userTimezone就是用来协调时区的
01. MySQL驱动5.1
1.1 数据库连接在建立时, 会创建一个Calendar对象保存在连接中, 其中保存了连接创建时的时区, 即下文的"连接时区". 见ConnectionImpl#705
1.2 如果配置了serverTimezone,则会将其保存到连接中, 即下文的"配置时区". 见ConnectionImpl#1978 1.3 userLegacyDatetimeCod=true&userTimezone=false, 这是默认情况 1.3.1 此时对应Java和MySQL时区相同
1.3.2 Java接收到MySQL传递来的年月日时分秒, 加上"连接时区"创建时间戳java.sql.Timestamp, 见ResultSetImpl#5877和TimeUtil#369 1.4 userLegacyDatetimeCod=true&userTimezone=true&serverTimezone=GMT%2B6 1.4.0 userTimezone=true, 必须在userLegacyDatetimeCod=true时才有效
1.4.1 此时对应二者时区不同 1.4.2 与3.2相同, 先将年月日时分秒+"连接时区", 创建时间戳
1.4.3 再进行时区调整, 调整为"配置时区". 见ResultSetImpl#5877和TimeUtil#160 1.5 userLegacyDatetimeCod=false&serverTimezone=GMT%2B6
1.5.1 此时对应二者时区不同 1.5.2 将年月日时分秒+"配置时区"创建时间戳. 见ResultSetImpl#5874
1.5.3 这也是8.0的处理方式
02. MySQL驱动8.0
2.1 8.0没有userLegacyDatetimeCode和userTimezone两个参数 2.2 一定要配置serverTimezone为MySQL运行的时区. 连接建立时会将这个时区存储到连接中. 见NativeProtocol#2147#2158 2.3 将年月日时分秒+"配置时区"构造时间戳. 见SqlTimestampValueFactory#100. 这里的cal就是在#68根据"配置时区"创建的
03. 代码跟踪中的一些关键点
版本5.1
连接初始化的过程
- ConnectionImpl#1978 "配置时区"
- ConnectionImpl#705 将当前时区保存到了数据库连接中
读取的过程
- MyBatis的各个TypeHandler
- ByteArrayRow#63 拿到字节数组
- ResultSetRow#705 将字节数组转为字符串
- ResultSetImpl#5729 将字符串分离为年月日时分秒
- ResultSetImpl#5873 对应上文01.5.2
- ResultSetImpl#5877 对应上文01.4.2和01.4.3
- ResultSetImpl#5317 如果Java要返回的是String, 则会在这里将时间戳转为jvm当前时区下的年月日时分秒
版本8.0
- NativeProtocol#2147#2158 保存"配置时区"
- ByteArrayRow#89 拿到数据库返回的字节, 大致相当于 01.5.1的ByteArrayRow#63
- MysqlTextValueDecoder#338 解析字节数组, 拿到年月日时分秒并封装为InternalTimestamp
- SqlTimestampValueFactory#100 也就是上文02.3
- StringValueFactory#94 如果Java要返回的是String, 就直接将InternalTimestamp转为字符串, 不考虑当前系统时区了, 与5.1的第7条有区别
- AbstractResultSetRow#78
- PropertyKey jdbc url的property的key枚举类, https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-configuration-properties.html
04. The server time zone value ''???DZ?ʱ?'' is unrecognized or represents more than one time zone 异常是怎么回事?
在三种情况下会抛出 上文01.4/01.5/02.2情况下未配置serverTimezone是都会抛出 因为, NativeProtocol#2130或者ConnectionImpl#1960拿到数据库的system_time_zone是乱码, 也就是select @@system_time_zone 的值 所以要配置serverTimezone为数据库运行的时区
05. 问题
时间戳传递为什么不是一个数字形式的秒/毫秒呢, 而是一个没有时区的年月日时分秒呢? 还得协调时区, 多复杂呢?
Calendar.getInstance(TimeZone.getTimeZone(“ UTC”))没有返回UTC时间
我真的对Calendar.getInstance(TimeZone.getTimeZone("UTC"))
方法调用的结果感到困惑,因为它返回了IST时间。
这是我使用的代码
Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println(cal_Two.getTime());
我得到的答复是:
Sat Jan 25 15:44:18 IST 2014
所以我尝试将默认的TimeZone更改为UTC,然后检查了一下,然后工作正常
Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println(cal_Two.getTime());
TimeZone tz = TimeZone.getDefault() ;
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Calendar cal_Three = Calendar.getInstance();
System.out.println(cal_Three.getTime());
TimeZone.setDefault(tz);
结果:
Sat Jan 25 16:09:11 IST 2014
Sat Jan 25 10:39:11 UTC 2014
我在这里想念什么吗?
Django DateTimeRangeField:默认= [timezone.now()]-[timezone.now()] + [10年]
不需要字符串操作,因为此字段的documented Python类型为DateTimeTZRange
。
我不能说我曾经使用过该字段,但是类似的东西应该可以工作:
from psycopg2.extras import DateTimeTZRange
from django.utils import timezone
from datetime import timedelta
def next_ten_years():
now = timezone.now()
# use a more accurate version of "10 years" if you need it
return DateTimeTZRange(now,now + timedelta(days=3652))
class MyModel(models.Model):
...
active_in = models.DateTimeRangeField(default=next_ten_years)
今天关于java.util.TimeZone日光信息如何更新?的介绍到此结束,谢谢您的阅读,有关.Net TimeZoneInfo.ConvertTimeFromUtc 标准时区条目的问题、0078 Java与MySQL时间戳传递/存储/协调问题--userLegacyDatetimeCode--userTimezone--serverTimezone、Calendar.getInstance(TimeZone.getTimeZone(“ UTC”))没有返回UTC时间、Django DateTimeRangeField:默认= [timezone.now()]-[timezone.now()] + [10年]等更多相关知识的信息可以在本站进行查询。
本文标签: