I am working on a small exercise to test myself. I made a small program to print the date/days in the current month. I assumed that you would be given the current date e.g. May 10th and the current day Thursday. My question, is it possible to do this exercise without being given the date today and the day? Here's my code:
public static void main(String [] args) {
int days = 31; // may
String arr[] = new String[]{"Mon", "Tue", "Wed", "Thur", "Fri","Sat", "Sun"};
int today_date = 10;
String today_day = "Thur";
int today_index = 0;
//get index of current day e.g. Thur = index 3
for(int i = 0; i<arr.length; i++) {
if(today_day.equals(arr[i])) {
today_index = i;
}
}
//count from today_day to 1, the purpose is to get the first day of the month
while(today_date > 1) {
if(today_index == 0) {
today_index = 6;
today_date--;
}
else {
today_date--;
today_index--;
}
}
//print each day of the month
for(int i = 1; i<=days; i++ ) {
if(today_index == 6) {
System.out.print(i+"("+ arr[today_index]+") \n");
today_index = 0;
}
else {
System.out.print(i+"("+ arr[today_index]+") | ");
today_index++;
}
}
}
Sample output: