title: Remaining days calculation in java
body: <p>I have implemented the following code to calculate the remaining days left. I have used SimpleDateFormat to convert string to date. If I enter 10/02/1993 (today's date) as the date, the output shows 0 days left. But if I choose 11/02/1993, it shows the same output as 0 days left. But if I change the month i.e. 22/07/1993, it shows 160 days left instead of 162 days, which is the original result. What am I doing wrong here ? Any help is greatly appreciated. </p>
  
  <pre><code>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 &lt; CMonth) {
              user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
          }
          else if (user_selected_month == CMonth){
              if(user_selected_date &lt; 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!!");
      }
  </code></pre>
  
  <blockquote>
    <p>UPDATE : I have updated the Code below and it works fine. Hope it helps someone in the future. </p>
  </blockquote>
  
  <pre><code>`       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 &lt; CMonth) {
              user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
          }
          else if (user_selected_month == CMonth){
              if(user_selected_date &lt; 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));
          }`
  </code></pre>