import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarDemo {
private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) throws ParseException {
System.out.println(dayOfYear("2013-03-12"));
}
public static int dayOfYear(String datetime) throws ParseException {
Date date = df.parse(datetime);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_YEAR);
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("DD");
System.out.println(sdf.format(date));
}
}
不用这么麻烦,有现成的方法
今天是2013.3.18
31+28+18 = 77
输入结果是:77天