请选择 进入手机版 | 继续访问电脑版
查看: 4466|回复: 8

电子日历代码(JAVA练习)

[复制链接]
发表于 2014-8-12 17:02:49 | 显示全部楼层 |阅读模式
本帖最后由 墨奚 于 2014-8-12 17:05 编辑


  1. /**                  电子日历查询                   */

  2. import java.util.Scanner;

  3. public class Test{
  4.         public static void main(String args[]){
  5.                 int setYear = 0,setMonth = 0,setDay = 0;
  6.                 boolean checkTime = false;
  7.                 int setY = 0,setM = 0,setD = 0;
  8.                 while(!checkTime){
  9.                         System.out.print("\n\n");
  10.                         Scanner get = new Scanner(System.in);
  11.                         System.out.print("请输入设定年份:");
  12.                         setY = get.nextInt();
  13.                         System.out.print("请输入设定月份:");
  14.                         setM = get.nextInt();
  15.                         System.out.print("请输入设定日期:");
  16.                         setD = get.nextInt();
  17.                         if(setY>=1900){
  18.                                 if(setM==2){
  19.                                         if((setY%400==0)||((setY%4==0)&&(setY%100!=0))){
  20.                                                 if(setD<=29){
  21.                                                         checkTime = true;
  22.                                                 }else{
  23.                                                         checkTime = false;
  24.                                                 }
  25.                                         }else{
  26.                                                 if(setD<=28){
  27.                                                         checkTime = true;
  28.                                                 }else{
  29.                                                         checkTime = false;
  30.                                                 }                                
  31.                                         }
  32.                                 }else if((setM==4)||(setM==6)||(setM==9)||(setM==11)){
  33.                                         if(setD<=30){
  34.                                                 checkTime = true;
  35.                                         }else{
  36.                                                 checkTime = false;
  37.                                         }
  38.                                 }else{
  39.                                         if(setD<=31){
  40.                                                 checkTime = true;
  41.                                         }else{
  42.                                                 checkTime = false;
  43.                                         }                        
  44.                                 }        
  45.                         }else{
  46.                                 checkTime = false;
  47.                         }
  48.                         if(!checkTime){
  49.                                 System.out.println("输入的时间无效,请重新输入:");
  50.                         }else{
  51.                                 System.out.println("操作成功!");
  52.                                 System.out.println("\n正在创建电子日历......");
  53.                         }
  54.                 }
  55.                 setYear = setY;
  56.                 setMonth = setM;
  57.                 setDay = setD;
  58.                 int i = 0,sum = 0,wekDay = 0,monthLong = 0;
  59.                 char weekChar = ' ';
  60.                 for(i=1990;i<setYear;i++){
  61.                         if((i%400==0)||((i%4==0)&&(i%100!=0))){
  62.                                 sum += 366;
  63.                         }else{
  64.                                 sum += 365;
  65.                         }
  66.                 }
  67.                 for(i=1;i<setMonth;i++){
  68.                         if(i==2){
  69.                                 if((setYear%400==0)||((setYear%4==0)&&(setYear%100!=0))){
  70.                                         sum += 29;
  71.                                 }else{
  72.                                         sum += 28;
  73.                                 }
  74.                         }else if((i==4)||(i==6)||(i==9)||(i==11)){
  75.                                 sum += 30;
  76.                         }else{
  77.                                 sum += 31;
  78.                         }
  79.                 }
  80.                 if(setMonth==2){
  81.                         if((setYear%400==0)||((setYear%4==0)&&(setYear%100!=0))){
  82.                                 monthLong = 29;
  83.                         }else{
  84.                                 monthLong = 28;
  85.                         }
  86.                 }else if((setMonth==4)||(setMonth==6)||(setMonth==9)||(setMonth==11)){
  87.                         monthLong = 30;
  88.                 }else{
  89.                         monthLong = 31;
  90.                 }
  91.                 switch((sum+setDay)%7){
  92.                         case 0:weekChar = '日';break;
  93.                         case 1:weekChar = '一';break;
  94.                         case 2:weekChar = '二';break;
  95.                         case 3:weekChar = '三';break;
  96.                         case 4:weekChar = '四';break;
  97.                         case 5:weekChar = '五';break;
  98.                         case 6:weekChar = '六';break;
  99.                 }
  100.                 sum += 1;
  101.                 wekDay = sum%7;
  102.                 System.out.println("\n\n设定日期为:"+setYear+"-"+setMonth+"-"+setDay+"\t星期"+weekChar+"\n\n");
  103.                 System.out.println("日\t一\t二\t三\t四\t五\t六");
  104.                 for(i=0;i<(wekDay%7);i++){
  105.                         System.out.print("\t");
  106.                 }
  107.                 for(i=1;i<=monthLong;i++){
  108.                         if(sum%7==6){
  109.                                 System.out.print(i+"\n");
  110.                         }else{
  111.                                 System.out.print(i+"\t");
  112.                         }
  113.                         sum ++;
  114.                 }
  115.                 System.out.println("\n\n创建成功!\n");
  116.         }
  117. }
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?加入因仑

x
回复

使用道具 举报

发表于 2014-8-12 19:47:41 | 显示全部楼层
从哪一年开始
回复 支持 反对

使用道具 举报

发表于 2014-8-12 19:51:33 | 显示全部楼层

再加一个功能,实现显示后N个月日历
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-12 19:58:26 | 显示全部楼层

>=1900年        
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-12 19:59:00 | 显示全部楼层
伊海 发表于 2014-8-12 19:51
再加一个功能,实现显示后N个月日历

显示那么多干嘛?   
回复 支持 反对

使用道具 举报

发表于 2014-8-13 08:48:40 | 显示全部楼层
太好了,楼主
回复 支持 反对

使用道具 举报

发表于 2014-8-13 14:12:56 | 显示全部楼层
很牛的感觉,以后就不用看日历了,自己做一个就行了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 加入因仑

本版积分规则

快速回复 返回顶部 返回列表