使用java更快地将时区转换为日期
我编写了这个方法来转换日期时区。我如何进一步减少该方法的执行时间。
public static Timestamp convertTimeZone(final Timestamp fromDate, final TimeZone fromTZ, final TimeZone toTZ ){
Long timeInDate = fromDate.getTime() ;
int fromOffset = fromTZ.getOffset(timeInDate);
int toOffset = toTZ.getOffset(timeInDate);
Timestamp dateStamp = new Timestamp(fromDate.getTime());
if (fromOffset >= 0){
int diff = 0;
if (toOffset > 0){
diff = (fromOffset - toOffset);
} else {
diff = (fromOffset + Math.abs(toOffset));
}
long date = fromDate.getTime() - diff;
dateStamp.setTime(date);
} else {
int diff = 0;
if (toOffset > 0){
diff = (Math.abs( fromOffset) + toOffset);
} else {
diff = (Math.abs( fromOffset) - Math.abs(toOffset));
}
long date = fromDate.getTime() + diff;
dateStamp.setTime(date);
}
return dateStamp;
}
I have written this method for converting a date time zone. How do i reduce the execution time of this method further.
public static Timestamp convertTimeZone(final Timestamp fromDate, final TimeZone fromTZ, final TimeZone toTZ ){
Long timeInDate = fromDate.getTime() ;
int fromOffset = fromTZ.getOffset(timeInDate);
int toOffset = toTZ.getOffset(timeInDate);
Timestamp dateStamp = new Timestamp(fromDate.getTime());
if (fromOffset >= 0){
int diff = 0;
if (toOffset > 0){
diff = (fromOffset - toOffset);
} else {
diff = (fromOffset + Math.abs(toOffset));
}
long date = fromDate.getTime() - diff;
dateStamp.setTime(date);
} else {
int diff = 0;
if (toOffset > 0){
diff = (Math.abs( fromOffset) + toOffset);
} else {
diff = (Math.abs( fromOffset) - Math.abs(toOffset));
}
long date = fromDate.getTime() + diff;
dateStamp.setTime(date);
}
return dateStamp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 joda-time ,它可能看起来像这样:
请注意,
Timestamp
没有时区的概念,因此不适合表示它。您的解决方案将具有良好的运行时间,因为它的复杂度为 O(1)。不过阅读起来比较困难。
With joda-time it may look like this:
Note that
Timestamp
has no notion of Timezone so it is not suitable for representing it.Your solution will have a good running time, since it's O(1). It's harder to read though.
首先,您可以将日期存储在 Calendar 对象中。以不同的时区格式显示它是您应用于 SimpleDateFormat 的配置问题。
从技术上讲,日期在所有时区都是相同的(它的内部值是相同的)。应用时区的概念允许日期格式化程序调整显示的偏移量。换句话说,表示伦敦时间 17:00 的日期等于表示纽约时间 12:00 的日期。在 GMT 与 EST 时区中显示它可以是日期格式化程序的函数。
You could store your date in a Calendar object to begin with. Showing it in different time zone formats will be a matter of configuration that you apply to a SimpleDateFormat.
Technically a Date is the same in all time zones (it's internal value is the same). Applying the concept of timezones allows date formatters to adjust offsets for display. In other words, a Date that represents 17:00 in London time is equal to a Date that represents 12:00 in New York. Displaying it in the GMT vs. the EST time zones can be a function of date formatters.
这不是一个完整的答案,但我建议始终将时间戳存储在数据库中作为 UTC 时间而不是本地时间。并仅在表示层设置
DateFormat.setTimeZone()
上显示不同时区It's not quite an answer, but I recommend always store timestamp in database as UTC time instead of local time. And display it for different timezones only on presentation layer setting
DateFormat.setTimeZone()