您的位置:首页 > 移动开发 > 微信开发

java日历小程序

2015-09-05 10:28 597 查看


java  自己的计算机简易版的日历,其中还有点问题没有解决,就是关于中国农历的算法,还是有一定的误差,如果大家有什么稿件可以一起互相讨论一下!

界面那一点代码没有放进去,会传文件版的上去!

import java.util.Scanner;

public class Calendersssss {

 Calendersssss(){

  Scanner reader = new Scanner(System.in);

  System.out.println("\t\t\t\t欢迎使用筱瑞万年历查询\t\t\t\t"

    + "\n*************************************************"

    + "***************************************************");

  System.out

  .println("please input the year what your rearch for(after the year 1905)"

    + "(请输入你要查询的年份:1905年以后的年份):");

  int year = reader.nextInt();

  while (year < 1905) {

   System.out.println("please input the year again(请重新输入年份:):");

   year = reader.nextInt();

  }// 输入年份

  int sumDay = 0;

  int x = 1905;

  while (x < year) {

   if (((x % 4 == 0) && (x % 100 != 0)) || (x % 400 == 0))

    sumDay += 366;

   else

    sumDay += 365;

   x++;

  }

  // 计算年份的总天数

  int temp1 = sumDay;//计算农历日期

  System.out

  .println("please input the month what your rearch for(1~12)(请输入月份1~12月):");

  int month = reader.nextInt();

  while (month < 1 || month > 12) {

   System.out.println("please input the month again(请重新输入月份):");

   month = reader.nextInt();

  }

  switch (month - 1) {

  case 12:

   sumDay += 31;

  case 11:

   sumDay += 30;

  case 10:

   sumDay += 31;

  case 9:

   sumDay += 30;

  case 8:

   sumDay += 31;

  case 7:

   sumDay += 31;

  case 6:

   sumDay += 30;

  case 5:

   sumDay += 31;

  case 4:

   sumDay += 30;

  case 3:

   sumDay += 31;

  case 2: {

   if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))

    sumDay += 29;

   else

    sumDay += 28;

   ;

  }

  case 1:

   sumDay += 31;

   break;

  default:

