Skip to content

Instantly share code, notes, and snippets.

@klebergraciasoares
Forked from alexandreaquiles/DiasUteis.java
Last active August 29, 2015 14:22
Show Gist options
  • Save klebergraciasoares/32d3f5bb7d885d3d6d1f to your computer and use it in GitHub Desktop.
Save klebergraciasoares/32d3f5bb7d885d3d6d1f to your computer and use it in GitHub Desktop.
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DiasUteis {
public static Map<YearMonth, List<LocalDate>> doAno(int ano){
return diasUteisDosMesesDoAno(mesesDoAno(ano));
}
public static List<LocalDate> doMes(int ano, Month mes) {
return doMes(YearMonth.of(ano, mes));
}
public static List<LocalDate> doMes(YearMonth mes) {
return diasUteisDosMesesDoAno(Stream.of(mes)).get(mes);
}
public static Map<YearMonth, List<LocalDate>> dosMeses(List<YearMonth> meses) {
return diasUteisDosMesesDoAno(meses.stream());
}
private static Stream<YearMonth> mesesDoAno(int ano) {
return Stream.of(Month.values()).map(month -> YearMonth.of(ano, month));
}
private static Map<YearMonth, List<LocalDate>> diasUteisDosMesesDoAno(Stream<YearMonth> mesesDoAno){
return mesesDoAno
.collect(Collectors.toMap(Function.identity(),
ym -> todosOsDiasDoMesAno(ym)
.filter(diaUtil())
.collect(Collectors.toList())));
}
private static Predicate<LocalDate> diaUtil() {
return date -> !date.getDayOfWeek().equals(DayOfWeek.SATURDAY) && !date.getDayOfWeek().equals(DayOfWeek.SUNDAY);
}
private static Stream<LocalDate> todosOsDiasDoMesAno(YearMonth yearMonth) {
return Stream.iterate(yearMonth.atDay(1), date -> date.plusDays(1)).limit(yearMonth.lengthOfMonth());
}
}
import java.time.Month;
import java.time.YearMonth;
import java.util.Arrays;
import java.util.List;
public class ProgramaDiasUteis {
public static void main(String[] args) {
System.out.println(DiasUteis.doAno(2014));
System.out.println(DiasUteis.doMes(2014, Month.APRIL));
System.out.println(DiasUteis.doMes(YearMonth.of(2014, Month.APRIL)));
List<YearMonth> abrilEMaioDe2014 = Arrays.asList(YearMonth.of(2014, Month.APRIL), YearMonth.of(2014, Month.MAY));
System.out.println(DiasUteis.dosMeses(abrilEMaioDe2014));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment