使用java更快地将时区转换为日期

发布于 2024-11-25 06:35:39 字数 995 浏览 0 评论 0原文

我编写了这个方法来转换日期时区。我如何进一步减少该方法的执行时间。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山人契 2024-12-02 06:35:39

对于 joda-time ,它可能看起来像这样:

DateTime dt = new DateTime(DateTimeZone.forID("GMT"));
System.out.println(dt); // 5 am
dt = dt.withZone(DateTimeZone.forID("EET"));
System.out.println(dt); // 8 am

请注意,Timestamp 没有时区的概念,因此不适合表示它。

您的解决方案将具有良好的运行时间,因为它的复杂度为 O(1)。不过阅读起来比较困难。

With joda-time it may look like this:

DateTime dt = new DateTime(DateTimeZone.forID("GMT"));
System.out.println(dt); // 5 am
dt = dt.withZone(DateTimeZone.forID("EET"));
System.out.println(dt); // 8 am

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.

笑叹一世浮沉 2024-12-02 06:35:39

首先,您可以将日期存储在 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.

扭转时空 2024-12-02 06:35:39

这不是一个完整的答案,但我建议始终将时间戳存储在数据库中作为 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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文