你腳下的問題是日期到了什麼格式。
你只有提供的JTextArea
爲他們進入的價值,這樣他們就可以進入任何事情......
所以,首先第一件事情,你需要通過它可以接受傳入的方法值...
public String toDayOfWeek(String date) {
}
從這裏你需要輸入值格式爲Date
String dayOfWeek = null;
try {
Date date = DateFormat.getDateInstance().parse(date);
} catch (ParseException exp) {
dayOfWeek = date + " is an invalid date format";
}
return dayOfWeek;
(顯然,上述所屬的toDayOfWeek(String)
法)
現在,就個人而言,我會通過Date
價值到另一種方法,但這導致我瘋了...
public String toDayOfWeek(Date date) {
// Now you could extract the various values from the Date object
// but those methods are deprecated...
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH); // Months are 0 based
int year = cal.get(Calendar.YEAR);
// Your calculation here...
return yourDayCalculation;
}
但說實話,它會更簡單,更容易做到這一點...
public String toDayOfWeek(Date date) {
// Now you could extract the various values from the Date object
// but those methods are deprecated...
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return DateFormatSymbols.getInstance().getWeekdays()[cal.get(Calendar.DAY_OF_WEEK)];
}
所以,你最終會得到兩種方法...
public String toDayOfWeek(String date) {
String dayOfWeek = null;
try {
Date date = DateFormat.getDateInstance().parse(date);
dayOfWeek = toDayOfWeek(date);
} catch (ParseException exp) {
dayOfWeek = date + " is an invalid date format";
}
return dayOfWeek;
}
public String toDayOfWeek(Date date) {
// Now you could extract the various values from the Date object
// but those methods are deprecated...
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return DateFormatSymbols.getInstance().getWeekdays()[cal.get(Calendar.DAY_OF_WEEK)];
}
這是一個相當複雜的要求,有的一隻有一個星期的經驗。我將首先閱讀[使用JFC/Swing創建GUI](http://docs.oracle.com/javase/tutorial/uiswing/index.html) – MadProgrammer
@RazorShadow您可以使用'JFormattedTextField'獲取日期值。這是[示例](http://stackoverflow.com/q/4252257/1048330)。 – tenorsax