I have a problem when it comes to obtaining a new date in the calendar after adding a few minutes to the current one.
I start getting the current date:
java.util.Date fechaActual = new java.util.Date();
By means of a function that I have created and that receives 4 parameters (Current date, days, hours and minutes), I want to calculate the new date. For this I do this:
Date nueva_fecha = sumarDiasAFecha(fechaActual, days, hours, minutes);
public static Date sumarDiasAFecha(Date fecha, int dias, int horas, int minutos){
if (dias==0 && horas==0 && minutos==0) {
return fecha;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(fecha);
//pasamos todo a minutos
int total_minutos = (dias*24*60) + (60*horas) + minutos;
calendar.add(Calendar.MINUTE, total_minutos);
return calendar.getTime();
}
Later I pass the date from Date to Calendar and work with it to get a date:
Calendar fecha_calendar = DateToCalendar(nueva_fecha);
int anyo = fecha_calendar.get(Calendar.YEAR);
int mes = fecha_calendar.get(Calendar.MONTH) + 1;
int dia = fecha_calendar.get(Calendar.DAY_OF_MONTH);
int hour = fecha_calendar.get(Calendar.HOUR);
int minute = fecha_calendar.get(Calendar.MINUTE);
String mes_fecha = "";
if (mes < 10) {
mes_fecha = "0"+mes;
} else {
mes_fecha = String.valueOf(mes);
}
String dia_fecha = "";
if (dia < 10) {
dia_fecha = "0"+dia;
} else {
dia_fecha = String.valueOf(dia);
}
String hour_fecha = "";
if (hour < 10) {
hour_fecha = "0"+hour;
} else {
hour_fecha = String.valueOf(hour);
}
String hour_minute = "";
if (minute < 10) {
hour_minute = "0"+minute;
} else {
hour_minute = String.valueOf(minute);
}
String fecha_total = String.valueOf(anyo) + "-" + mes_fecha + "-" + dia_fecha + " " + hour_fecha + ":" + hour_minute + ":00";
The code works for me the year and month, but the hours do not, and I do not understand why.