通过输入特定日期来计算工作日

时间:2016-06-01 14:52:15

标签: java calendar

我需要创建一个程序,显示星期几,当您输入特定日期进行控制时。

这是我编写的代码,但仍然无效,为什么会这样?

public class CalculateDay{

     public static void main(String []args){


         int m = Integer.parseInt(args[0]);
         int d = Integer.parseInt(args[1]);
         int y = Integer.parseInt(args[2]);

         System.out.println("Finding day of week for "+m+"-"+d+"-"+y);


         int yo = y - ((14-m)/12);

         int x = yo + yo/4 - yo/100 + yo/400;

         int mo = m + 12*((14-m)/12) -2;

         int dayOfWeek = (d + x + (31*mo)/12)%7;

         String dayString;

         switch(dayOfWeek){
            case 0:    dayString = "Sunday";
                        break;
            case 1:    dayString = "Monday";
                        break;
            case 2:    dayString = "Tuesday";
                        break;
            case 3:    dayString = "Wednesday";
                        break;
            case 4:    dayString = "Thursday";
                        break;
            case 5:    dayString = "Friday";
                        break;
            case 6:    dayString = "Saturday";
                        break;            
            default:    dayString = "Could not find";
         }

        System.out.println(dayString);
     }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

int m = Integer.parseInt(args[0]);
int d = Integer.parseInt(args[1]);
int y = Integer.parseInt(args[2]);

System.out.println("Finding day of week for "+m+"-"+d+"-"+y);

Calendar cal = new GregorianCalendar(y, m, d);
System.out.println("DAY_OF_WEEK: " + cal.get(Calendar.DAY_OF_WEEK));