Good morning,
I can not find a way to add minutes to the current date and give me a new date. For example if you add 48 hours you would have to change the day too, but the code I have does not. I am working with the following:
java.util.Date fechaActual = new java.util.Date();
try {
String quedan = doc.select(".c-price .text-muted").first().text();
String[] parts = quedan.split("•");
String remaining = parts[1].replaceAll("\u00a0", "");
String[] palabras = remaining.split(" ");
boolean hay_dia = false;
boolean hay_horas = false;
boolean hay_minutos = false;
for (String palabra : palabras) {
if (palabra.equals("day")) {
hay_dia = true;
}
if (palabra.equals("hour")) {
hay_horas = true;
}
if (palabra.equals("minute")) {
hay_minutos = true;
}
}
int days = 0;
int hours = 0;
int minutes = 0;
if (hay_dia && hay_horas && hay_minutos) {
String[] dias = remaining.split("day");
days = Integer.parseInt(dias[0]);
String[] horas = dias[1].split("hour");
hours = Integer.parseInt(horas[0]);
String[] minutos = horas[1].split("minute");
minutes = Integer.parseInt(minutos[0]);
}
if (hay_dia == false && hay_horas && hay_minutos) {
String[] horas = remaining.split("hour");
hours = Integer.parseInt(horas[0]);
String[] minutos = horas[1].split("minute");
minutes = Integer.parseInt(minutos[0]);
}
if (hay_dia == false && hay_horas == false && hay_minutos) {
String[] minutos = remaining.split("minute");
minutes = Integer.parseInt(minutos[0]);
}
if (hay_dia && hay_horas == false && hay_minutos == false) {
String[] dias = remaining.split("day");
days = Integer.parseInt(dias[0]);
}
if (hay_dia && hay_horas && hay_minutos == false) {
String[] dias = remaining.split("day");
days = Integer.parseInt(dias[0]);
String[] horas = dias[1].split("hour");
hours = Integer.parseInt(horas[0]);
}
Date nueva_fecha = sumarDiasAFecha(fechaActual, days, hours, minutes);
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";
item.getValues().put(Constants.FECHA_SPOTLIGHT, fecha_total);
} catch (ArrayIndexOutOfBoundsException ex){
//SI LA OFERTA ES INDEFINIDA ENTONCES HAY QUE PONER FECHA NULA
item.getValues().put(Constants.FECHA_SPOTLIGHT, "0000-00-00 00:00:00");
}
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.DAY_OF_YEAR, dias);
calendar.add(Calendar.MINUTE, total_minutos);
return calendar.getTime();
}
public static Calendar DateToCalendar(Date date ) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
I am trying to add, for example, 12 hours 48 minutes to the current date and return 2017-05-22 01:11:00
, at least the day should be 23 instead of 22 (Spanish time).