两个不同时区的时差 [英] Time Difference of two different timezone

查看:78
本文介绍了两个不同时区的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前时间(哪个是IST)与存储在DB(EST)中的时间之差.为了做到这一点,我试图在计算差异之前将当前时间转换为EST.但它不起作用.在以下方法中,当地时间不会仅转换为EST.你能建议我更好的方法吗? getModifiedDate 的返回类型为 java.sql.Timestamp 和数据类型列的是 DATE

I want to get the difference of current time (Which is IST) and the time which is stored in DB(EST). In order to that I am trying to convert current time to EST before calculating the difference. But its not working. In the following approach, local time is not getting converted to EST only. Could you please suggest me the better way to do it ? The return type of getModifiedDate is java.sql.Timestamp and the data type of the column is DATE

代码:

Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("EST"));
 cal.setTimeInMillis(System.currentTimeMillis());
 cal.getTimeInMillis() - emp.getModifiedDate().getTime();

我试图使用 SimpleDateFormat 来做到这一点,但是我不确定如何继续这种方法.

I was trying to do it using SimpleDateFormat , But I am not sure how to proceed with that approach.

如果您可以提供有用的代码段

If you can provide the code snippet that will be helpful

推荐答案

您可以尝试java.util.TimeZone

You can try java.util.TimeZone

    long now = System.currentTimeMillis();
    long diff = TimeZone.getTimeZone("IST").getOffset(now) - TimeZone.getTimeZone("EST").getOffset(now); 

getOffset-返回该时区在指定日期与UTC的偏移量

getOffset - Returns the offset of this time zone from UTC at the specified date

这篇关于两个不同时区的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