How to add days into the date in android

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

    It works for me.

    Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);
    
    String CurrentDate = mYear + "/" + mMonth + "/" + mDay;
    String dateInString = dob; // Select date
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    c = Calendar.getInstance();
    try {
    c.setTime(sdf.parse(dateInString));
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    c.add(Calendar.DATE, 14);    //14 dats add
    sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date resultdate = new Date(c.getTimeInMillis());
    dateInString = sdf.format(resultdate);
    System.out.println(dateInString);
    
    0 讨论(0)
  • 2020-12-01 12:18
    Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);
    
        // display the current date
        String CurrentDate = mYear + "/" + mMonth + "/" + mDay;
    
        String dateInString = CurrentDate; // Start date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
        c = Calendar.getInstance();
    
        try {
            c.setTime(sdf.parse(dateInString));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        c.add(Calendar.DATE, 7656);//insert the number of days you want to be added to the current date
        sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date resultdate = new Date(c.getTimeInMillis());
        dateInString = sdf.format(resultdate);
    
        //Display the Result in the Edit Text or Text View your Choice
        EditText etDOR = (EditText)findViewById(R.id.etDateOfReturn);
        etDOR.setText(dateInString);
    
    0 讨论(0)
  • 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()); 
    
    0 讨论(0)
  • 2020-12-01 12:31

    Step-1 Get Calendar instance from the specified string

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Calendar c = Calendar.getInstance();
                c.setTime(sdf.parse(dateInString));
    

    Step-2 use add() to add number of days to calendar

    c.add(Calendar.DATE, 40); 
    

    Step-3 Convert the dtae to the resultant date format

    sdf = new SimpleDateFormat("MM/dd/yyyy");
                Date resultdate = new Date(c.getTimeInMillis());
                dateInString = sdf.format(resultdate);
    

    Source Code

    String dateInString = "2011-11-30";  // Start date
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(dateInString));
            c.add(Calendar.DATE, 40);  
            sdf = new SimpleDateFormat("MM/dd/yyyy");
            Date resultdate = new Date(c.getTimeInMillis());
            dateInString = sdf.format(resultdate);
            System.out.println("String date:"+dateInString);
    
    0 讨论(0)
  • 2020-12-01 12:32

    Don't use:

    Calendar c = Calendar.getInstance();
    

    Use this instead:

    GregorianCalendar calendar =new GregorianCalendar();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.DAY_OF_MONTH , 30);
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题