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
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.
– Wellington Avelino
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.
– adelmo00
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?
– DiegoAugusto
edited the question to explain a little better what I’m trying to do
– DiegoAugusto