600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > mysql时区时间戳_Java MySQL时间戳时区问题

mysql时区时间戳_Java MySQL时间戳时区问题

时间:2022-05-08 23:20:11

相关推荐

mysql时区时间戳_Java MySQL时间戳时区问题

时区只是查看日期的不同方式(这是一个固定的时间点).我在这里写了一个小例子(密切关注断言):

// timezone independent date (usually interpreted by the timezone of

// the default locale of the user machine)

Date now = new Date();

// now lets get explicit with how we wish to interpret the date

Calendar london = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));

Calendar paris = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"));

// now set the same date on two different calendar instance

london.setTime(now);

paris.setTime(now);

// the time is the same

assert london.getTimeInMillis() == paris.getTimeInMillis();

// London is interpreted one hour earlier than Paris (as of post date of 9th May )

String londonTime = london.get(Calendar.HOUR) + ":" + london.get(Calendar.MINUTE);

String londonTZ = london.getTimeZone().getDisplayName(london.getTimeZone().inDaylightTime(london.getTime()), TimeZone.SHORT);

System.out.println(londonTime + " " + londonTZ);

// Paris is interpreted one hour later than Paris (as of post date of 9th May )

String parisTime = paris.get(Calendar.HOUR) + ":" + paris.get(Calendar.MINUTE);

String parisTZ = paris.getTimeZone().getDisplayName(paris.getTimeZone().inDaylightTime(paris.getTime()), TimeZone.SHORT);

System.out.println(parisTime + " " + parisTZ);

此片段的输出是(结果将根据执行日期/时间而有所不同):

8:18 BST

9:18 CEST

问题中的代码片段根本没有对存储日期做任何事情.通常,数据库是为本机TimeZone配置的.我建议存储一个额外的字段,表示在解释日期时要使用的TimeZone.

修改日期(通常是固定时间点之前/之后的毫秒)通常不是(通常)一个好主意,因为这将是一个有损修改,在一年中的不同时间点会有不同的解释(由于夏令时)时间).

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。