使用 java api class 日历创建日历

creating a calendar using java api class calendar

我正在创建一个日历,它接受 1-12 月份的输入值。年份的默认值是当前年份,所以现在它使用的是 2015 年日历我成功创建了它,但我有一个问题 calendar.getDisplayName(Calendar.DAY_OF_WEEK, 1, Locale.US); returns 日历的当前日期值而不是第一天这个月..所以我现在的问题是它没有正确打印这个月的第一周,我必须得到这个月的第一天,顺便说一句,这是我的代码

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);
}

如果我理解你的问题,那么你还需要将 DAY_OF_THE_MONTH 设置为

calendar.set(Calendar.MONTH, inputMonth - 1);
calendar.set(Calendar.DAY_OF_MONTH, 1); // <-- add this