2017-07-20 106 views
1

我嘗試了很多不同的方法來找到確切的解決方案,但我只能得到時間差,如果我知道未來的日期,但我希望從當前時間的下個星期日時間。如何從當前時間獲得下個星期日的剩餘時間?

+0

你試過了什麼?你的搜索和研究帶來了什麼?這裏已經有很多類似的問題和答案,如果您指定了他們提供的內容以及您仍然缺少的內容,我們可以更好地幫助您。 –

回答

2

你可以試試這個,

Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 

    int saturdayInMonth = calendar.get(Calendar.DAY_OF_MONTH) + (Calendar.SATURDAY - calendar.get(Calendar.DAY_OF_WEEK)); 

    calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 
      saturdayInMonth, 23, 59, 59); // This time will be sunday night 23 hour 59 min and 59 seconds 

    Date sunday = new Date(calendar.getTimeInMillis() + 1000); //this is 1 second after that seconds that is sunday 00:00. 
+1

這有點棘手,但我相信它有效。我更喜歡[ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP)與[Hugo的回答](https://stackoverflow.com/a/45220666/5772882)中的清晰度。 –

2

的Android,您可以用ThreeTenABP使用ThreeTen Backport,對Java 8的新的日期/時間類大反向移植,一起(更多關於如何使用它here )。所有課程都在org.threeten.bp包中。

要獲得2個日期之間的差異,您可以使用org.threeten.bp.ZonedDateTime,因爲此類負責日光節約時間更改(以及可能發生的任何其他偏移更改)並提供正確/準確的結果(如果您計算它不使用時區,在計算中不考慮DST變化)。

我也使用org.threeten.bp.temporal.TemporalAdjusters類,它有一個內置的方法來查找下一個指定的星期幾(通過使用org.threeten.bp.DayOfWeek類中的常量)。

爲了獲得不同,您可以使用org.threeten.bp.temporal.ChronoUnit.MILLIS來獲取以毫秒爲單位的差異(然後使用此值以您想要的任何格式顯示)。或者您可以使用org.threeten.bp.temporal.ChronoUnit類中的其他常量(例如MINUTESHOURS,它們會以分鐘或小時爲單位給出差異 -​​查看所有可用單元)。

另一種獲得差異的方法是使用org.threeten.bp.Duration,它將包含兩個日期之間的秒數和納秒數。

// change this to the timezone you need 
ZoneId zone = ZoneId.of("Asia/Kolkata"); 
// get current date in the specified timezone 
ZonedDateTime now = ZonedDateTime.now(zone); 
// find next Sunday 
ZonedDateTime nextSunday = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)); 

// get the difference in milliseconds 
long diffMillis = ChronoUnit.MILLIS.between(now, nextSunday); 

// get the difference as a Duration 
Duration duration = Duration.between(now, nextSunday); 

請注意,我使用了時區Asia/Kolkata。該API使用IANA timezones names(始終格式爲Region/City,如America/Sao_PauloEurope/Berlin)。 避免使用3字母縮寫(如IST或),因爲它們是ambiguous and not standard

通過調用ZoneId.getAvailableZoneIds(),您可以獲得可用時區列表(並選擇最適合您系統的時區)。


上面的代碼將得到nextSunday用相同的時間(小時/分/秒/納秒)爲now - 除非有一個DST變化(在這種情況下,the time is adjusted accordingly)。

但是,如果你想從現在得到的剩餘時間,直到開始下週日的,那麼你必須計算差值之前,將其調整到一天的開始:

// adjust it to the start of the day 
nextSunday = nextSunday.toLocalDate().atStartOfDay(zone); 

注即一天的開始時間並非總是午夜 - 由於DST更改,例如,一天可能在凌晨1:00開始(例如,時鐘可能設置爲午夜前的1小時,因此一天中的第一個小時爲凌晨1點)。使用atStartOfDay(zone)保證您不必擔心,因爲API爲您處理它。


如果當前日期已經是星期天,那麼結果是什麼?

即使當前日期是星期日,上面的代碼也會得到下一個星期日。如果你不想要那個,你可以使用TemporalAdjusters.nextOrSame,如果它已經是星期天,它會返回相同的日期。


要顯示的時間單位Duration值(如小時,分鐘和秒),你可以做到以下幾點:

StringBuilder sb = new StringBuilder(); 
long seconds = duration.getSeconds(); 
long hours = seconds/3600; 
append(sb, hours, "hour"); 
seconds -= (hours * 3600); 
long minutes = seconds/60; 
append(sb, minutes, "minute"); 
seconds -= (minutes * 60); 
append(sb, seconds, "second"); 
append(sb, duration.getNano(), "nanosecond"); 

System.out.println(sb.toString()); 

// auxiliary method 
public void append(StringBuilder sb, long value, String text) { 
    if (value > 0) { 
     if (sb.length() > 0) { 
      sb.append(" "); 
     } 
     sb.append(value).append(" "); 
     sb.append(text); 
     if (value > 1) { 
      sb.append("s"); // append "s" for plural 
     } 
    } 
} 

結果(以我目前的時間)爲:

47小時44分鐘43秒1.48納秒

我如果你想要毫秒而不是納秒,你可以用append(sb, duration.getNano(), "nanosecond")替換爲:

// get milliseconds from getNano() value 
append(sb, duration.getNano()/1000000, "millisecond"); 
相關問題