5

我有上个季度的季度结束日期,顺其自然30-09-20,要求是找到下个季度的结束日期,即31-12-20。我正在使用下面的代码来做同样的事情,但它在某些情况下会给出错误的输出。该解决方案对所有方面都应该是正确的。

String str = "30-09-20";
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");
Date date = format.parse(str);
Date newDate = DateUtils.addMonths(date, 3);
System.out.println(newDate);//Dec 30 - It should be 31 Dec
4

5 回答 5

8

要回答您的问题,我认为您正在寻找这个:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yy");
LocalDate end = LocalDate.parse("30-09-20", formatter)
    .plusMonths(3)                             // add three months to your date
    .with(TemporalAdjusters.lastDayOfMonth()); // with the last day of the month

注意:不要使用旧的 Date 库,您标记了您的问题 Java-8,这意味着您可以使用java-time API。


获取当前季度的最后一天

@deHaar有理由,要获得当前季度的结束日期,我建议使用:

public LocalDate lastDayFromDateQuarter(String date) {
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yy");
    LocalDate ld = LocalDate.parse(date, formatter);
    int quarter = ld.get(IsoFields.QUARTER_OF_YEAR); // Get the Quarter, 1, 2, 3, 4
    // Then create a new date with new quarter * 3 and last day of month
    return ld.withMonth(quarter * 3).with(TemporalAdjusters.lastDayOfMonth());
}

获取下一季度的最后一天

要获得下个季度的最后一天,您只需将日期添加三个月,如下所示:

public static LocalDate lastDayFromDateQuarter(String date) {
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yy");
    LocalDate ld = LocalDate.parse(date, formatter);
    int quarter = ld.get(IsoFields.QUARTER_OF_YEAR);
    return ld.withMonth(quarter * 3)
            .plusMonths(3)
            .with(TemporalAdjusters.lastDayOfMonth());
}
于 2020-01-28T10:56:36.343 回答
5
于 2020-01-31T07:57:23.893 回答
2

这是我的版本(希望更具可读性)找到下一个季度的最后一天的任何日期:

public LocalDate lastDayOfNextQuarter(LocalDate date) {
  Month firstMonthOfCurrentQuarter = date.getMonth().firstMonthOfQuarter();
  LocalDate lastMonthOfCurrentQuarter = date.with(firstMonthOfCurrentQuarter.plus(2));
  LocalDate lastMonthOfNextQuarter = lastMonthOfCurrentQuarter.plusMonths(3);
  return lastMonthOfNextQuarter.with(lastDayOfMonth());
}

以及相应的测试方法:

@ParameterizedTest
@CsvSource({"2020-01-01,2020-06-30", "2020-02-01,2020-06-30", "2020-03-01,2020-06-30", "2020-04-10,2020-09-30",
  "2020-05-10,2020-09-30", "2020-06-10,2020-09-30", "2020-07-20,2020-12-31", "2020-08-20,2020-12-31",
  "2020-09-30,2020-12-31", "2020-10-30,2021-03-31", "2020-11-30,2021-03-31", "2020-12-31,2021-03-31"})
public void testLastDayOfNextQuarter(LocalDate input, LocalDate expected) {
  LocalDate result = timeUtils.lastDayOfNextQuarter(input);
  assertEquals(expected, result);
}
于 2020-01-30T12:44:05.820 回答
0

您可以使用Calendar实例来获取该月的最后一天。

String str = "30-12-20";
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");
Date date = format.parse(str);
Date newDate = DateUtils.addMonths(date, 3);

Calendar cal = new GregorianCalendar();
cal.setTime(newDate);

System.out.println(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
于 2020-01-28T10:53:11.303 回答
0

您可以使用 TemporalAdjusters 轻松操作季度。见下文:

    LocalDate localDate = LocalDate.now();
    LocalDate firstDayOfQuarter = localDate.with(IsoFields.DAY_OF_QUARTER, 1);
    System.out.println(firstDayOfQuarter);

    LocalDate lastDayOfQuarter = firstDayOfQuarter.plusMonths(2).with(TemporalAdjusters.lastDayOfMonth());
    System.out.println(lastDayOfQuarter);

    LocalDate firstDateOfNextQuarter = lastDayOfQuarter.plusDays(1);

    LocalDate lastDayOfNextQuarter = firstDateOfNextQuarter.plusMonths(2).with(TemporalAdjusters.lastDayOfMonth());
    System.out.println(lastDayOfNextQuarter);

输出:

2020-01-01
2020-03-31
2020-06-30
于 2020-01-28T11:05:05.863 回答