第1个回答 推荐于2016-01-18
package test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public 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 static void main(String[] args) throws Exception {
DateTest test = new DateTest();
System.out.println(test.dayOfYear("2008-12-23"));
}
}本回答被提问者采纳