How to add days into the date in android

前端 未结 10 1151
眼角桃花
眼角桃花 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:34

    This piece of code should do the job well!!!

    public static String addDay(String oldDate, int numberOfDays) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(dateFormat.parse(oldDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.add(Calendar.DAY_OF_YEAR,numberOfDays);
        dateFormat=new SimpleDateFormat("MM-dd-YYYY");
        Date newDate=new Date(c.getTimeInMillis());
        String resultDate=dateFormat.format(newDate);
        return resultDate;
    }
    

    For more functionality do please checkout this link
    Add days,months,years to a date

提交回复
热议问题