1

I want to return the time in millisecond of the time until 20:00 on the next upcoming Wednesday or 20:00 on the next upcoming Saturday, depending on which is closest. I just don't know how to get the time in millisecond of each of these times.

YCF_L
  • 51,266
  • 13
  • 85
  • 129
oopsie
  • 29
  • 2

1 Answers1

3

You can use :

LocalDateTime now = LocalDateTime.now();                  //Get current Date time
LocalDateTime nextSaturday = now                          //Get date time
        .with(TemporalAdjusters.next(DayOfWeek.SATURDAY)) //of next SATURDAY
        .with(LocalTime.of(20, 0));                       //at 20:00   

//Now you can use until with ChronoUnit.MILLIS to get millisecond betwenn the two dates
long millSeconds = now.until(nextSaturday, ChronoUnit.MILLIS);
//or             = ChronoUnit.MILLIS.between(now, nextSaturday);

System.out.println(now);          //2018-07-05T16:54:54.585789200
System.out.println(nextSaturday); //2018-07-07T20:00
System.out.println(millSeconds);  //183905414

With zone time you can use :

ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Paris"));//Or any zone time
ZonedDateTime nextSaturday = now
        .with(TemporalAdjusters.next(DayOfWeek.SATURDAY))
        .with(LocalTime.of(20, 0));
long millSeconds = ChronoUnit.MILLIS.between(now, nextSaturday);

System.out.println(now);           //2018-07-05T18:25:10.377511100+02:00[Europe/Paris]
System.out.println(nextSaturday);  //2018-07-07T20:00+02:00[Europe/Paris]
System.out.println(millSeconds);   //178489622

To check which is close Wednesday or Saturday you can use :

LocalDate saturday = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
LocalDate wednesday = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));

long result;
if(saturday.isBefore(wednesday)){
    result = getMillSecond(DayOfWeek.SATURDAY);
}
result = getMillSecond(DayOfWeek.WEDNESDAY);

Or as @Andreas suggest in comment you can use :

ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
DayOfWeek day = EnumSet.range(DayOfWeek.WEDNESDAY, DayOfWeek.FRIDAY)
        .contains(now.getDayOfWeek()) ? DayOfWeek.SATURDAY : DayOfWeek.WEDNESDAY;
long result = getMillSecond(day);

and you can put one of the previous code in a method and call it based on the condition above :

public static long getMillSecond(DayOfWeek day){
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Paris"));//Or any zone time
    ZonedDateTime nextSaturday = now
            .with(TemporalAdjusters.next(day))
            .with(LocalTime.of(20, 0));
    return ChronoUnit.MILLIS.between(now, nextSaturday);
}
Andreas
  • 147,606
  • 10
  • 133
  • 220
YCF_L
  • 51,266
  • 13
  • 85
  • 129
  • Doesn't account for Daylight Savings Time. Still +1 – Andreas Jul 05 '18 at 16:01
  • Thank you @Andreas what about this solution https://ideone.com/jeZGaa, is that what you mean? – YCF_L Jul 05 '18 at 16:16
  • 1
    More like this: https://ideone.com/1YwYHi --- It sets time to 8 PM, not 8:00:15.685 PM, and it ensures that start and end times are in the same time zone, rather than changing the time zone of only the end time. – Andreas Jul 05 '18 at 16:23
  • Thank you @Andreas I appreciate your help – YCF_L Jul 05 '18 at 16:27
  • 1
    "If you care about zone time" -> You should ALWAYS care about that before it bites you in the ass. – Sebastiaan van den Broek Jul 05 '18 at 16:42
  • I agree @SebastiaanvandenBroek I removed that part *If you care* ;) – YCF_L Jul 05 '18 at 16:44
  • 1
    For choosing between Wednesday and Saturday, you can also do: `DayOfWeek day = (EnumSet.range(DayOfWeek.WEDNESDAY, DayOfWeek.FRIDAY).contains(now.getDayOfWeek()) ? DayOfWeek.SATURDAY : DayOfWeek.WEDNESDAY);` – Andreas Jul 05 '18 at 16:46
  • Intelligent solution @Andreas thank you so much – YCF_L Jul 05 '18 at 16:51
  • I just did if millisUntilWednesday < millisUntilSaturday to check which is closer. – oopsie Jul 05 '18 at 17:06
  • @oopsie correct that also solve your problem `if(getMillSecond(DayOfWeek.WEDNESDAY) < getMillSecond(DayOfWeek.SATURDAY)){` I find it smart idea also ;) – YCF_L Jul 05 '18 at 17:10