英文:
How to translate system date from java backend service to the logged in user's time zone?
问题
以下是您要求的翻译部分:
这是我的问题:
我正在构建一个基于网络的应用程序。我在后端使用Java使用RESTful服务。这些服务托管在英国的服务器上。
我的应用程序用户大多位于欧洲地区。这两个地点之间的时差正在产生问题。假设我的应用程序用户在德国。德国比英国快1小时(假设我们处于夏季)。现在,为了从后端搜索项目并在用户界面中显示,我有一个需要在后端服务中应用的时间过滤器。
如果我的用户在德国当地时间上午9点登录我的应用程序,我如何确保后端服务也根据德国时间而不是英国时间应用时间过滤器。
我到目前为止尝试过的:
在我的存储库类(UnitRepository.java)中,我做了以下更改:
@Query(value="select asu.* from Site_Unit su where su.SCHEDULED_DEPARTURE_TIME between :startTime and :endTime", nativeQuery = true )
List<ResultDTO> getUnitsByTime(@Param("startTime") Timestamp startTimeFilter,
@Param("endTime") Timestamp endTimeFilter);
在我的DaoImpl类中,我做了以下更改:
public List<ResultDTO> getUnitsByTime() {
Calendar today12Noon = new GregorianCalendar();
today12Noon.set(Calendar.HOUR_OF_DAY, 12);
today12Noon.set(Calendar.MINUTE, 0);
today12Noon.set(Calendar.SECOND, 0);
today12Noon.set(Calendar.MILLISECOND, 0);
java.util.Date endTime = today12Noon.getTime(); // 结束时间是同一天中午12点
java.util.Date startTime = new java.util.Date(); // 开始时间是用户登录应用程序的时间
return unitRepository.getUnitsByTime(new Timestamp(startTime.getTime()), new Timestamp(endTime.getTime()));
}
然而,'startTime'获取了英国时间的系统日期,而我希望它是德国的时区时间。
任何指针将不胜感激。(还有一些代码片段我没有包括在内,因为与此不相关)
英文:
Here is my issue:
I am building a web based application. Am using RESTful services using java in the backend. These services are hosted on a server in United Kingdom.
My application users mostly fall in European locations. Time difference between the 2 locations is creating issue. Let's say my app users are in Germany. Germany is ahead of UK by 1hr. (Assuming we are in summer). Now, to search items from backend and display in UI, I have a time filter to be applied in back end service.
If my user logs in to my application at Germany at 9am local time, how can I make sure backend service also applies time filter based on Germany time and not UK time.
Here is what I tried so far:
In my repository class(UnitRepository.java) I did this:
@Query(value="select asu.* from Site_Unit su where su.SCHEDULED_DEPARTURE_TIME between :startTime and :endTime", nativeQuery = true )
List<ResultDTO> getUnitsByTime(@Param("startTime") Timestamp startTimeFilter,
@Param("endTime") Timestamp endTimeFilter);
In my DaoImpl class I did this:
public List<ResultDTO> getUnitsByTime() {
Calendar today12Noon = new GregorianCalendar();
today12Noon.set(Calendar.HOUR_OF_DAY, 12);
today12Noon.set(Calendar.MINUTE, 0);
today12Noon.set(Calendar.SECOND, 0);
today12Noon.set(Calendar.MILLISECOND, 0);
java.util.Date endTime = today12Noon.getTime(); // end time is till 12pm of the same day
java.util.Date startTime = new java.util.Date(); // start time is the time when user logs in to the application
return unitRepository.getUnitsByTime(new Timestamp(startTime.getTime()), new Timestamp(endTime.getTime()));
}
However, 'startTime' picks up system date for UK time whereas I want it to be for Germany time zone.
Any pointers would be appreciated.(There are few more pieces of code which I left out as it was not relevant here)
答案1
得分: 1
首先,我强烈建议不要使用过时的类Date
和Timestamp
,并切换到java.time
包中的类。例如,使用该包中的ZonedDateTime。通过使用该包中的类,您可以轻松地将时间从GMT转换为本地时间。这是一个通用建议。至于您的问题,有几种解决方案:
- 用户可以向您发送包含时区信息的时间参数:例如,他们将向您发送一个带有时区信息的
ZonedDateTime
实例,而不是没有时区信息的LocalDateTime
。在这种情况下,如果您在查询中使用ZonedDateTime
类作为过滤器,一切都会自动运行。 - 您可以在应用中保留用户配置文件,并在那里存储用户的时区。在这种情况下,即使用户向您发送没有时区信息的时间查询参数,您也可以根据从用户配置文件中获取的用户时区调整时间。
英文:
First of all, I VERY STRONGLY recommend not to use outdated classes Date
and Timestamp
and switch to classes in the java.time
package. For example ZonedDateTime. Using classes from that package you can easily convert from time in GMT to local time. This is a general suggestion. As for your issue, there are several solutions:
- user can send you time parameters that include time zone in it: for instance, they will send you an instance of
ZonedDateTime
that has time zone info and notLocalDateTime
which does not. In this case, if you use theZonedDateTime
class as a filter in your query everything will work automatically. - You can keep user profile in your app, and there you can store user's time zone. In this case even if user sends you time query parameter without time zone, you can adjust the time based on user's time zone taken from user's profile
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论