本帖最后由 浪无痕-陈文坤 于 2013-8-3 00:20 编辑
- //按年份+月份
- import javax.swing.JOptionPane;
- class Time
- {
- public int year;
- public int month;
- public int alldays;//所有的天数;
- public int weekDay;//一月当中的第一天是星期几;
- public int monthDays;//每月的天数
- public boolean isRn;//判断是否闰年
- public Time()
- { //添加了年份弹出框输入功能:加了提示信息防止出错
- while(year<1898)
- {
- String Year=JOptionPane.showInputDialog("请输入年份,必须大于1898年! ");
- year=Integer.parseInt(Year);
- }
- //添加了月份弹出框输入功能:加了提示信息防止出错
- while(!(month>=1&&month<=12))
-
- {
- String Month=JOptionPane.showInputDialog("请输入月份 ,必须在1~12之间!");
- month=Integer.parseInt(Month);
- }
- //判断是否是闰年还是平年
- isRn=year%4==0&&year%100!=0||year%400==0;
- }
-
- public int getDays()
- { //获取从1898到(year-1)之间的总天数
- for(int i=1898;i<year;i++)
- {
- if(isRn)
- alldays +=366;
- else
- alldays +=365;
- }
- //月份相对应的天数和year年的总天数
- for (int i=1;i<month;i++)
- {
- switch(i)
- {
- case 4:
- case 6:
- case 9:
- case 11:
- alldays +=30;
- break;
- case 2:
- if(isRn)
- {
- alldays +=29;
- }
- else
- {
- alldays +=28;
- }
- break;
- default:
- alldays +=31;
-
- }
- }
- return alldays;
-
- }
- }
- public class TimeDemo
- {
- public static void main(String[] args)
- {
- System.out.println(new Time().getDays());
- }
- }
复制代码 |