I am trying to fix my Calendar (Month-March):
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 0 0 0 0
32 0 0 0 0 0 0
1) The first problem is that I can't stop counting after 31, as you can see I have 0 0 0 0 0
but in this place I would like to have empty space.
2) Second problem is that March is starting from Thursday, not from Monday, how to start counting days from Thursday?. This is my code:
public class MyPoorCalendar{
public static void main(String[] args) {
int firstday = 1;
int mycalendar[][] = new int[6][7];
String nameOfTheWeeks = "Mon\t" + "Tue\t" + "Wed\t" + "Thu\t" + "Fri\t" + "Sat\t" + "Sun\t";
System.out.println(nameOfTheWeeks);
//initializing
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
mycalendar[i][j] = firstday++;
if (firstday > 31) {
break;
}
}
}
//printing
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(mycalendar[i][j] + "\t");
if (j == 6) {
System.out.println();
}
}
}
}
}
Just print a blank space when mycalendar[i][j] == 0
System.out.print((mycalendar[i][j] == 0 ? " " : mycalendar[i][j]) + "\t");
When setting the values you should break out of the outer most for loop, currently you only break out of the inner most loop so do this instead
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
mycalendar[i][j] = firstday++;
}
if (firstday > 31) {
break;
}
}
Lastly if you want to start at a different location in the calendar then you could place a counter that, when it equals 0 you start your days. For example if you wanted to start on Thursday you could do something like this
int dayToStartOn = 4; //Thursday
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
mycalendar[i][j] = dayToStartOn-- > 0 ? 0 : firstday++;
}
if (firstday > 31) {
break;
}
}
You can do this (explanation in code):
// store the length of the month and the first day of month's weekday number
int lengthOfMonth = LocalDate.now().lengthOfMonth();
int firstDayOfMonthsWeekDay = LocalDate.now().withDayOfMonth(1).getDayOfWeek().getValue();
// use a labeled break statement to terminate the outer for loop when we reach the end of the month
month:
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
if(--firstDayOfMonthsWeekDay > 0){ // fixing first day of month's weekday to start counting from tuesday for example
continue;
}
mycalendar[i][j] = firstday++;
if (firstday > lengthOfMonth) {
break month; // the execution will continue after the outer loop
}
}
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
System.out.print((mycalendar[i][j] == 0 ? " " : mycalendar[i][j]) + "\t"); // replace 0's in the array with spaces
if (j == 6) {
System.out.println();
}
}
}
Output:
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
More information about labeled break here . And about the java.time api here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.