输入日期时间得到天数

前期准备工作(包括相关工具或所使用的原料等)eclipseJava 详细的操作方法或具体步骤
代码如下:

代码1
package com.huangtao.sxt;
import java.util.Scanner;
public class Test1 {
public static void main(String args[]){
//创建日期对象
System.out.println("输入年份");
int y=getScanner();
System.out.println("输入月份");
int m=getScanner();
System.out.println("输入日期");
int d=getScanner();
YearMonthDay day=new YearMonthDay(y,m,d);
System.out.println("输入日期是:"+day.getYear()+"年"+day.getMonth()+"月"+day.getDay()+"日");
day.isExits();
System.out.println("第"+day.totleDays()+"天");
}
/*
* 获取输入
*/
public static int getScanner(){
Scanner sc=new Scanner(System.in);
return sc.nextInt();
}
}
演示

package com.huangtao.sxt;
public class YearMonthDay {
private int year;
private int month;
private int day;
//使用一个静态数组变量
public static int[] months={31,28,31,30,31,30,31,31,30,31,30,31};
public int getYear() {
return year;
}
public void setYear(int year) {
this.year=year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month=month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day=day;
}
public YearMonthDay(){
}
public YearMonthDay(int year, int month, int day) {
super();
this.year=year;
this.month=month;
this.day=day;
}
/*
* 判断是否是闰年
* 4年一闰,百年不闰,400年又闰
*/
private void isRunYear(){
if((this.year%4==0&&this.year%100!=0)||this.year%400==0){
//闰年
months[1]=29;
}else{
//平年
months[1]=28;
}
}
/*
* 天数计算
*/
public int totleDays(){
isRunYear();
int totle=0;
for(int i=0;i totle=totle+months[i]; } totle=totle+this.day; return totle; } /* * 判断用户输入是否存在 */ public void isExits(){ if(month<0||month>12){ System.out.println("月份错误"); System.exit(0); } if(day<0||day>31){ System.out.println("日期错误"); System.exit(0); } } } 
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
