使用java api类日历创建日历

时间:2015-01-24 05:48:14

标签: java calendar

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

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

1 个答案:

答案 0 :(得分:1)

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

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