   sumDay += 0;

  }

  // 计算年份的天数加上月份的天数

  int  temp2 = sumDay - temp1;//计算农历日期

  int first = sumDay % 7;// 该月的第一天星期几

  System.out.println("please input the day of  " + year + "年 " + month

    + "月 what your rearch for (请输入某日):");

  int day = reader.nextInt();

  int temp = 0;

  if (month == 2) {

   if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))

    temp = 29;

   else {

    temp = 28;

   }

  }

  if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8

    || month == 10 || month == 12) {

   temp = 31;

  }

  if (month == 4 || month == 6 || month == 9 || month == 11) {

   temp = 30;

  }

  // temp2+=day;//计算农历日期.........................

  if (day > temp || day < 0) {

   System.out.println("please input the day again(请重新输入): ");

   day = reader.nextInt();

  }// 输入某天后的总天数

  int week = (sumDay + day) % 7;// 所查询的某一天是星期几

  String weeks = null;

  switch (week-1) {

  case 1:

   weeks = "一";

   break;

  case 2:

   weeks = "二";

   break;

  case 3:

   weeks = "三";

   break;

  case 4:

   weeks = "四";

   break;

  case 5:

   weeks = "五";

   break;

  case 6:

   weeks = "六";

   break;

  case 0:

   weeks = "日";

   break;

  default: weeks = "日";

  }

  //***************************************************************

  int x1 = year%60 - 3;

  if(x1<1){

   x1+=60;

  }

  String ganZhi = null;

  switch(x1){

  case 1: ganZhi = "甲子";break; case 2: ganZhi = "乙丑";break; case 3: ganZhi = "丙寅" ;break;

  case 4: ganZhi = "丁卯 ";break; case 5:ganZhi = "戊辰";break; case 6: ganZhi = "己巳 ";break;

  case 7: ganZhi = "庚午";break; case 8:ganZhi = "辛未";break; case 9: ganZhi = "壬申";break;

  case 10: ganZhi = "癸酉";break; case 11:ganZhi = "甲戌";break; case 12:ganZhi = "乙亥";break;

  case 13:ganZhi = "丙子";break; case 14:ganZhi = "丁丑";break;case 15:ganZhi ="戊寅"; break;

  case 16:ganZhi = "己卯 ";break; case 17:ganZhi = "庚辰 ";break; case 18:ganZhi = "辛巳";break;

  case 19:ganZhi = "壬午";break; case 20:ganZhi = "癸未";break; case 21:ganZhi = "甲申";break;

  case 22:ganZhi = "乙酉";break; case 23:ganZhi = "丙戌";break; case 24:ganZhi = "丁亥 ";break;

  case 25:ganZhi = "戊子";break; case 26:ganZhi = "己丑";break; case 27:ganZhi = "庚寅";break;

  case 28:ganZhi = "辛卯";break; case 29:ganZhi = "壬辰";break; case 30:ganZhi = "癸巳";break;

  case 31:ganZhi = "甲午";break; case 32:ganZhi = "乙未 ";break; case 33:ganZhi = "丙申";break;

  case 34:ganZhi = "丁酉";break; case 35:ganZhi = "戊戌 ";break; case 36:ganZhi = "己亥";break;

  case 37:ganZhi = "庚子";break; case 38:ganZhi = "辛丑";break; case 39:ganZhi = "任寅";break;

  case 40:ganZhi = "癸卯 ";break; case 41:ganZhi = "甲辰 ";break; case 42:ganZhi = "乙巳";break;

  case 43:ganZhi = "丙午";break; case 44:ganZhi = "丁未";break; case 45:ganZhi = "戊申";break;

  case 46:ganZhi = "己酉";break; case 47:ganZhi = "庚戌";break; case 48:ganZhi = "辛亥";break;

  case 49:ganZhi = "壬子";break; case 50:ganZhi = "癸丑";break; case 51:ganZhi = "甲寅";break;

  case 52:ganZhi = "乙卯";break; case 53:ganZhi = "丙辰";break; case 54:ganZhi = "丁己";break;

  case 55:ganZhi = "戊午";break; case 56:ganZhi = "己未 ";break; case 57:ganZhi = "庚申";break;

  case 58:ganZhi = "辛酉";break; case 59:ganZhi = "壬戌";break; case 60:ganZhi = "癸亥";break; 

  }//判断天干地支

  int x2 = (year-1900)%12+1;

  String animal = null;

  switch(x2){

  case 1: animal = "(子)鼠";break; case 2: animal = "(丑)牛";break; case 3: animal = "(寅)虎" ;break;

  case 4: animal = "(卯) 兔 ";break; case 5:animal = "(辰)龙";break; case 6: animal = "(巳)蛇";break;

  case 7: animal = "(午)马";break; case 8:animal = "(未)羊";break; case 9: animal = "(申)猴";break;

  case 10: animal = "(酉)鸡";break; case 11:animal = "(戌)狗";break; case 0:animal = "(亥)猪";break;

  }//判断年份的生肖

  switch(x2){

  }

  System.out.println("\n"+year+"年   "+month+"月 "+day+"日农历 : ["+ganZhi+" "+animal+"年]" );//计算农历日期

  System.out.println(year + "年 " + month + "月 " + day + "日 :星期" + weeks);

  System.out.println("\n星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t");

  for (int i =1; i < week; i++) {

   System.out.print("\t");

  }

  System.out.print(" ^^ ");//输出星期

  int x3 = 6 - first;

  System.out.println("\n\n" + year + "年 " + month + "月的日历: ");

  System.out.println("\n星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t\n");

  for (int i = 0; i < first; i++) {

   System.out.print("\t");

  }

  int q,r,n;

  q = (year-1901)/4;

  r = (year-1901)%4;

  int lo;

  String Li = null;

  int j1,t;

  int a,b=0;

  for (int i = 1 ; i <= temp; i++) {

   if(month == 1 && i == 1){

    System.out.print("(元旦)\t");

   }else if(month == 2 && i == 2){

    System.out.print("(湿地日)\t");

   }else if(month == 2 && i == 14){

    System.out.print("(情人节)\t");

   }else if(month == 3 && i == 3){

    System.out.print("(爱耳日)\t");

   }else if(month == 3 && i == 5){

    System.out.print("(志愿者)\t");

   }else if(month == 3 && i == 8){

    System.out.print("(妇女节)\t");

   }else if(month == 3 && i == 9){

    System.out.print("(母亲河)\t");

   }else if(month == 3 && i == 12){

    System.out.print("(植树节)\t");

   }else if(month == 3 && i == 14){

    System.out.print("(警察日)\t");

   }else if(month == 3 && i == 15){

    System.out.print("(消费者日)");

   }else if(month == 3 && i == 21){

    System.out.print("(森林日)\t");

   }else if(month == 3 && i == 22){

    System.out.print("(水日)\t");

   }else if(month == 3 && i == 23){

    System.out.print("(气象日)\t");

   }

   else if(month == 3 && i == 24){

    System.out.print("(防结核病日)");

   }else if(month == 4 && i == 1){

    System.out.print("(愚人节\t");

   }else if(month == 4 && i == 5){

    System.out.print("(清明节)\t");

   }else if(month == 4 && i == 7){

    System.out.print("(卫生日)\t");

   }else if(month == 4 && i == 22){

    System.out.print("(地球日)\t");

   }else if(month == 4 && i == 26){

    System.out.print("(知识产权日)");

   }else if(month == 5 && i == 1){

    System.out.print("(劳动节)\t");

   }else if(month == 5 && i == 3){

    System.out.print("(哮喘日)\t");

   }else if(month == 5 && i == 4){

    System.out.print("(青年节)\t");

   }else if(month == 5 && i == 8){

    System.out.print("(红十字日)");

   }else if(month == 5 && i == 12){

    System.out.print("(护士节)\t");

   }else if(month == 5 && i == 15){

    System.out.print("(家庭日)\t");

   }else if(month == 5 && i == 17){

    System.out.print("(电信日)\t");

   }else if(month == 5 && i == 20){

    System.out.print("(学生营养日)");

   }else if(month == 5 && i == 23){

    System.out.print("(牛奶日)\t");

   }else if(month == 5 && i == 31){

    System.out.print("(无烟日)\t");

   }else if(month == 6 && i == 1){

    System.out.print("(儿童节)\t");

   }else if(month == 6 && i == 6){

    System.out.print("(爱眼日)\t");

   }else if(month == 6 && i == 17){

    System.out.print("(防旱日)\t");

   }else if(month == 6 && i == 23){

    System.out.print("(奥运日)\t");

   }else if(month == 6 && i == 25){

    System.out.print("(土地日)\t");

   }else if(month == 6 && i == 26){

    System.out.print("(禁毒日)\t");

   }else if(month == 7 && i == 1){

    System.out.print("(共产党日)\t");

   }else if(month == 7 && i == 7){

    System.out.print("(抗日 日)\t");

   }else if(month == 7 && i == 11){

    System.out.print("(人口日)\t");

   }else if(month == 8 && i == 1){

    System.out.print("(建军节)\t");

   }else if(month == 8 && i == 12){

    System.out.print("(青年节)\t");

   }else if(month == 9 && i == 8){

    System.out.print("(扫盲日)\t");

   }else if(month == 9 && i == 10){

    System.out.print("(教师节)\t");

   }else if(month == 9 && i == 16){

    System.out.print("(脑健康日)");

   }else if(month == 9 && i == 20){

    System.out.print("(爱牙日)\t");

   }else if(month == 9 && i == 21){

    System.out.print("(停火日)\t");

   }else if(month == 9 && i == 27){

    System.out.print("(旅游日)\t");

   }else if(month == 10 && i == 1){

    System.out.print("(国庆节)\t");

   }else if(month == 10 && i == 4){

    System.out.print("(动物日)\t");

   }else if(month == 10 && i == 5){

    System.out.print("(教师日)");

   }else if(month == 10 && i == 8){

    System.out.print("(高血压日)");

   }else if(month == 10 && i == 9){

    System.out.print("(邮政日)\t");

   }else if(month == 10 && i == 10){

    System.out.print("(精神卫生日)");

   }else if(month == 10 && i == 15){

    System.out.print("(盲人节)\t");

   }else if(month == 10 && i == 16){

    System.out.print("(粮食日)\t");

   }else if(month == 10 && i == 17){

    System.out.print("(消除贫困日)");

   }else if(month == 10 && i == 29){

    System.out.print("(生物多样性日)");

   }else if(month == 10 && i == 31){

    System.out.print("(万圣节)\t");

   }else if(month == 11 && i == 8){

    System.out.print("(中国记者日) ");

   }else if(month == 11 && i == 9){

    System.out.print("(消防日)\t");

   }else if(month == 11 && i == 14){

    System.out.print("(糖尿病日)");

   }else if(month == 11 && i == 17){

    System.out.print("(大学生节)\t");

   }else if(month == 12 && i == 1){

    System.out.print("(艾滋病日)");

   }else if(month == 12 && i == 4){

    System.out.print("(法制宣传日) ");

   }else if(month == 12 && i == 9){

    System.out.print("(足球日)\t");

   }else if(month == 12 && i == 1){

    System.out.print("(生物多样性日) ");

   }else{

    System.out.print(" "+i + "\t");}

   if ((i + first) % 7 == 0|| i==temp) {

    System.out.print("\n");

    a=b;

    b=i;

    //排版输出 查询的某个月的日历

    for( j1 = a+1;j1 <=b; j1++){

     double s = 14*q + 10.6*(r+1)+temp2+j1;

     lo = (int)(s%29.5);

     if(year == 2015&&month == 1&&day == 19)

      lo=29;

     switch(lo){

     case 1: Li = "初一";break; case 2: Li = "初二";break; case 3: Li = "初三" ;break;

     case 4: Li = "初四 ";break; case 5:Li = "初五";break; case 6: Li = "初六 ";break;

     case 7: Li = "初七";break; case 8:Li = "初八";break; case 9: Li = "初九";break;

     case 10: Li = "初十";break; case 11:Li = "十一";break; case 12:Li = "十二";break;

     case 13:Li = "十三";break; case 14:Li = "十四";break;case 15:Li ="十五"; break;

     case 16:Li = "十六 ";break; case 17:Li = "十七 ";break; case 18:Li = "十八";break;

     case 19:Li = "十九";break; case 20:Li = "二十";break; case 21:Li = "廿一";break;

     case 22:Li = "廿二";break; case 23:Li = "廿三";break; case 24:Li = "廿四 ";break;

     case 25:Li = "廿五";break; case 26:Li = "廿六";break; case 27:Li = "廿七";break;

     case 28:Li = "廿八";break; case 29:Li = "廿久";break; case 30:Li = "三十";break;

     }

     if((j1 + first + x3)==7){

      for ( int j = 0; j < first; j++) {

       System.out.print("\t");

      }}

     System.out.print(Li + "\t");

     if ((j1 + first) % 7 == 0) {

      System.out.print("\n\n");

     }//排版输出 所查询的某个月的日历

    }

   }

  }}

 public static void main(String[] args) {

  Scanner reader = new Scanner(System.in);

  new Calendersssss();

  System.out.println("\n\n您是否继续查询yes/no?");

  String x= reader.next();

        while(true){

         if(x.equals("yes")){

          new Calendersssss();

         }

         else{

          System.out.println("\t\t\t\t\t\t感谢你使用筱瑞万年历查询!");

          break;

         }

        }

 }

}

/*

 * 遇到的困难和解决方法:

 *  1.计算年份的总天数时 while(x<year)

 *  2.计算月份的总天数时

 *  3.计算某月的第一天是星期几时,利用该月的上一个月的最后一天解决

 *  4.排版月的天数与星期对应时

 *  5.if(((x%4==0)&&(x%100!=0))||(x%400==0))

 *  6.月底与月首的衔接关系

 *  7.java中 一键规范格式ctrl + shift + f

 *  8.String 最好副初值null

 *  @author 吴鹏

 */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 日历