tôi cần phải tìm ra thời điểm nào, khi nào nó sẽ tiếp theo được 7:00 vào buổi sáng ở Auckland (New Zealand)JodaTime: làm thế nào để tìm một thời gian tương lai trong một múi giờ khác nhau
Tôi sử dụng Joda thời gian 2,6
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.6</version>
</dependency>
Khi thử nghiệm với những điều sau
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)));
}
}
Nếu tôi chạy trên sau Midnight in Auckland, nó là tốt, nó là
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
Nhưng, nếu tôi chạy trên trước Midnight in Auckland, tôi nhận được 07:00 trong quá khứ ...
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
Có cách nào để nói với joda-thời gian để đi tiếp khi thay đổi thời gian?
Bạn nên đổi tên tham số đầu tiên cho một sử dụng mục đích chung hơn, vì phương pháp này thực sự không quan tâm liệu giá trị có phải là "bây giờ" hay một thời gian nào khác không. Ngoài ra, nếu phần * tiếp theo * của tên phương thức nên được thực hiện theo nghĩa đen, thì điều kiện phải là '! isAfter() ', vì dòng mã của bạn trả về giá trị đầu vào nếu nó xảy ra * chính xác * là' giờ' đã cho. – Andreas