How to add days into the date in android

前端 未结 10 1134
眼角桃花
眼角桃花 2020-12-01 12:07

In my database I am getting start date like 2011-11-30(yyyy/mm/dd)format.and duration date like 40 days.How can i calculate the days and get new date format of mm/dd/yyyy.

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 12:20

    You can try something like this,

    String dt = "2012-01-04";  // Start date
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    try {
        c.setTime(sdf.parse(dt));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    c.add(Calendar.DATE, 40);  // number of days to add, can also use Calendar.DAY_OF_MONTH in place of Calendar.DATE
    SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy");
    String output = sdf1.format(c.getTime()); 
    

提交回复
热议问题