使用Calendar API横向打印日历

时间:2016-06-15 15:49:12

标签: printing calendar format

大家好我有这个问题..我想使用Calendar API打印日历。

条件: 用户输入一年,一个介于1000 - 9999之间的整数。 那么它将输出当年的日历和接下来的两年。 所以,如果我输入2012,它将输出2012年,2013年和2014年的日历。 这里必须是SIDEWAYS!

package lessons;

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

public class lesson3 {
    static String[] days = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
    static int counter = 0;

    public static void main(String[] args) {
        GregorianCalendar gcal = new GregorianCalendar();

        Scanner scanner = new Scanner(System.in);
        System.out.print("Year: ");
        int input = scanner.nextInt(); //month input


        //System.out.print("YEAR (): ");
        //int input2 = scanner.nextInt();

        scanner.close();



        int year = input;
        gcal.set(Calendar.YEAR,input);

        for(int monthCount = 0;monthCount<12;monthCount++){

            gcal.set(Calendar.MONTH,monthCount); // month counter 0 - 11 producing jan to dec.
            monthCheck(monthCount); // converting months to readable names

            int x = gcal.get(Calendar.DAY_OF_WEEK);
            int max = gcal.getActualMaximum(Calendar.DAY_OF_MONTH);


            for(int i = 0;i<days.length;i++){
                System.out.printf("%8s",days[i]);

            }
            System.out.println("");




            for(int i = 0; i < x-1;i++){
                System.out.printf("%8s","");
                counter++;
                if(counter%7==0){
                    System.out.println("");
                }


            }

            for(int i = 1;i<=max;i++){
                System.out.printf("%8s",i);
                counter++;
                if(counter%7==0){
                    System.out.println("");
                }

            }
            System.out.println("");
            //System.out.println(x);
        }
    }
    static void monthCheck(int monthCount){

        ArrayList monthsList = new ArrayList<String>();
        String[] months = new DateFormatSymbols().getMonths();
        for (int i = 0; i < months.length-1; i++) {
          String monthArr = months[i];
          monthsList.add(months[i]);

        }
        System.out.println(months[monthCount]);

    }





}

This is what i can do with the code Above, click here!

this is what i am aiming for now. CLICK HERE!

0 个答案:

没有答案