简体   繁体   English

Java本地时区问题

[英]Local timezone issues with java

I have a scenario where i am missing some hours data based on the local timezone. 我有一种情况,我会丢失一些基于本地时区的小时数据。 Datastore is having records in UTC. 数据存储区在UTC中有记录。 Lets assume the the local timezone is "Asia/Kolkata". 假设本地时区为“亚洲/加尔各答”。 User have some options to choose between dates and report will be generated for those dates in the UI. 用户可以在日期之间选择一些选项,然后将在UI中为这些日期生成报告。 For each day user will see a count in that day starting from first hour to last hour. 用户每天都会看到从第一个小时到最后一个小时的那一天的计数。 Lets user have entered Jan 22nd to feb 12th on the UI, then, then the query will be fire between these date inclusive of both dates starting from 00:00 hours to ending 23:00 Hours with given a result set in UTC. 让用户在用户界面上输入1月22日至2月12日,然后在这些日期之间触发查询,包括从00:00小时到23:00小时的两个日期,并以UTC设置结果。 I have a code snippet where this result set will be converted to local timezone as below mentioned. 我有一个代码片段,其中此结果集将转换为本地时区,如下所述。

java.util.Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Kolkata")); 
java.sql.Timestamp ts = resultSet.getTimestamp(PUBLISH_TIME, cal);

Becuase of this, the end resultset miss some hours data due to local timezone ie on Jan 22nd(The other days count is correct except this starting day) i will get the data from 05:30 hours not from 00:00 Hours of the day. 因此,由于本地时区的原因,最终结果集错过了一些小时数据,即1月22日(除此开始日期外,其他几天的计数是正确的)我将从05:30小时(而不是当天00:00)获得数据。 If i have the UTC set in the resultset calendar instance then i am getting all hours data without any miss. 如果我在结果集日历实例中设置了UTC,那么我将获得所有小时数据,而不会错过任何时间。

Is this the correct behavior? 这是正确的行为吗?

I also tried querying the DB with local timezone with the help of below snippet but it also gives the wrong counts. 我还尝试在以下代码段的帮助下使用本地时区查询数据库,但它也提供了错误的计数。 I am not sure whether i need to do this at the query time but you can see the below snippet? 我不确定是否需要在查询时执行此操作,但是您可以看到以下代码段吗?

 SimpleDateFormat localTimeFormatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss.SSS");
localTimeFormatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
SimpleDateFormat utcFormatter = new SimpleDateFormat(DateUtil.DATE_PATTERN);
utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date startDateUTC;
String startDateUTCString;
Date endDateUTC;
String endDateUTCString;
try {
    startDateUTC = localTimeFormatter.parse(reportCriteria.getStartDate());
    startDateUTCString = utcFormatter.format(startDateUTC);


    endDateUTC = localTimeFormatter.parse(reportCriteria.getEndDate());
    endDateUTCString = utcFormatter.format(endDateUTC);

} catch (ParseException e) {
    e.printStackTrace();
    log.error("Error while formating Date :" + e);    

How can get the missed hours data in local timezone? 如何获取本地时区的漏工时间数据? I am not using joda time and i do not want to use that now with using java7. 我没有使用joda时间,现在也不想在使用java7时使用它。

Lets user have entered Jan 22nd to feb 12th on the UI, then, then the query will be fire between these date inclusive of both dates starting from 00:00 hours to ending 23:00 Hours with given a result set in UTC. 让用户在用户界面上输入1月22日至2月12日,然后在这些日期之间触发查询,包括从00:00小时到23:00小时的两个日期,并以UTC设置结果。

To me this sounds like you are using a range of local timestamps, provided by a user, to query a table containing UTC timestamps, ignoring the different timezones. 对我来说,这听起来像是您在使用由用户提供的一系列本地时间戳来查询包含UTC时间戳的表,而忽略了不同的时区。 You need to convert local timestamps to UTC first and use the converted value to query the database. 您需要先将本地时间戳转换为UTC,然后使用转换后的值查询数据库。

If you don't things go wrong. 如果不这样做,那就会出错。

Eg say the table contains a record with the timestamp Jan 22nd 23:00 (UTC) . 例如,说该表包含带有时间戳Jan 22nd 23:00 (UTC) When you query the table using the local timestamp Jan 23nd 00:00 (UTC+1) , you are not going to get that record out even though they're exactly the same. 当您使用本地时间戳Jan 23nd 00:00 (UTC+1)查询表时,即使它们完全相同,也不会获取该记录。

In your case, the Asia/Kolkata timezone is equal to UTC+5:30. 在您的情况下,亚洲/加尔各答时区等于UTC + 5:30。 So if the database is queried for the UTC timestamp Jan 1st 00:00 and afterwards the result is converted to a local datetime, the result Jan 1st 00:00 becomes Jan 1st 05:30. 因此,如果查询数据库的UTC时间戳1月1日00:00,然后将结果转换为本地日期时间,则结果1月1日00:00变为1月1日05:30。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM