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
- Convertir cadena en formato de fecha
- Forzar Android DateUtils.getRelativeDateTimeString () para ignorar la configuración regional del dispositivo?
- Cómo formatear una fecha de calendario
- Consulta Sqlite para la clasificación de fechas
- Convertir la fecha en milisegundos está dando resultados inconsistentes en Android (java)
Gracias
- ¿Cómo puedo calcular la edad en Java con precisión? Fecha de nacimiento
- SQLite selecciona todos los registros de hoy y día anterior
- Obtener la fecha de instalación de la aplicación en Android
- Notificación de Android en una fecha específica
- ¿Cuál es la diferencia entre android.text.format.DateFormat y java.text.DateFormat
- ¿Los hilos son lo suficientemente fiables para calcular segundos?
- Compare las fechas (almacenadas como cadena) en la base de datos android sqlite?
- Consiga tiempo real - no dispositivo fije el tiempo en androide
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);
- ¿Cómo ignorar el error "evitar constructores no predeterminados en fragmentos"?
- Compruebe si la tarjeta SD está presente, boolean siempre es cierto