您的位置:首页 > 其它

判断某天为当年第几天的demo

2017-05-10 20:17 239 查看
package com.zhp.java;

//用OOP 求出  某年某月某日是当年中的第几天
public class Day {
private int y;
private int m;
private int d;
int[] arr = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

public Day(int y, int m, int d) {
super();
this.y = y;
this.m = m;
this.d = d;
}

public Day() {
super();
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getM() {
return m;
}

public void setM(int m) {
this.m = m;
}

public int getD() {
return d;
}

public void setD(int d) {
this.d = d;
}

// 判断是否为闰年
public boolean runNian() {
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) {
return true;
} else {
return false;
}
}

// 改变闰年二月的天数
public void two() {
if (runNian()) {
arr[2] = 29;
}
}

@Override
public String toString() {
two();
int sum = 0;
for (int i = 1; i < m; i++) {
sum = sum + arr[i];
}
return y + "年" + m + "月" + d + "日是" + y + "年的" + (sum + d) + "天";
}

}


package com.zhp.java;

import java.util.Scanner;

public class DayDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
int y = sc.nextInt();
System.out.println("请输入月份");
int m = sc.nextInt();
System.out.println("请输入日期");
int d = sc.nextInt();
Day day = new Day(y, m, d);
System.out.println(day.toString());
}

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