java timestamp mysql_Java MySQL Timestamp时区问题

小编典典

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

// 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 2012)

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 2012)

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。

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

或这样:http : //puretech.paawak.com/2010/11/02/how-

to-handle-oracle-timestamp-with-timezone-from-

java/

2020-11-16

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值