【问题标题】:JodaTime: how to find a future time in a different timezoneJodaTime:如何在不同时区找到未来时间
【发布时间】:2017-01-09 10:27:26
【问题描述】:

我需要找到时间点,下一个是奥克兰(新西兰)早上 7:00

我正在使用 joda-time 2.6

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.6</version>
    </dependency>

当使用以下测试时

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class FindDateTimeInFuture {
    static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS z Z");

    public static void main(String[] args) {
        // Use UTC as application wide default
        DateTimeZone.setDefault(DateTimeZone.UTC);

        System.out.println("now UTC         = " + formatter.print(DateTime.now()));

        System.out.println("now in Auckland = " + formatter.print(DateTime.now(DateTimeZone.forID("Pacific/Auckland"))));

        System.out.println("7 AM Auckland   = " + formatter.print(DateTime.now(DateTimeZone.forID("Pacific/Auckland")).withTime(7, 0, 0, 0)));
    }
}

如果我在奥克兰的午夜之后运行上面的,没关系,它是

now UTC         = 2016-09-01 13:37:26.844 UTC +0000
now in Auckland = 2016-09-02 01:37:26.910 NZST +1200
7 AM Auckland   = 2016-09-02 07:00:00.000 NZST +1200
                           ^ ok, in the future

但是,如果我在奥克兰的午夜之前运行上述代码,我会得到过去的早上 7 点...

now UTC         = 2016-09-01 09:37:48.737 UTC +0000
now in Auckland = 2016-09-01 21:37:48.831 NZST +1200
7 AM Auckland   = 2016-09-01 07:00:00.000 NZST +1200
                           ^ ko, in the past

有没有办法在更改时间时告诉 joda-time 前进?

【问题讨论】:

    标签: java time timezone jodatime


    【解决方案1】:

    我认为最明显的解决方案可能是正确的一个

    DateTime nowAuckland = 
        DateTime.now(DateTimeZone.forID("Pacific/Auckland"));
    boolean addDay = nowAuckland.getHourOfDay() >= 7;
    DateTime aucklandAt700 = nowAuckland.withTime(7, 0, 0, 0);
    if (addDay) {
        aucklandAt700 = aucklandAt700.plusDays(1);
    }
    

    您可以检查奥克兰是否已经有超过7:00,如果有,只需增加天数。

    【讨论】:

      【解决方案2】:
      private DateTime getNextDateTime(DateTime now, int hour)
      {
          DateTime nextDateTime = now.withTime(hour, 0, 0, 0);
          if(nextDateTime.isBefore(now))
          {
              nextDateTime = nextDateTime.plusDays(1);
          }
          return nextDateTime;
      }
      

      【讨论】:

      • 您应该重命名第一个参数以用于更通用的用途,因为该方法并不真正关心该值是“现在”还是其他时间。此外,如果方法名称的 next 部分应该按字面意思理解,那么条件应该是 ! isAfter(),因为如果恰好是 exactly i> 给定的hour
      【解决方案3】:
      DateTime nowAuckland = DateTime.now(DateTimeZone.forID("Pacific/Auckland"));
      DateTime currentTimeNow = DateTime.now(DateTimeZone.getDefault());
      DateTime aucklandAt700 = nowAuckland.withTime(7, 0, 0, 0);
      System.out.println(currentTimeNow);
      Duration duration = new Interval(nowAuckland, aucklandAt700).toDuration();
      System.out.println(currentTimeNow.plusMillis((int) duration.getMillis()));
      

      打印:

      2016-09-01T21:07:13.444+05:30
      2016-09-02T00:30:00.047+05:30
      

      新西兰的早上 7 点将是 IST 的 00:30,这在谷歌看来是正确的

      *7:00 AM Friday, in Auckland, New Zealand is
      12:30 AM Friday, Indian Standard Time (IST)*
      

      【讨论】:

        猜你喜欢
        • 2010-10-28
        • 2011-02-19
        • 2014-10-10
        • 1970-01-01
        • 2020-04-24
        • 1970-01-01
        • 2011-01-30
        • 1970-01-01
        • 2017-09-25
        相关资源
        最近更新 更多