Last active
October 11, 2021 00:31
-
-
Save alexandreaquiles/10300153 to your computer and use it in GitHub Desktop.
Passado um ano ou um mês ou alguns meses de um ano, gera um mapa (Map) dos meses (YearMonth) com a lista de datas (LocalDate) que são dias úteis (segunda a sexta).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
ola gente que maravilha de código... alguém poderia me ajudar numa duvida....
Como eu poderia por exemplo dividir um valor de X em 3 semanas usando a nova api de datas..
tenho um sistema de vendas em que o vendedor vende um valor X divide pro cliente dele em 3 ou 4 ou 6 semanas....
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dá pra fazer: