How to get the first date and last date of current quarter in java.util.Date

前端 未结 8 861

I need to get the first date of the current quarter as a java.util.Date object and the last date of the current quarter as a java.util.Date

8条回答
  •  星月不相逢
    2020-12-10 14:58

    you can use java 8 LocaDate to get current quarter first and last day

    The code below gives you.

    public static LocalDate getCurrentQuarterStartDay(LocalDate date) {
            return date.with(IsoFields.DAY_OF_QUARTER, 1L);
        }
    
    public static LocalDate getCurrentQuarterEndDate(LocalDate date) {
            return date.with(IsoFields.DAY_OF_QUARTER, 92L);
        }
    

    Here in second method the 92 has partially lenient

    The day-of-quarter has values from 1 to 90 in Q1 of a standard year, from 1 to 91in Q1 of a leap year, from 1 to 91 in Q2 and from 1 to 92 in Q3 and Q4

    see ISOFields documentation for details here

提交回复
热议问题