javaandroiddatetime-formatandroid-calendar

Android calendar object from string


I want to make a function for my android app, where i pass 3 variables

  1. From (string representing a date)
  2. To (string representing a date)
  3. 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);

Solution

  • If data example is in this format 2016-01-20

    Change SimpleDateFormat format from yyyy-MMM-dd to yyyy-MM-dd:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    

    With this call:

    datesDifference("2016-01-01", "2016-02-01", 1);
    

    OUTPUT

    From date = 2016-01-01 to date = 2016-02-01
    from_year 2016 from month 1 from day 1
    to_year 2016 to month 2 to day 1
    Difference in weeks 4
    

    With your example:

    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);
    

    OUTPUT:

    From date = 2016-01-20 to date = 2016-04-18
    from_year 2016 from month 1 from day 20
    to_year 2016 to month 4 to day 18
    Difference in weeks 12