zoukankan      html  css  js  c++  java
  • 获取某年某月某日是周几

    public static void getweek(){

    /*
    遇到整百年时(如2000,1900,300)要被400整除才是闰年,否则为平年
    (2000闰年,1900平年,300平年)
    ;遇到非整百年时(如2004,2005),只要被4整除就是闰年,不能被4整除为平年
    (2004闰年,2005平年)。
    闰年的2月有29天,平年的2月有28天。
    */
    Scanner scan = new Scanner(System.in);
    int allday = 0;//总天数
    System.out.println("请输入年份:");
    int year = scan.nextInt();//年
    System.out.println("请输入月份:");
    int month = scan.nextInt();//月
    System.out.println("请输入日期:");
    int day = scan.nextInt();//日
    if ((year % 100 != 0 && year % 4 == 0) || (year % 100 == 0 && year % 400 == 0)) {
    System.out.println(year + " 是闰年!");
    }else {
    System.out.println(year + " 是平年!");
    }
    for (int i = 1900; i < year; i++) {
    if ((i % 100 != 0 && i % 4 == 0) || (i % 100 == 0 && i % 400 == 0)) {
    allday += 366;
    }else {
    allday += 365;
    }
    }
    for (int i = 1; i < month; i++) {
    switch (i) {
    case 4:
    case 6:
    case 9:
    case 11:
    allday += 30;
    break;
    case 2:
    if ((year % 100 != 0 && year % 4 == 0) || (year % 100 == 0 && year % 400 == 0)) {
    allday += 29;
    }else {
    allday += 28;
    }
    break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
    allday += 31;
    break;
    }
    }
    allday = allday + day;
    System.out.println(year + "年" + month + "月" + day + "日距 1900年1月1日 共 " + allday + " 天!");
    int week = allday % 7;
    System.out.println("周" + week);
    }

    /***********运行如下************/

    请输入年份:
    2016
    请输入月份:
    10
    请输入日期:
    3
    2016 是闰年!
    2016年10月3日距 1900年1月1日 共 42645 天!
    周1

  • 相关阅读:
    WML
    CocoaPods install 安装
    刷新控件,审核
    UIImageView
    课堂练习——返回一个一维整数数组中最大子数组的和
    团队与题目介绍
    学习进度条05
    软件工程结对作业02
    课堂练习——返回一个整数数组中最大子数组的和
    学习进度条04
  • 原文地址:https://www.cnblogs.com/weloglog888/p/5929148.html
Copyright © 2011-2022 走看看