Cómo añadir días a la fecha en android

En mi base de datos estoy recibiendo la fecha de inicio como fecha de la fecha de 2011-11-30 (aaaa / mm / dd) y la duración como 40 días. ¿Cómo puedo calcular los días y obtener un nuevo formato de fecha de mm / dd / aaaa.

Alguien puede ayudarme

Gracias

Usted puede intentar algo como esto,

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

Paso-1 Obtener instancia de Calendar de la cadena especificada

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

Paso-2 use add() para agregar el número de días al calendario

 c.add(Calendar.DATE, 40); 

Paso 3 Convierte el dtae al formato de fecha resultante

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

Código fuente

 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); 
 Date dtStartDate=o.getStartDate(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Calendar c = Calendar.getInstance(); c.setTime(dtStartDate); c.add(Calendar.DATE, 3); // number of days to add String dt = sdf.format(c.getTime()); // dt is now the new date Toast.makeText(this, "" + dt, 5000).show(); 
 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); 

Este código funciona 100%

  Calendar c = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss"); String reg_date = df.format(c.getTime()); showtoast("Currrent Date Time : "+reg_date); c.add(Calendar.DATE, 3); // number of days to add String end_date = df.format(c.getTime()); showtoast("end Time : "+end_date); 

Simple Copia sencilla de este método pasar datos

 public static String ConvertToDate(String dateString,String inputFormater ,String outputFormater) { String outputText = "" ; try { SimpleDateFormat inputFormat = new SimpleDateFormat(inputFormater); SimpleDateFormat outputFormat = new SimpleDateFormat(outputFormater); Date parsed = null; parsed = inputFormat.parse(dateString); outputText = outputFormat.format(parsed); Log.d(TAG, " msg : " + outputText); } catch (ParseException e) { e.printStackTrace(); Log.e(TAG, "ERROR : " + e.getMessage()); } return outputText; } 

Y lo llaman como

  public static String INPUTE_JSON_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static String FORMAT_FOR_JOB_APPLIED = "MMM dd, yyyy"; String date ="2017-10-29"; ConvertToDate(date ,DateUtils.INPUTE_JSON_DATE_FORMAT ,DateUtils.FORMAT_FOR_JOB_APPLIED); 

Puedes poner cualquier formato que quieras en tu cituación

 ConvertToDate("2011-11-30","yyyy/mm/dd" ,"mm/dd/yyyy"); 
 it's complete work 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); 
  • Android / Java - Fecha Diferencia en días
  • Java último día del problema del mes
  • ¿Cuál es el formato de hora para esta "fecha": "2014-08-20 00:00:00 -0500"?
  • android obtener la fecha anterior de una fecha correspondiente (no la fecha de ayer)
  • ¿Por qué el año en curso en la fecha guardada como 3912?
  • Android - SQLite - SELECCIONAR ENTRE Fecha1 Y Fecha2
  • ¿Por qué "Date d = new Date ();" devuelve un error?
  • Comprobación de Android de la fecha cambiada
  • Diferencia de Android entre dos fechas
  • Guardando y recuperando la fecha en Firebase
  • JAVA - ¿Es un error en la clase java.util.Calendar o qué?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.