Reputation:
I am creating a calendar which accepts an input value of 1-12 for months. The default value for year is current year so right now it is using a 2015 calendar I successfully created it but i have a problem that calendar.getDisplayName(Calendar.DAY_OF_WEEK, 1, Locale.US);
returns the current day value of the calendar and not the FIRST day of the month.. so my problem now is it doesn't print the 1st week of the month correctly and i have to get the First day of the month here is my code by the way
public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();
Scanner sc = new Scanner(System.in);
String[] day = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
int inputMonth = 0;
boolean validInputForMonth = false;
while(!validInputForMonth)
{
System.out.print("Enter a number for the month(1 to 12 only): ");
String input = sc.next(); // This will be the input of the user for the month
Pattern p = Pattern.compile("[1-9]{1}|1[0-2]{1}"); // Compiles the valid pattern for month input
Matcher m = p.matcher(input); // Matches the user input to the month input
boolean b = m.matches(); // if input is valid it will go to if statement else if false
if(b)
{
inputMonth = Integer.parseInt(input); // Parse the input string into Integer
if((inputMonth >= 1) || (inputMonth <= 12))
{
validInputForMonth = true; // returns true if input is 1 - 12
}
else
{
System.out.println("\n\nInvalid Month! Input number again for month.");
}
}
else
{
System.out.println("\n\nInvalid Month! Input number again for month.");
}
}
sc.close();
System.out.println("");
calendar.set(Calendar.MONTH, inputMonth - 1);
System.out.println(calendar.getDisplayName(Calendar.MONTH, 2, Locale.US) + " " + calendar.get(Calendar.YEAR));
String startDay = calendar.getDisplayName(Calendar.DAY_OF_WEEK, 1, Locale.US);
int totalDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
/* Loop to display the Days of the week */
for (int x = 0; x < day.length; x++)
{
System.out.print(day[x] + "\t");
}
System.out.println("");
int counter = 0;
int daysInTheWeek = 7;
/* Loop to print empty spaces until first day is found */
for (int x = 0; x < daysInTheWeek; x++)
{
if (day[x].equals(startDay))
{
System.out.print("");
break;
}
else
{
System.out.print("\t");
counter++;
}
}
int startingDate = 1; // Initialize the starting date to 1
/* Loop to display the days of the month on the 1st week */
for(int x = 1; x <= daysInTheWeek - counter; x++)
{
if(x == 1 && (daysInTheWeek - counter == 1))
{
System.out.println(x);
}
else if(x == 1)
{
System.out.print(x);
}
else if(x == daysInTheWeek - counter)
{
System.out.print("\t" + startingDate + "\n");
}
else
{
System.out.print("\t" + startingDate);
}
startingDate++;
}
/* Loop to display the days of the month after the 1st week is done */
do
{
for(int x = 1; x <= daysInTheWeek; x++)
{
if (x < daysInTheWeek)
{
System.out.print(startingDate + "\t");
if (startingDate < totalDays)
{
startingDate++;
}
if (startingDate == totalDays)
{
System.out.println(startingDate);
break;
}
}
else
{
System.out.println(startingDate);
if (startingDate < totalDays)
{
startingDate++;
}
if (startingDate == totalDays)
{
System.out.println(startingDate);
break;
}
}
}
}while (startingDate < totalDays);
}
Upvotes: 0
Views: 85
Reputation: 201487
If I understand your question then you need to also set the DAY_OF_THE_MONTH
like
calendar.set(Calendar.MONTH, inputMonth - 1);
calendar.set(Calendar.DAY_OF_MONTH, 1); // <-- add this
Upvotes: 0