| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3631 人关注过本帖
标题:如何计算某年某月的第一天是星期几?
只看楼主 加入收藏
也许等直到
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-2-25
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:3 
如何计算某年某月的第一天是星期几?
下面这段代码我的目的是就算某年某月的第一天是星期几,可是不对,我是按照这种方法做的  :1900年1月1日星期一 作为标杆
然后算当月第一天距1900.1.1多少天,取余7,不知道哪里有问题,求指教啊
package rili;

import java.util.Scanner;

public class rili {
    boolean isRunNian(int year) {
        if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
            return true;
        else
            return false;
    }

    int calcDayOfMonth(int year, int month) {
        int days = 0;
        int sum = 0;
        for (int i = 1; i <= month; i++) {
            switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                if (isRunNian(year))
                    days = 29;
                else
                    days = 28;

            }
            sum += days;
        }
        return sum;
    }

    int calcDays(int year, int month) {
        int sum = 0;
        int days;
        for (int i = 1900; i < year; i++) {
            if (isRunNian(i)) {
                sum += 366;
            } else {
                sum += 365;
            }
        }
        // 对月份中的天数进行处理
        days = calcDayOfMonth(year, month);
        sum += days;
        return sum;

    }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        rili r=new rili();
        Scanner s = new Scanner(System.in);
        int year;
        int month;
        year = s.nextInt();
        month = s.nextInt();
        int sum=r.calcDays(year,month-1);
        System.out.println((sum)%7+1);

    }

}
搜索更多相关主题的帖子: package public return import false 
2016-03-21 13:37
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:10 
import java.util.Scanner;

 public class S {
     boolean isRunNian(int year) {
         if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
             return true;
         else
             return false;
     }

     int calcDayOfMonth(int year, int month) {
         int days = 0;
         int sum = 0;
         for (int i = 1; i <= month; i++) {
             switch (i) {            //这里应该用i做参数
             case 1:
             case 3:
             case 5:
             case 7:
             case 8:
             case 10:
             case 12:
                 days = 31;
                 break;
             case 4:
             case 6:
             case 9:
             case 11:
                 days = 30;
                 break;
             case 2:
                 if (isRunNian(year))
                     days = 29;
                 else
                     days = 28;

             }
             sum += days;
         }
         return sum;
     }

     int calcDays(int year, int month) {
         int sum = 0;
         int days;
         for (int i = 1900; i < year; i++) {
             if (isRunNian(i)) {
                 sum += 366;
             } else {
                 sum += 365;
             }
         }
         // 对月份中的天数进行处理
        days = calcDayOfMonth(year, month);
         sum += days;
         return sum;

     }

     public static void main(String[] args) {
         // TODO 自动生成的方法存根
        S r=new S();
         Scanner s = new Scanner(System.in);
         int year;
         int month;
         year = s.nextInt();
         month = s.nextInt();
         int sum=r.calcDays(year,month-1);
         System.out.println((sum)%7+1);

     }
}
2016-03-21 14:29
也许等直到
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-2-25
收藏
得分:0 
谢谢你啊,比我仔细多了,我太不仔细了,求个联系方式吧,以后多沟通。我QQ1020196987

比你优秀的人不可怕,可怕的是比你优秀的人比你更努力
2016-03-21 16:14
a8191586
Rank: 2
等 级:论坛游民
威 望:1
帖 子:3
专家分:14
注 册:2016-3-4
收藏
得分:10 
回复 2楼 grmmylbs
经过测试。这段代码,是错误的。改正后。无法得到,想要的结果
2016-03-23 13:06
快速回复:如何计算某年某月的第一天是星期几?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029052 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved