Pregunta

Sir I have a date like "08-25-2013" as a string, I got the number of days from this string date, now I add few number days in it like long days=days+250; now the question is how can I get the date like "05-02-2014" as a string. Please answer me as soon as possible please thanks in advance respected sir. Qustions: number of days convert to the date.

¿Fue útil?

Solución

Do this Way..

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

Otros consejos

Use this code

            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Date dateparsed = sdf.parse("29-08-2013");
        Calendar c = Calendar.getInstance();
        c.setTime(dateparsed); 
        c.add(Calendar.DATE, 250); // Adding n number of days 
        String output = sdf.format(c.getTime());
        System.out.println(output);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top