1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class DateUtils {
public static LocalDate getFirstDayOfMonth(LocalDate date) {
return LocalDate.of(date.getYear(), date.getMonth(), 1);
}
public static LocalDate getLastDayOfMonth(LocalDate date) {
return getFirstDayOfMonth(date).plusMonths(1).minusDays(1);
}
public static List<Date> getListOfLastDaysOfMonth(Date start, Date end, boolean includeBounds) {
return toDateList(getListOfLastDaysOfMonth(convert(start), convert(end), includeBounds));
}
/**
* Liste des derniers jours des mois compris entre la date de début et la date de fin incluse
* @param start date de début
* @param end date de fin
* @param includeBounds vrai si on veut les dates de début et de fin dans la liste
* @return
*/
public static List<LocalDate> getListOfLastDaysOfMonth(LocalDate start, LocalDate end, boolean includeBounds) {
if ( end.isBefore(start) ) throw new IllegalArgumentException();
List<LocalDate> dates = new ArrayList<>();
LocalDate date = getLastDayOfMonth(start); // dernier jour du mois de la date de départ
if ( includeBounds && !date.equals(start) ) { // // si la date de départ n'est pas le dernier jour de mois et qu'on doit la mettre dans la liste, on la met
dates.add(start);
}
while(date.isBefore(end)) { // tant que la date est avant la date de fin
dates.add(date);
date = getLastDayOfMonth(date.plusDays(1)); // on avance d'un mois
}
if ( date.equals(end) ) { // si la date de fin est un dernier jour de mois, on l'intègre
dates.add(date);
}
else if ( includeBounds ) { // sinon, on l'intègre si demandé
dates.add(end);
}
return dates;
}
public static Date convert(LocalDate date) {
return Date.from(date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}
public static LocalDate convert(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
public static List<Date> toDateList(Collection<LocalDate> dates) {
return dates.stream().map(DateUtils::convert).collect(Collectors.toList());
}
public static List<LocalDate> toLocalDateList(Collection<Date> dates) {
return dates.stream().map(DateUtils::convert).collect(Collectors.toList());
}
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date dateDebut = format.parse("16/01/2018");
Date dateFin = format.parse("02/04/2018");
List<Date> dates = DateUtils.getListOfLastDaysOfMonth(dateDebut, dateFin, true);
System.out.println(getLastDayOfMonth(convert(dateDebut)));
System.out.println("Date de début : " + format.format(dateDebut));
System.out.println("Date de fin : " + format.format(dateFin));
System.out.println("le résultat doit etre : ");
dates.stream().map(format::format).forEach(System.out::println);
}
} |
Partager