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.
Asked
Active
Viewed 224 times
1

Youcef LAIDANI
- 55,661
- 15
- 90
- 140

oopsie
- 29
- 2
-
Please search Stack Overflow thoroughly before posting. Your issues have been handled many times already. – Basil Bourque Jul 05 '18 at 22:25
1 Answers
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
- 154,647
- 11
- 152
- 247

Youcef LAIDANI
- 55,661
- 15
- 90
- 140
-
-
Thank you @Andreas what about this solution https://ideone.com/jeZGaa, is that what you mean? – Youcef LAIDANI Jul 05 '18 at 16:16
-
1More 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
-
-
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* ;) – Youcef LAIDANI Jul 05 '18 at 16:44
-
1For 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
-
-
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 ;) – Youcef LAIDANI Jul 05 '18 at 17:10