2015-02-10 118 views
1

我已經實現了以下代碼來計算剩餘的剩餘天數。我使用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)); 
     }` 
+1

注意:'/ 24 * 60 * 60 * 1000'不會總是因爲每日節約時間(DST)的工作:http://en.wikipedia.org/wiki/Daylight_saving_time – 2015-02-10 16:27:12

+0

@ArnaudDenoyelle : 好。那麼,解決方案是什麼? – gogs09 2015-02-11 05:48:01

回答

1

的問題是在

user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR)); 

這將今天的日期的年份爲user_selected天線和月相等,將裏面去ELSEIF出去一樣,不做任何更改。

另請檢查您提到的if else條件。

0

不要自己進行計算。獲得日曆對象後,使用「添加」方法增加所需的值。

1

您還可以使用Java 8中的LocalDate類來隨時添加/減去日期。

java.time提供Period類來表示的時間長度,如 數天,或小時和分鐘的。 LocalDate和朋友報價 plus()minus()方法來添加或減去期間或其他 時間相關的對象。期限提供工廠方法,如ofDays()

閱讀本示例以供參考。它計算一下日期將是700天從現在開始:

/** 
* DateAdd -- compute the difference between two dates (e.g., today and 700 days 
* from now). 
*/ 
public class Main { 
    public static void main(String[] av) { 
     /** Today's date */ 
     LocalDate now = LocalDate.now(); 
     Period p = Period.ofDays(700); 
     LocalDate then = now.plus(p); 
     System.out.printf("Seven hundred days from %s is %s%n", now, then); 
    } 
} 

運行此程序報告當前的日期和時間,什麼日期和時間將是700天從現在開始。例如:

Seven hundred days from 2013-11-09 is 2015-10-10