Calendar.set()JAVA Basic

时间:2016-06-18 17:21:39

标签: java calendar

我正在编写我的代码,这是一个新问题。 即使我手动完成,今年也不会改变。

请看第一周的印刷品。 它应该改变,因为我已经改变了日历的年份。但仍然你输入的是你得到的.pls help.using the object.set(Calendar.YEAR,XXXX);

   package lessons;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class lesson4 {

    private static Scanner scanner;
    private static String[] days = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
    private static int max;
    static int counter = 0;
    static int currentDay;
    static GregorianCalendar gcal = new GregorianCalendar();

    public static void main(String[] args) {

        max = gcal.getActualMaximum(Calendar.DAY_OF_MONTH); // get the actualy
                                                            // Max day for the
                                                            // current month
        gcal.set(gcal.get(Calendar.YEAR), 0, 1);// default set current YEAR,
                                                // January in month and 1 in
                                                // date.
        scanner = new Scanner(System.in);

        ////////// gets input and set it to year.///////
        System.out.print("Enter Year:");
        int year = scanner.nextInt();// get input Year
        gcal.set(Calendar.YEAR, year);// Set year!
        ///////// sets current date to 1///////////////////
        gcal.set(Calendar.DATE, 1);// sets default date to 1.

        currentDay = gcal.get(Calendar.DAY_OF_WEEK);// gets current day of week.

        int max = gcal.getActualMaximum(Calendar.DAY_OF_MONTH); // get the
                                                                // actualy Max
                                                                // day for the
                                                                // current month

        printYear(year);// calls the method printyear to out put year 3 times.

        // for (int m = 0; m < 12; m++) {

        monthCheck(0);// calls monthCheck method that will print out months 3
                        // times.

        printDay();

        printFirstWeek();
        gcal.set(Calendar.YEAR, 1999); // look at this i set it manually
        printFirstWeek();
        gcal.set(Calendar.YEAR, 1860);// look at this i set it manually
        printFirstWeek();
        System.out.println();
        // }

    }

    // print year
    static void printYear(int y) {
        int dummyYear = y;
        for (int yi = 0; yi < 3; yi++) {
            if (yi == 0) {
                System.out.printf("%5d", dummyYear);
            } else {
                System.out.printf("%31d", dummyYear);
            }
            dummyYear++;
        }
        System.out.println();
    }

    // print month
    static void monthCheck(int m) {

        ArrayList<String> monthsList = new ArrayList<String>();
        String[] months = new DateFormatSymbols().getMonths();
        for (int i = 0; i < months.length - 1; i++) {
            // String monthArr = months[i];
            // System.out.println("month = " + month);
            monthsList.add(months[i]);
        }

        for (int y = 0; y < 3; y++) {
            if (y == 0) {
                System.out.printf("%5s", months[m]);
            } else {
                System.out.printf("%31s", months[m]);
            }
        }
        System.out.println();

    }

    static void printDay() {
        for (int j = 0; j < 3; j++) {
            for (int i = 0; i < 7; i++) {
                System.out.printf("%5s", days[i]);
            }
            if (j == 2) {

                System.out.println();
            }
        }

    }

    static void printFirstWeek() {

        gcal.set(2011, 0, 1); // ive used gcal here... still no change in output. the input is still the value.

        for (int iSpace = 0; iSpace < currentDay - 1; iSpace++) {

        System.out.printf("%5s", "");
        counter++;
        if (counter % 7 == 0) {
            System.out.print("W");

        }

    }

    for (int iDate = 1; iDate <= max + 1; iDate++) {
        System.out.printf("%5s", iDate);
        counter++;
        if (counter % 7 == 0) {
            System.out.print("");
            iDate = 99;

        }
    }

}

}

1 个答案:

答案 0 :(得分:0)

printFirstWeek不依赖于日历gcal,它取决于currentDaymax,更改gcal不会更新这些字段。

您需要像第一次使用

那样重新分配它们
max = gcal.getActualMaximum(Calendar.DAY_OF_MONTH);
currentDay = gcal.get(Calendar.DAY_OF_WEEK);