2016-09-01 13 views
5

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?

Trả lời

2

Tôi nghĩ rằng giải pháp rõ ràng nhất có thể đúng một

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); 
} 

Bạn chỉ có thể kiểm tra xem đã có hơn 7:00 là tại Auckland và nếu như vậy chỉ tăng số ngày.

1
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; 
} 
+0

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

0
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())); 

Prints:

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

7 PM in NZ sẽ 0:30 trong IST, mà dường như đúng theo google

*7:00 AM Friday, in Auckland, New Zealand is 
12:30 AM Friday, Indian Standard Time (IST)* 
Các vấn đề liên quan