androiddatetimedate-manipulation

calculate 3 months after the date - Android


I want to calculate 4 months after the date I pulled from the database. How can I do that. The output of history is as follows.

Wed Nov 27 14:42:23 GMT+03:00 2019

  JSONObject form_tarih2 = jObj.getJSONObject("form_tarih2");
        String date = form_tarih2.getString("date");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        Date calculateDate = sdf.parse(date);

Solution

  • Try this:

    JSONObject form_tarih2 = jObj.getJSONObject("form_tarih2");
    String date = form_tarih2.getString("date");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    Date calculateDate = sdf.parse(date);
    
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(calculateDate);
    calendar.add(Calendar.MONTH,4);
    

    You can add/sub days, months, year etc according to your need using Calendar