I want to make a function for my android app, where i pass 3 variables
- From (string representing a date)
- To (string representing a date)
- unit (integer)
What the result should be, is the difference of these two date in weeks, or days or years depending on unit. This is my code:
 private int datesDifference(String from, String to, int unit){
   Calendar from_date = Calendar.getInstance();
   Calendar to_date = Calendar.getInstance();
   DebugLogger.debug("From date = "+from+" to date = "+to);
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd", Locale.getDefault());
   try {
       from_date.setTime(sdf.parse(from));
       to_date.setTime(sdf.parse(to));
   } catch (ParseException e) {
       e.printStackTrace();
   }
   int from_year = from_date.get(Calendar.YEAR);
   int from_month = from_date.get(Calendar.MONTH)+1;
   int from_day = from_date.get(Calendar.DAY_OF_MONTH);
   DebugLogger.debug("from_year "+from_year+" from month "+from_month+" from day "+from_day);
   int to_year = to_date.get(Calendar.YEAR);
   int to_month = to_date.get(Calendar.MONTH)+1;
   int to_day = to_date.get(Calendar.DAY_OF_MONTH);
   DebugLogger.debug("to_year "+to_year+" to month "+to_month+" to day "+to_day);
   from_date.set(from_year, from_month, from_day);//set begin_of_goal_date
   to_date.set(to_year, to_month, to_day);//set now_date
   if(unit==0){
       //TODO unit for days difference
       return 0;
   }else if(unit==1){
       //TODO unit for week difference
       long milliseconds1 = from_date.getTimeInMillis();
       long milliseconds2 = to_date.getTimeInMillis();
       long diff = milliseconds2 - milliseconds1;
       long diffWeeks = diff / (7*24 * 60 * 60 * 1000);
       int weeks = (int) diffWeeks;
       DebugLogger.debug("Difference in weeks "+weeks);
       return weeks;
   }else{
       //TODO unit for years difference
       return 0;
   }
}
The above code is not working correctly, as it does not take date from and to as calendar object but instead gets a from_date and to_date equal today! What is wrong with my code?
UPDATE
this is the data i pass in my function
    String begin_of_goal_date="2016-01-20";//when we changed the goal
    String now_date="2016-04-18";//now
    int weeks = datesDifference(begin_of_goal_date,now_date,1);
 
     
     
    