Check if day x falls on a Saturday or Sunday

2

I am making a system that will be an Agenda, in this system you have an option to repeat the appointment except fortnightly, that is, I save the appointment and the system will notify me every 15 days. But this commitment can not fall into sábado or domingo . How can I do this verification?

For example, since it's biweekly if I add an appointment today it will save today and increase by 15 more days if the result falls on a Saturday or Sunday it jumps and saves the appointment on Monday. That is, to check if the day of the week is Saturday or Sunday I have to pass a data(data futura) as a parameter and on that date to do the verification

    
asked by anonymous 10.07.2015 / 16:36

2 answers

4

You can use Calendar same.

Demo (obviously, half of the code can be removed because it is System.out.println ... and also use SWITCH , anyway ... it's just for demonstration purposes)

Main.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main
{
    public static void main(String[] args)
    {
        try
        {
            Calendar data = Calendar.getInstance();
            //data.set(2015, Calendar.JULY, 5); // ou neste neste formato ao invés de usar o simpleFormat abaixo
            SimpleDateFormat simpleFormat = new SimpleDateFormat("dd/MM/yyyy");

            data.setTime(simpleFormat.parse("06/07/2015"));

            System.out.println("Data antes: " + simpleFormat.format(data.getTime()));
            data = checaFDS(data);
            System.out.println("Data apos: " + simpleFormat.format(data.getTime()));
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Verifica se data á sábado ou domingo e acrescenta dias conforme necessário p/ retornar dia de semana.
     *
     * @param   data        Um objeto Calendar
     * @return  Calendar
     */
    public static Calendar checaFDS(Calendar data)
    {
        // se for domingo
        if (data.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
        {
            data.add(Calendar.DATE, 1);
            System.out.println("Eh domingo, mudando data para +1 dias");
        }
        // se for sábado
        else if (data.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
        {
            data.add(Calendar.DATE, 2);
            System.out.println("Eh sabado, mudando data para +2 dias");
        }
        else
        {
            System.out.println("Eh dia de semana, mantem data");
        }
        return data;
    }

}

Saturday (07/04/2015)

Sunday(07/05/2015)

Weekday (06/07/2015)

    
10.07.2015 / 17:32
3

The secret is to use u in the default SimpleDateFormat , as in the code:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ProximoDiaDaSemana {

    private static DateFormat DIA_DA_SEMANA = new SimpleDateFormat("u");

    private static long MILISEGUNDOS_EM_UM_DIA = 24 * 60 * 60 * 1000;

    private static int DOMINGO = 7;

    private static int SABADO = 6;

    private static int diferencaSabadoOuDomingo(int diaDaSemana) {
        if (diaDaSemana == DOMINGO) {
            return 1;
        }
        if (diaDaSemana == SABADO) {
            return 2;
        }
        return 0;
    }

    private static Date obterDataProximoCompromisso(Date dataInicial, int dias) {
        Date daquiXDias = new Date(dataInicial.getTime() + dias * MILISEGUNDOS_EM_UM_DIA);
        int diferencaSabadoOuDomingo = diferencaSabadoOuDomingo(Integer.valueOf(DIA_DA_SEMANA.format(daquiXDias)));
        if (diferencaSabadoOuDomingo > 0) {
            return new Date(daquiXDias.getTime() + diferencaSabadoOuDomingo * MILISEGUNDOS_EM_UM_DIA);
        } else {
            return daquiXDias;
        }
    }
}

So when we run a test:

    private static DateFormat EXIBICAO = DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault());

    public static void main(String[] args) {
        Locale.setDefault(new Locale("pt-BR"));
        Date hoje = new Date();
        System.out.println("Hoje: " + EXIBICAO.format(hoje));
        for (int i = 1; i <= 15; i++) {
            System.out.println("Se agendar para daqui " + i + " dias cairá em: "
                    + EXIBICAO.format(ProximoDiaDaSemana.obterDataProximoCompromisso(new Date(), i)));
        }
    }

We have the desired result:

  

Today: Friday, July 10, 2015

     

If scheduled for 1 day will fall on: Monday, July 13,   2015

     

If scheduled for 2 days, it will fall on: Monday, July 13,   2015

     

If scheduled for 3 days will fall on: Monday, July 13,   2015

     

If scheduled for 4 days will fall on: Tuesday, July 14,   2015

     

If scheduled for 5 days will fall on: Wednesday, July 15,   2015

     

If scheduled for 6 days will fall on: Thursday, July 16,   2015

     

If scheduled for 7 days will fall on: Friday, July 17,   2015

     

If scheduled for 8 days will fall on: Monday, July 20,   2015

     

If scheduled for 9 days will fall on: Monday, July 20,   2015

     

If scheduled for 10 days will fall on: Monday, July 20,   2015

     

If you schedule for here 11 days will fall on: Tuesday, July 21,   2015

     

If scheduled for 12 days from now, it will fall on: Wednesday, July 22,   2015

     

If scheduled for 13 days will fall on: Thursday, July 23,   2015

     

If scheduled for 14 days, it will fall on: Friday, July 24,   2015

     

If scheduled for 15 days, it will fall on: Monday, July 27,   2015

    
10.07.2015 / 17:33