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

前端 未结 8 862

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:57

    You can use joda:

    Date start = date.withMonthOfYear(((date.getMonthOfYear() / 3) + 1) * 3 - 2)
                .withDayOfMonth(1)
                .withTimeAtStartOfDay()
                .toDate();
    Date end = date.withMonthOfYear(((date.getMonthOfYear() / 3) + 1) * 3)
                .withDayOfMonth(Month.of(((date.getMonthOfYear() / 3) + 1) * 3).maxLength())
                .withTimeAtStartOfDay()
                .toDate();
    

    When DateTime date = new DateTime(2016, 8, 12, 1, 1, 1);

    I get:

    Fri Jul 01 00:00:00 CEST 2016
    Fri Sep 30 00:00:00 CEST 2016
    

提交回复
热议问题