无法在Java上打印正确的日历

时间:2015-10-07 02:50:15

标签: java methods calendar

任何人都可以帮我找到我的代码有什么问题吗?我无法弄清楚如何显示当月的正确开始日期(请注意,1900年1月1日是星期一)或者如果年份是闰年,则显示2月份的正确天数。

public class Calendar {
/**
 * 
 * 
 * @param s
 * @return 
 */ 
    public static int getMonthNumber(String s){

            if (s.equalsIgnoreCase("jan"))
                return 1;
            if (s.equalsIgnoreCase("feb"))
                return 2;
            if (s.equalsIgnoreCase("mar"))
                return 3;
            if (s.equalsIgnoreCase("apr"))
                return 4;
            if (s.equalsIgnoreCase("may"))
                return 5;
            if (s.equalsIgnoreCase("jun"))
                return 6;
            if (s.equalsIgnoreCase("jul"))
                return 7;
            if (s.equalsIgnoreCase("aug"))
                return 8;
            if (s.equalsIgnoreCase("sep"))
                return 9;
            if (s.equalsIgnoreCase("oct"))
                return 10;
            if (s.equalsIgnoreCase("nov"))
                return 11;
            if (s.equalsIgnoreCase("dec"))
                return 12;
            else
                System.out.println("Not valid month!");
            return 0;
 }
    public static boolean isLeapYear(int year){
       int month = 0;
        int s = getDaysIn(month, year);
        return year%4==0 && (year % 100 != 0) || (year % 400 == 0); 

    }
    public static int getDaysIn (int month, int year){

        switch (month) {
            case 1:  return 31; 
            case 2:  if(isLeapYear(month)) return 29;
            else return 28;
            case 3:  return 31; 
            case 4:  return 30;
            case 5:  return 31; 
            case 6:  return 30; 
            case 7:  return 31; 
            case 8:  return 31; 
            case 9:  return 30; 
            case 10: return 31; 
            case 11: return 30; 
            case 12: return 31; 
            default: return -1;
    } 
    }
    public static String getMonthName (int month){         
        switch (month) {
            case 1:  return "January";
            case 2:  return "February"; 
            case 3:  return "March";
            case 4:  return "April"; 
            case 5:  return "May"; 
            case 6:  return "June";
            case 7:  return "July"; 
            case 8:  return "August"; 
            case 9:  return "September"; 
            case 10: return "October"; 
            case 11: return "November"; 
            case 12: return "December"; 
            default: return "Invalid month.";
    } 

    }
    public static int getStartDay (int month, int year){
        int days = 0;

        for (int i = 1900; i<year; i++){
            days = days + 365;
            if (isLeapYear(i))
                days = days + 1;}
        for (int i=1; i<month; i++)
            days = days + getDaysIn(month, year);
        int startday = (days + 1)%7+1; 
        return startday;
    }
    public static void displayCalendar(int month, int year){

        String monthname=getMonthName(month);
        int startDay= getStartDay(month, year);
        int monthDays = getDaysIn(month, year);

        System.out.println("   Sun   Mon   Tue   Wed   Thu   Fri   Sat");
        int weekDay = startDay-1;
        for (int i=1; i<=startDay; i=i+1)
            System.out.print("    ");    
        for ( int x=1; x<=monthDays; x++){
           weekDay = weekDay +1;
            if (weekDay>7){
                System.out.println();
            weekDay = 1;}
        System.out.format("   %3d",x);     
    }
        if (weekDay > 7){
            System.out.println();

        }
    }
      public static void main(String[] args){
      Scanner c = new Scanner (System.in);
      System.out.print("Give the first three letters of a month and enter the year: ");
      String month,year;
      month=c.next();
      year=c.next();

      int yearno =Integer.parseInt(year); 

      int monthno = getMonthNumber(month);

      displayCalendar(monthno, yearno);

      }  
    }

2 个答案:

答案 0 :(得分:0)

假设您可以使用java.util.Calendar,我会编写如下代码:

import java.util.Calendar;
import java.util.Scanner;

public class CalendarPrinter {

    private Calendar c = null;

    CalendarPrinter(int month, int year ){
        c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.MONTH, month);
        c.set(Calendar.YEAR, year);
    }

    public String showCalendar(){
        StringBuffer calendarTxt = new StringBuffer("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
        int daysInAMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        int firstDayOfWeek = c.getFirstDayOfWeek();
        int i = 1;
        do {
            if (i < c.get(Calendar.DAY_OF_WEEK)){
                calendarTxt.append("\t");
                daysInAMonth++;
            } else {
                calendarTxt.append(firstDayOfWeek+"\t");
                firstDayOfWeek++;
            }
            if (i%7 == 0 && i != 1){
                calendarTxt.append("\n");
            }
            i++;
        } while (i <= daysInAMonth);
        return calendarTxt.toString();
    }

    public static int getMonthNumber(String s){

        if (s.equalsIgnoreCase("jan"))
            return 0;
        if (s.equalsIgnoreCase("feb"))
            return 1;
        if (s.equalsIgnoreCase("mar"))
            return 2;
        if (s.equalsIgnoreCase("apr"))
            return 3;
        if (s.equalsIgnoreCase("may"))
            return 4;
        if (s.equalsIgnoreCase("jun"))
            return 5;
        if (s.equalsIgnoreCase("jul"))
            return 6;
        if (s.equalsIgnoreCase("aug"))
            return 7;
        if (s.equalsIgnoreCase("sep"))
            return 8;
        if (s.equalsIgnoreCase("oct"))
            return 9;
        if (s.equalsIgnoreCase("nov"))
            return 10;
        if (s.equalsIgnoreCase("dec"))
            return 11;
        else
            System.out.println("Not valid month!");
        return 0;
    }

    public static void main(String[] args){
        Scanner input = new Scanner (System.in);
        System.out.print("Give the first three letters of a month and enter the year: ");
        String month;
        int year;
        month=input.next();
        year=input.nextInt();
        CalendarPrinter c = new CalendarPrinter(getMonthNumber(month), year);
        System.out.println(c.showCalendar());

    }
}

答案 1 :(得分:0)

以下是基于Zeller's congruence的星期几的示例。

enter image description here

示例代码:

cmsDataType

正在运行代码here

代码参考here