我也有这道题
public class Test9 {
public static void main (String[] args){
countDay();
}
public static void countDay() {
System.out.println("请输入一个日期:");
try{
String data1=new BufferedReader(new InputStreamReader(System.in)).readLine(); //输入字符串
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");//格式化日期格式
Date date2=df.parse(data1); //将字符data1转为日期格式
Calendar c=Calendar.getInstance();//新建日历类
c.setTime(date2);//设置日期
System.out.println("这是"+weekDay(c));
System.out.println("一年中的第"+c.get(Calendar.DAY_OF_YEAR)+"天");
}
catch(Exception e){
System.out.println("输入不正确,请重新输入:");
countDay();
}
}
public static String weekDay(Calendar c){//将Calendar类获得的日期值对应中文值
String [] week={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return week[c.get(Calendar.DAY_OF_WEEK)-1];
}
}
|