I want to understand this code. I know this code gives me the calendar when I enter the year, month and day. Then program shows the calendar of that month, additionally the code shows me the time and date now. But I want to understand how does it work?
import java.util.*;
import java.text.SimpleDateFormat;
public class Calendar3
{
private static void doSimpleDateFormat() {
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.print(" \n It is now : " + formatter.format(now.getTime()));
System.out.println();
}
public static void CalendarDemo(int Year, int Month, int Date) {
GregorianCalendar a = new GregorianCalendar();
a.set(Calendar.YEAR, Year);
a.set(Calendar.MONTH, (Month - 1));
a.set(Calendar.DAY_OF_MONTH, 1);
int weekday = a.get(Calendar.DAY_OF_WEEK);
int month = a.get(Calendar.MONTH);
System.out.println("'\n' YEAR : " + Year);
switch (month) {
case 0:
System.out.println();
System.out.println(" JANUARY ");
System.out.println();
break;
case 1:
System.out.println();
System.out.println(" FEBRUARY ");
System.out.println();
break;
case 2:
System.out.println();
System.out.println(" MARCH ");
System.out.println();
break;
case 3:
System.out.println();
System.out.println(" APRIL ");
System.out.println();
break;
case 4:
System.out.println();
System.out.println(" MAY ");
System.out.println();
break;
case 5:
System.out.println();
System.out.println(" JUNE ");
System.out.println();
break;
case 6:
System.out.println();
System.out.println(" JULY ");
System.out.println();
break;
case 7:
System.out.println();
System.out.println(" AUGUST ");
System.out.println();
break;
case 8:
System.out.println();
System.out.println(" SEPTEMBER ");
System.out.println();
break;
case 9:
System.out.println();
System.out.println(" OCTOBER ");
System.out.println();
break;
case 10:
System.out.println();
System.out.println(" NOVEMBER ");
System.out.println();
break;
case 11:
System.out.println();
System.out.println(" DECEMBER ");
System.out.println();
break;
}
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for (int i = Calendar.SUNDAY; i < weekday; i++) {
System.out.print(" ");
}
do {
int day = a.get(Calendar.DAY_OF_MONTH);
if (day < 10) {
System.out.print(" " + day);
} else if (day >= 10) {
System.out.print(day);
}
if (day == Date) {
System.out.print("* ");
} else {
System.out.print(" ");
}
if (weekday == Calendar.SATURDAY) {
System.out.println();
}
a.add(Calendar.DAY_OF_MONTH, 1);
weekday = a.get(Calendar.DAY_OF_WEEK);
}
while (a.get(Calendar.MONTH) == month);
if (weekday != Calendar.SUNDAY) {
System.out.println();
}
System.out.println();
System.out.println("Note: '*' over any number is your desired number");
System.out.println();
doSimpleDateFormat();
}
}
It is very lengthy and not particularly well written. (eg The entire switch block could be replaced with one line of code)
If you want to understand what it does and how it does it I suggest you step through the program in a debugger in your IDE. This will show you line by line what all the variables are set to.
EDIT: Shorter is not always better but since you were wondering how short you can make it.
import static java.util.Calendar.*;
private static final SimpleDateFormat YEAR_MONTH = new SimpleDateFormat("'\n YEAR: ' yyyy'\n 'MMMM'\n'");
public static void CalendarDemo(int year, int month, int date) {
GregorianCalendar a = new GregorianCalendar(year, month-1, 1);
System.out.printf("%n%s%nSun Mon Tue Wed Thu Fri Sat%n%"+4*(SUNDAY-a.get(DAY_OF_WEEK))+"s", YEAR_MONTH.format(a.getTime()).toUpperCase() , "");
for(;a.get(MONTH) == month-1;a.add(DAY_OF_MONTH, 1)) {
int day = a.get(DAY_OF_MONTH);
System.out.printf("%2d%s%s", day, day == date ? "*" : " ", a.get(DAY_OF_WEEK) == SATURDAY ? "\n" : " ");
}
System.out.println("\n\nNote: '*' over any number is your desired number\n");
doSimpleDateFormat();
}
CalendarDemo(2011,01,07);
Prints
YEAR: 2011 JANUARY Sun Mon Tue Wed Thu Fri Sat 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
Go to www.jetbrains.org, download intellij community edition
Create a project and put your code in it.
Add a main method
public static void main(String[] args) { Calendar3.CalendarDemo(2011, 1, 8); }
Put a break point on the line: GregorianCalendar a = new GregorianCalendar(); by clicking on the left hand side margin.
Debug.
BTW: There is a formatting issue, this change will help:
for (int i = Calendar.SUNDAY; i < weekday; i++) {
System.out.print(" ");
}
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.