【发布时间】: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