fork download
  1. import static java.time.DayOfWeek.SATURDAY;
  2. import static java.time.DayOfWeek.SUNDAY;
  3.  
  4. import java.time.DayOfWeek;
  5. import java.time.LocalDate;
  6. import java.time.LocalTime;
  7. import java.time.ZoneId;
  8. import java.time.ZonedDateTime;
  9. import java.time.format.TextStyle;
  10. import java.time.temporal.ChronoUnit;
  11. import java.time.temporal.TemporalAdjusters;
  12. import java.util.Locale;
  13.  
  14. public class Main {
  15. public static void main(String[] args) {
  16. // Tests
  17. process(ZonedDateTime.now(ZoneId.systemDefault()));
  18. process(ZonedDateTime.of(LocalDate.of(2021, 6, 23), LocalTime.of(10, 20, 30, 123456789),
  19. ZoneId.systemDefault()));
  20. process(ZonedDateTime.of(LocalDate.of(2021, 6, 26), LocalTime.of(10, 20, 30, 123456789),
  21. ZoneId.systemDefault()));
  22. }
  23.  
  24. static long millisBetween(ZonedDateTime start, ZonedDateTime end) {
  25. return ChronoUnit.MILLIS.between(start, end);
  26. }
  27.  
  28. static boolean isWeekend(ZonedDateTime now) {
  29. DayOfWeek dow = now.getDayOfWeek();
  30. return dow == SATURDAY || dow == SUNDAY;
  31. }
  32.  
  33. static void process(ZonedDateTime now) {
  34. String dow = now.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
  35. if (isWeekend(now)) {
  36. System.out.printf("It's %s (a weekend) now. Monday is %d milliseconds ahead.%n", dow,
  37. millisBetween(now, now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).toLocalDate()
  38. .atStartOfDay(ZoneId.systemDefault())));
  39. } else {
  40. System.out.printf("It's %s (a weekday) now. Saturday is %d milliseconds ahead.%n", dow,
  41. millisBetween(now, now.with(TemporalAdjusters.next(DayOfWeek.SATURDAY)).toLocalDate()
  42. .atStartOfDay(ZoneId.systemDefault())));
  43. }
  44. }
  45. }
Success #stdin #stdout 0.12s 52688KB
stdin
Standard input is empty
stdout
It's Sunday (a weekend) now. Monday is 33799715 milliseconds ahead.
It's Wednesday (a weekday) now. Saturday is 221969876 milliseconds ahead.
It's Saturday (a weekend) now. Monday is 135569876 milliseconds ahead.