Subtract days correctly

0

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?

    
asked by Villatoro 10.08.2017 в 06:02
source

2 answers

1

for what you are commenting the option would be DAY_OF_MONTH because DAY_OF_WEEK indicates that day of the week that is Monday, Tuesday, Wednesday ... and if you add or subtract it will indicate in what day of the week you stay but it will not be useful because you will not have the reference for the date.

    
answered by 10.08.2017 / 12:31
source
2

The appropriate thing, java8, would be if you used the classes in the java.time package

For example:

    public Date SumaRestarFecha(Date fecha, int sumaresta, String opcion){
        LocalDate date = fecha.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        //Con Java9
        //LocalDate date = LocalDate.ofInstant(input.toInstant(), ZoneId.systemDefault());
        TemporalUnit unidadTemporal;
        switch(opcion){
            case "DAYS":
                unidadTemporal = ChronoUnit.DAYS;
                break;
            case "MONTHS":
                unidadTemporal = ChronoUnit.MONTHS;
                break;
            case "YEARS":
                unidadTemporal = ChronoUnit.YEARS;
                break;
            default:
                //Controlar error
        }
        LocalDate dateResultado = date.minus(sumaresta, unidadTemporal);
        return Date.from(dateResultado.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
    
answered by 10.08.2017 в 08:45