Java offers me different variables to subtract days from a date such as:
DAY_OF_MONTH
DAY_OF_WEEK
DAY_OF_WEEK_IN_MONTH
DAY_OF_YEAR
I have a method in which you subtracted days or months as I wish to a date, but I have doubts about which of them all to use for the days, and what is the difference between them, in the sense of how it could affect me in the operation, here I show you the method of which I speak to you
public Date SumaRestarFecha(Date fecha, int sumaresta, String opcion)
{
Calendar calendar = Calendar.getInstance();
try
{
calendar.setTime(fecha);
switch (opcion)
{
case "DAYS":
calendar.add(Calendar.DAY_OF_WEEK, sumaresta);
break;
case "MONTHS":
calendar.add(Calendar.MONTH, sumaresta);
break;
case "YEARS":
calendar.add(Calendar.YEAR, sumaresta);
break;
default:
System.out.println("parametro enviado al Switch no concuerda");
break;
}
}
catch(Exception e)
{
System.out.println("Error:\n" + e);
}
return calendar.getTime();
}
It is worth mentioning that if I want to add up, sumaresta
will be a number greater than zero, if I want to subtract it will be less than zero.
Is it okay to use DAY_OF_WEEK
for what I want? and if not, what would be the indicated and why?