# Cycling through days of the week

**URL:** <https://discourse.processing.org/t/cycling-through-days-of-the-week/29609>\
**Category:** Coding Questions\
**Created:** [April 24, 2021, 6:04pm UTC](https://discourse.processing.org/t/cycling-through-days-of-the-week/29609 "2021-04-24T18:04:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [April 24, 2021, 6:04pm UTC](https://discourse.processing.org/t/cycling-through-days-of-the-week/29609/1 "2021-04-24T18:04:07Z")

</div>

Hi.  
I’m trying to write a code that cycles through the days of the week. Specifically, if today is Saturday, my code has to print

_today is Saturday_  
_tomorrow is Sunday_  
_the day after tomorrow is Monday_

and this has to work for every day of the week as detected by the computer’s clock (e.g. if Friday is detected, the code should print _… is Friday, …is Saturday and …is Sunday_). Here’s my solution:

```auto
import java.text.SimpleDateFormat;
import java.util.*;

int day = int(new SimpleDateFormat("u").format(new Date()));

String today = "";
String tomorrow = "";
String dayAfterTomorrow = "";

int week[] = {1, 2, 3, 4, 5, 6, 7};
String daysOfWeek[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

for (int i = 0; i < week.length; i++) {
  if ((day == week[i]) && (i < 5)) {
    today = "today is " + daysOfWeek[i];
    tomorrow = "tomorrow is " + daysOfWeek[i+1];
    dayAfterTomorrow = "the day after tomorrow is " + daysOfWeek[i+2];
  } else if ((giorno == settimana[i]) && (i == 5)) {
    tomorrow = "today is " + daysOfWeek[i];
    domani = "tomorrow is " + daysOfWeek[i+1];
    dayAfterTomorrow = "the day after tomorrow is " + daysOfWeek[0];
  } else if ((giorno == settimana[i]) && (i == 6)) {
    today= "today is " + daysOfWeek[i];
    tomorrow = "tomorrow is " + daysOfWeek[0];
    dayAfterTomorrow = "the day after tomorrow is " + daysOfWeek[1];
  }
}

println(today);
println(tomorrow);
println(dayAfterTomorrow);

```

I’m pretty sure that there are too many lines of code and a more elegant solution could be found. In particularly I remember of having seen a solution to cycle through the array using modulo… but I’m not able to figure out how it should work.  
Can you please help me?

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 24, 2021, 7:12pm UTC](https://discourse.processing.org/t/cycling-through-days-of-the-week/29609/2 "2021-04-24T19:12:41Z")

</div>

you need modulus 🙂

```auto
import java.text.SimpleDateFormat;
import java.util.*;

int day = int(new SimpleDateFormat("u").format(new Date()));

day -= 1;

String today = "";
String tomorrow = "";
String dayAfterTomorrow = "";

String daysOfWeek[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

today = "today is " + daysOfWeek[day];
tomorrow = "tomorrow is " + daysOfWeek[(day+1)%7];
dayAfterTomorrow = "the day after tomorrow is " + daysOfWeek[(day+2)%7];

println(today);
println(tomorrow);
println(dayAfterTomorrow);

```

---

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [April 24, 2021, 9:29pm UTC](https://discourse.processing.org/t/cycling-through-days-of-the-week/29609/3 "2021-04-24T21:29:58Z")

</div>

I will definitely! 🤣
