public class calendar
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
System.out.println("Please select from one of the following choices: ");
System.out.println("m - display calendar for month specified");
System.out.println("q - quit program");
String userChoice = input.nextLine();
do
{
while (userChoice.charAt(0) != 'q' && userChoice.charAt(0) != 'Q'
&& userChoice.charAt(0) != 'm'
&& userChoice.charAt(0) != 'M')
{
System.out.println("Invalid menu choice");
System.out
.println("Please select from one of the following choices: ");
System.out.println("m - display calendar for month specified");
System.out.println("q - quit program");
userChoice = input.nextLine();
}
if (userChoice.charAt(0) == 'm' || userChoice.charAt(0) == 'M')
{
System.out.println("Enter a year greater than 1900");
year = input.nextInt();
while( year < 1900)
{
System.out.println("Invalid! Enter a year greater than 1900");
year = input.nextInt();
}
System.out
.println("Enter a month as a number. 1 for jan, 2 for feb, etc.");
month = input.nextInt();
while( month < 1 || month > 12)
{
System.out.println("Enter a month between 1 and 12");
month = input.nextInt();
}
}
GregorianCalendar date = new GregorianCalendar( year, month - 1, 1 );
int dayOfWeek = date.get( Calendar.DAY_OF_WEEK );
int maxNumberOfDays = date.getActualMaximum(Calendar.DATE);
String[] monthName = {"January", "February",
"March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
String monthStr = monthName[ month - 1 ];
System.out.println("\t\t\t"+ monthStr + " " + year);
for ( int i = 1; i <= 55; i++)
{
System.out.print("_");
}
System.out.println();
System.out.println();
System.out.println("Sun\tMon\tTus\tWed\tThu\tFri\tSat");
int maxOffset = dayOfWeek + maxNumberOfDays;
for ( int j = 1; j <= maxOffset; j++)
{
System.out.print( j+ "\t" );
}
System.out.println();
break;
}while (userChoice.charAt(0) != 'q' || userChoice.charAt(0) != 'Q');
}