java calendar

the first week displays weirdly. pls help:
package lab4;
import java.util.Scanner;
import java.time.Month;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

public class lab4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int month;
int year;
Month selectedMonth;
while (true) {
System.out.println("Enter month: ");
month = input.nextInt();
if (month > 0 && month < 13) {
selectedMonth = Month.of(month);
break;
}
else {
System.out.println("Invalid input. Please enter a valid month (1-12).");
}
}
while (true) {
System.out.println("Enter year: ");
year = input.nextInt();
if (year > 0 && year < 10000) {
break;
}
else {
System.out.println("Invalid input. Please enter a valid year (1-).");
}
}

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.YEAR, year);

System.out.println(selectedMonth + ", " + year);

YearMonth yearMonth = YearMonth.of(year, month);
int daysInMonth = yearMonth.lengthOfMonth();

// Print the Calendar
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for (int i = 1; i < calendar.get(Calendar.DAY_OF_WEEK); i++) {
System.out.print(" ");
}
for (int i = 1; i <= daysInMonth; i++) {
System.out.printf("%3d ", i);
if((i + calendar.get(Calendar.DAY_OF_WEEK) + 1) % 7 == 0) {
System.out.println();
}
}
}

}
package lab4;
import java.util.Scanner;
import java.time.Month;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

public class lab4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int month;
int year;
Month selectedMonth;
while (true) {
System.out.println("Enter month: ");
month = input.nextInt();
if (month > 0 && month < 13) {
selectedMonth = Month.of(month);
break;
}
else {
System.out.println("Invalid input. Please enter a valid month (1-12).");
}
}
while (true) {
System.out.println("Enter year: ");
year = input.nextInt();
if (year > 0 && year < 10000) {
break;
}
else {
System.out.println("Invalid input. Please enter a valid year (1-).");
}
}

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.YEAR, year);

System.out.println(selectedMonth + ", " + year);

YearMonth yearMonth = YearMonth.of(year, month);
int daysInMonth = yearMonth.lengthOfMonth();

// Print the Calendar
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for (int i = 1; i < calendar.get(Calendar.DAY_OF_WEEK); i++) {
System.out.print(" ");
}
for (int i = 1; i <= daysInMonth; i++) {
System.out.printf("%3d ", i);
if((i + calendar.get(Calendar.DAY_OF_WEEK) + 1) % 7 == 0) {
System.out.println();
}
}
}

}
2 Replies
JavaBot
JavaBot•2w ago
⌛ This post has been reserved for your question.
Hey @duhless 🜲! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
JavaBot
JavaBot•2w ago
Post Closed
This post has been closed by <@734864042442883133>.
Want results from more Discord servers?
Add your server