JodaTime:如何在不同的时区找到未来的时间

Jul*_*ien 5 java time timezone jodatime

我需要找到时间点,接下来是早上7点在奥克兰(新西兰)

我正在使用joda-time 2.6

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.6</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

使用以下测试时

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)));
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我在奥克兰的午夜之后运行上述内容,那很好,就是这样

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
Run Code Online (Sandbox Code Playgroud)

但是,如果我在奥克兰的午夜之前运行上述内容,我过去的早上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
Run Code Online (Sandbox Code Playgroud)

有没有办法告诉joda-时间改变时间?

vsm*_*kov 3

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

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);
}
Run Code Online (Sandbox Code Playgroud)

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