Check if day x falls on a Saturday or Sunday

Asked

Viewed 5,402 times

3

I’m making a system that will be an Agenda, in this system has an option to repeat the commitment unless fortnightly, ie I save the commitment and the system will notify me every 15 days. But this commitment must not fall into sábado or domingo. How can I make this check?

For example as it is fortnightly if I add an appointment today it will save today and increment another 15 days if the result falls on a Saturday or Sunday it jumps and saves the commitment on Monday. That is, to check if the day of the week is Saturday or Sunday I have to spend a data(data futura) as parameter and on top of that date do the check

  • Have you ever worked with Legend? it has DAY_OF_MONTH you could subtract from your current day or do by week DAY_OF_WEEK -2 that would be the 6th and 7th of the week.

  • Are you using Java 8? If so, there is a Localdate class that has a method called getDayOfWeek, through it you can validate which is the day of the week. If you are working with Java 7 you can use the @Wellingtonavelino tip.

  • All right, I’ll try to use both and see which one fits better. But it is possible with these classes to find out what is the day of the week of the next month for example?

  • edited the question to explain a little better what I’m trying to do

2 answers

5


You can use Calendar even.

Demo (obviously half of the code can be removed as it is System.out.println...and also use SWITCH, finally...is only 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 (04/07/2015)

inserir a descrição da imagem aqui

Sunday (05/07/2015)

inserir a descrição da imagem aqui

Weekday (06/07/2015)

inserir a descrição da imagem aqui

  • 2

    haha, my idea was exactly that, but I don’t have time to put examples :(

  • Thank you very much, I implemented your idea in my system and it worked perfectly.

  • Imagine Techies ! (goes @Wellingtonavelino kkkk)

3

The key is to use the u in the pattern of 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 you schedule for 1 day you will fall in: Monday, July 13 2015

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

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

If you schedule for 4 days from now it will fall on: Tuesday, July 14 2015

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

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

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

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

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

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

If scheduled for 11 days from now it will fall on: Tuesday, July 21 2015

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

If scheduled for 13 days from now it will fall on: Thursday, July 23rd 2015

If you schedule for 14 days from now it will fall on: Friday, July 24th 2015

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

  • Thank you for the reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.