如何从当前时间获得下个星期日的剩余时间?

问题描述:

我尝试了很多不同的方法来找到确切的解决方案,但我只能得到时间差,如果我知道未来的日期,但我希望从当前时间的下个星期日时间。如何从当前时间获得下个星期日的剩余时间?

+0

你试过了什么?你的搜索和研究带来了什么?这里已经有很多类似的问题和答案,如果您指定了他们提供的内容以及您仍然缺少的内容,我们可以更好地帮助您。 –

你可以试试这个,

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://*.com/a/45220666/5772882)中的清晰度。 –

的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");