74. Getting the first and last day of a quarter
Let’s assume that we represent the first and last day of a quarter via this simple class:
public final class Quarter {
private final Date firstDay;
private final Date lastDay;
...
}
Next, we have a java.util.Date and we want the first and the last day of the quarter containing this date. For this, we can use JDK 8’s IsoFields.DAY_OF_QUARTER (we introduced IsoFields in the previous problem). But, before we can use IsoFields, we have to convert the given java.util.Date to a LocalDate as follows:
LocalDate localDate = date.toInstant()
.atZone(ZoneId.systemDefault()).toLocalDate();
Once we have the given Date as a LocalDate, we can easily extract the first day of the quarter via IsoFields.DAY_OF_QUARTER. Next, we add 2 months to this day to move into the last month of the quarter (a quarter has 3 months, so a year has 4 quarters) and we rely on java.time.temporal.TemporalAdjusters, more precisely on...