我问过你GregorianCalendar
,Calendar
有些人说这样Calendar
更好。所以我在Calender
课堂上做到了。
我对一种方法有疑问:JavaApplication1#whichIsEarlier(),
总是收到错误。请检查一下,如果可以的话。如何解决?我会感谢你的帮助。
public class JavaApplication1 {
public static void main(String[] args) {
Calendar yy =Calendar.getInstance();
Calendar xx= Calendar.getInstance();
xx.set(2001, 10,10);
Data dt = new Data(yy);
Data xt = new Data(xx);
Data dd = new Data( yy, xx);
System.out.println(dt.toString());
System.out.println(xt.toString());
System.out.println(dd.whichIsEarlier());
}
}
class Data {
private Calendar calendar;
Calendar cale;
Data(Calendar x){
calendar = x;
}
Data(Calendar x, Calendar y){
calendar = x;
cale = y;
}
public String toString(){
String months[] = {"Jan","Feb", "Mar" , "Apr", "Mai", "June", "July","Aug",
"Sep", "Okt", "Nov", "Dec"};
String str = "";
str = "Data: "+ calendar.get(Calendar.DATE) +" "+ months[calendar.get(Calendar.MONTH)]+
" "+ calendar.get(Calendar.YEAR);
return str;
}
public String whichIsEarlier(Calendar cale){
String str = "";
if(calendar.after(cale)) str = calendar.toString() ;
else str = cale.toString();
return str +" jest wczesniej";
}
}