设计一个应用程序,通过类的方法来判断某年某月某日是当年的第几天(用java语言)

如题所述

//输入数据请按照标准输入 未作输入格式校验 2月份是否闰年 测试通过
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
class DateTest {
private int year;
private int month;
private int day;
public DateTest() {}
public DateTest(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
// strDate 格式为"yyyy-MM-dd"
public DateTest(String strDate) throws Exception {
this.year = strDate.indexOf(0, 3);
this.month = strDate.indexOf(5, 6);
this.day = strDate.indexOf(8, 9);
}
public int dayOfYear(String strDate) throws Exception {
String pattern = "yyyy-MM-dd";
Date date = new SimpleDateFormat(pattern).parse(strDate);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_YEAR);
}
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 class time_Date {
public static void main(String[] args) throws Exception {
DateTest test = new DateTest();
System.out.print("该日期是当年的第");
System.out.print(test.dayOfYear("2011-8-2")); //此处的日期为你要输入查询的日期
System.out.print("天!");
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答