文章目录
package Remain;
import java.util.Scanner;
public class Demo07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入日期,格式:2018-12-11");
String date = sc.nextLine();
Time time = new Time(date);
System.out.println(time);
}
}
class Time {
String date;
String[] dateArray;
int year;
int month;
int day;
int allDay = 365;
public Time(String date) {
this.date = date;
this.dateArray = date.trim().split("-");
this.year = Integer.parseInt(dateArray[0]);
this.month = Integer.parseInt(dateArray[1]);
this.day = Integer.parseInt(dateArray[2]);
}
public int elapsedTime() {
int elapsedTime = day;
for (int i = 1; i < this.month; i++) {
elapsedTime = elapsedTime + month(this.year, i);
}
return elapsedTime;
}
public int daysLeft() {
int daysLeft = allDay - elapsedTime();
return daysLeft;
}
public boolean format() {
boolean judge = true;
if ((date.length() != 10) && (date.charAt(4) != '-') && (date.charAt(7) != '-')) {
System.out.println("您输入的日期有误");
judge = false;
}
return judge;
}
public boolean year(int year) {
boolean judge = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
allDay = 366;
judge = true;
}
return judge;
}
public int month(int year, int month) {
int day = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if (year(year)) {
day = 29;
} else {
day = 28;
}
break;
}
return day;
}
public int day(int year, int month, int day) {
int daysLeft = month(year, month) - day;
return daysLeft;
}
public String toString() {
String time = null;
if (month >= 6) {
time = year + "年已过去了" + elapsedTime() + "天,还剩下" + daysLeft() + "天,在剩下的日子里,你要继续加油哦!";
} else {
time = year + "年已过去了" + elapsedTime() + "天,还剩下" + daysLeft() + "天,新的一年才刚刚开始,你要加油哦!";
}
return time;
}
}
