我已經實現了以下代碼來計算剩餘的剩餘天數。我使用SimpleDateFormat將字符串轉換爲日期。如果我輸入10/02/1993(今天的日期)作爲日期,則輸出顯示剩餘0天。但是如果我選擇11/02/1993,它顯示的是0天后的相同輸出。但是如果我改變這個月,即22/07/1993,它顯示了160天而不是162天,這是最初的結果。我在這裏做錯了什麼?任何幫助是極大的讚賞。在java中剩餘天數
private static void calculateNextBirthday() {
Scanner in = new Scanner(System.in);
System.out.println("Enter your birthday(dd/MM/yyyy)");
user_input = in.nextLine();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar user_selected_day = new GregorianCalendar();
Calendar today = new GregorianCalendar();
Date selected_date = null;
try {
selected_date = formatter.parse(user_input);
} catch (ParseException e) {
e.printStackTrace();
}
user_selected_day.setTime(selected_date);
int user_selected_month = user_selected_day.get(Calendar.MONTH);
int CMonth = today.get(Calendar.MONTH);
int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
int CDate = today.get(Calendar.DAY_OF_MONTH);
user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));
user_selected_day.set(Calendar.DAY_OF_WEEK,today.get(Calendar.DAY_OF_WEEK));
if (user_selected_month < CMonth) {
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
else if (user_selected_month == CMonth){
if(user_selected_date < CDate){
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
}
long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
long days;
days = (millis/(24 * 60 * 60 * 1000));
System.out.println(String.valueOf(days) + " days left!!");
}
更新:我已經更新了下面的代碼,它工作正常。希望它能幫助未來的人。
` SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar user_selected_day = new GregorianCalendar();
Calendar today = new GregorianCalendar();
Date selected_date = null;
try {
selected_date = formatter.parse(plainDate);
} catch (ParseException e) {
e.printStackTrace();
}
user_selected_day.setTime(selected_date);
int user_selected_month = user_selected_day.get(Calendar.MONTH);
int CMonth = today.get(Calendar.MONTH);
int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
int CDate = today.get(Calendar.DAY_OF_MONTH);
user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));
user_selected_day.set(Calendar.HOUR_OF_DAY,today.get(Calendar.HOUR_OF_DAY));
user_selected_day.set(Calendar.MINUTE,today.get(Calendar.MINUTE));
user_selected_day.set(Calendar.SECOND,today.get(Calendar.SECOND));
user_selected_day.set(Calendar.MILLISECOND,today.get(Calendar.MILLISECOND));
if (user_selected_month < CMonth) {
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
else if (user_selected_month == CMonth){
if(user_selected_date < CDate){
user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
}
}
long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
long days;
if(user_selected_day.getTime()==today.getTime()){
days = 0;
}
else{
days = (millis/(24 * 60 * 60 * 1000));
}`
注意:'/ 24 * 60 * 60 * 1000'不會總是因爲每日節約時間(DST)的工作:http://en.wikipedia.org/wiki/Daylight_saving_time – 2015-02-10 16:27:12
@ArnaudDenoyelle : 好。那麼,解決方案是什麼? – gogs09 2015-02-11 05:48:01