public class MainActivity extends AppCompatActivity {
private TextView et1, et2, et3, et4;
private int mYear, mMonth, mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(TextView)findViewById(R.id.tv_descarga);
et2=(TextView)findViewById(R.id.tv_proxdescarga);
et3=(TextView)findViewById(R.id.tv_falta);
et4=(TextView)findViewById(R.id.tv_actual);
et1.setText("");
et2.setText("");
et3.setText("");
et4.setText("");
}
public void inttroduce_fecha(View v) {
final Calendar dateIni = Calendar.getInstance();
mYear = dateIni.get(Calendar.YEAR);
mMonth = dateIni.get(Calendar.MONTH);
mDay = dateIni.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth){
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth));
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = format.format(calendar.getTime());
et1.setText("Descargada el " + dateString);
Calendar calendar1 = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth+28));
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
String dateString1 = format.format(calendar.getTime());
et2.setText("Descargar el " + dateString1);
Date d=new Date();
SimpleDateFormat fecc=new SimpleDateFormat("d, MMMM, yyyy");
String fechacComplString = fecc.format(d);
et4.setText("Hoy es " + fechacComplString);
// Se crean dos instancias de fechas
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
// Inicializar cada una de las instancias con la fecha requerida
cal1.set(mYear, mMonth, mDay);
cal2.set(year, monthOfYear, (dayOfMonth+28));
//cal2.set(mYear, mMonth, mDay);
// se obtiene el valor de la fecha en milisegundos
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();
// se calcula la diferencia de milisegundos entre ambas fechas
long diff = milis2 - milis1;
// Para calcular la diferencia en segundos
// Para calcular la diferencia en dias
long diffDays = diff / (24 * 60 * 60 * 1000);
et3.setText("Faltan : " + diffDays + " dias " );
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
}
public class MainActivity extends AppCompatActivity {
private TextView et1, et2, et3, et4;
private int mYear, mMonth, mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(TextView)findViewById(R.id.tv_descarga);
et2=(TextView)findViewById(R.id.tv_proxdescarga);
et3=(TextView)findViewById(R.id.tv_falta);
et4=(TextView)findViewById(R.id.tv_actual);
et1.setText("");
et2.setText("");
et3.setText("");
et4.setText("");
}
public void inttroduce_fecha(View v) {
final Calendar dateIni = Calendar.getInstance();
mYear = dateIni.get(Calendar.YEAR);
mMonth = dateIni.get(Calendar.MONTH);
mDay = dateIni.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth){
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth));
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = format.format(calendar.getTime());
et1.setText("Descargada el " + dateString);
Calendar calendar1 = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth+28));
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
String dateString1 = format.format(calendar.getTime());
et2.setText("Descargar el " + dateString1);
Date d=new Date();
SimpleDateFormat fecc=new SimpleDateFormat("d, MMMM, yyyy");
String fechacComplString = fecc.format(d);
et4.setText("Hoy es " + fechacComplString);
// Se crean dos instancias de fechas
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
// Inicializar cada una de las instancias con la fecha requerida
cal1.set(mYear, mMonth, mDay);
cal2.set(year, monthOfYear, (dayOfMonth+28));
//cal2.set(mYear, mMonth, mDay);
// se obtiene el valor de la fecha en milisegundos
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();
// se calcula la diferencia de milisegundos entre ambas fechas
long diff = milis2 - milis1;
// Para calcular la diferencia en segundos
// Para calcular la diferencia en dias
long diffDays = diff / (24 * 60 * 60 * 1000);
et3.setText("Faltan : " + diffDays + " dias " );
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
}
public class MainActivity extends AppCompatActivity {
private TextView et1, et2, et3, et4;
private int mYear, mMonth, mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(TextView)findViewById(R.id.tv_descarga);
et2=(TextView)findViewById(R.id.tv_proxdescarga);
et3=(TextView)findViewById(R.id.tv_falta);
et4=(TextView)findViewById(R.id.tv_actual);
et1.setText("");
et2.setText("");
et3.setText("");
et4.setText("");
}
public void inttroduce_fecha(View v) {
final Calendar dateIni = Calendar.getInstance();
mYear = dateIni.get(Calendar.YEAR);
mMonth = dateIni.get(Calendar.MONTH);
mDay = dateIni.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth){
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth));
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = format.format(calendar.getTime());
et1.setText("Descargada el " + dateString);
Calendar calendar1 = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth+28));
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
String dateString1 = format.format(calendar.getTime());
et2.setText("Descargar el " + dateString1);
Date d=new Date();
SimpleDateFormat fecc=new SimpleDateFormat("d, MMMM, yyyy");
String fechacComplString = fecc.format(d);
et4.setText("Hoy es " + fechacComplString);
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.set(mYear, mMonth, mDay);
cal2.set(year, monthOfYear, (dayOfMonth+28));
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();
long diff = milis2 - milis1;
long diffDays = diff / (24 * 60 * 60 * 1000);
et3.setText("Faltan : " + diffDays + " dias " );
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
}
Guardar fecha al cerrar aplicación
Me puede alguien decir como puedo meter el SharedPreferences,supongo que es la solucion a mi problema, para que al cerrar la aplicacion me guarde los datos y al abrirla de nuevo continuen ahi, hasta que los vuelva a cambiar?
public class MainActivity extends AppCompatActivity {
private TextView et1, et2, et3, et4;
private int mYear, mMonth, mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(TextView)findViewById(R.id.tv_descarga);
et2=(TextView)findViewById(R.id.tv_proxdescarga);
et3=(TextView)findViewById(R.id.tv_falta);
et4=(TextView)findViewById(R.id.tv_actual);
et1.setText("");
et2.setText("");
et3.setText("");
et4.setText("");
}
public void inttroduce_fecha(View v) {
final Calendar dateIni = Calendar.getInstance();
mYear = dateIni.get(Calendar.YEAR);
mMonth = dateIni.get(Calendar.MONTH);
mDay = dateIni.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth){
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth));
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = format.format(calendar.getTime());
et1.setText("Descargada el " + dateString);
Calendar calendar1 = Calendar.getInstance();
calendar.set(year, monthOfYear, (dayOfMonth+28));
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
String dateString1 = format.format(calendar.getTime());
et2.setText("Descargar el " + dateString1);
Date d=new Date();
SimpleDateFormat fecc=new SimpleDateFormat("d, MMMM, yyyy");
String fechacComplString = fecc.format(d);
et4.setText("Hoy es " + fechacComplString);
// Se crean dos instancias de fechas
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
// Inicializar cada una de las instancias con la fecha requerida
cal1.set(mYear, mMonth, mDay);
cal2.set(year, monthOfYear, (dayOfMonth+28));
//cal2.set(mYear, mMonth, mDay);
// se obtiene el valor de la fecha en milisegundos
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();
// se calcula la diferencia de milisegundos entre ambas fechas
long diff = milis2 - milis1;
// Para calcular la diferencia en segundos
// Para calcular la diferencia en dias
long diffDays = diff / (24 * 60 * 60 * 1000);
et3.setText("Faltan : " + diffDays + " dias " );
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
}
Gracias
default