obtener el día anterior / siguiente Fecha al presionar el botón después de obtener la fecha de la actividad anterior usando SharedPreferences android
En mi aplicación estoy usandoesta biblioteca para vista de calendario personalizado. Al hacer clic en cualquier fecha, pasaré esa fecha a la siguiente actividad a través de preferencias compartidas. Puedo mostrar la fecha en la próxima actividad. Ahora lo que quiero es que haya dos botones, uno es "Anterior" y otro "Siguiente". Si hago clic en cualquier botón, la fecha tiene que cambiar según el requisito.
El código de fecha de clic es
calendarView.setOnDateClickListener(new CalendarView.OnDateClickListener() {
@Override
public void onDateClick(@NonNull Date selectedDate) {
SimpleDateFormat df_new = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
String newselectedDate=df_new.format(selectedDate);
Toast.makeText(getApplicationContext(), "You selected" + newselectedDate + " : Date", Toast.LENGTH_SHORT).show();
// SharedPreferences for sharing the Date with Next Activity, Added by Tara
SimpleDateFormat s_day=new SimpleDateFormat("dd",Locale.getDefault()); String sdd=s_day.format(selectedDate);
SimpleDateFormat s_month=new SimpleDateFormat("MM",Locale.getDefault());String smm=s_month.format(selectedDate);
SimpleDateFormat s_year=new SimpleDateFormat("yyyy",Locale.getDefault());String syy=s_year.format(selectedDate);
SharedPreferences spf= getSharedPreferences("myprfs", Context.MODE_PRIVATE);
SharedPreferences.Editor spe = spf.edit();
// spe.putString("sdate", String.valueOf(selectedDate));
spe.putString("sday", String.valueOf(sdd));
spe.putString("smonth", String.valueOf(smm));
spe.putString("syear", String.valueOf(syy));
Log.d("Day is", String.valueOf(sdd));
Log.d("Month is", String.valueOf(smm));
Log.d("Year is", String.valueOf(syy));
spe.commit();
Intent i_available_slot=new Intent(BookAnAppoinment.this, ChangeDate.class);
startActivity(i_available_slot);
}
});
y la siguiente actividad donde estoy recuperando la fecha a través de la preferencia compartida es
SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE);
String id1 = spf.getString("sday", "no value");
String id2 = spf.getString("smonth", "no value");
String id3 = spf.getString("syear", "no value");
String sdate=id1+"-"+id2+"-"+id3;
// d.setText(sdate);
final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
final String formattedDate = df.format(sdate);
d.setText(formattedDate);
try {
Date date=df.parse(sdate);
Calendar cal = df.getCalendar();
} catch (ParseException e) {
e.printStackTrace();
}
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
p.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Escribí el bloque try / catch pero no pude codificar según mi requisito. Por favor, ayúdame.