Javaを使用して次の四半期の終了日与えられた前四半期の終了日を探します

ローレン:

私は最後の四半期の四半期の終了日は、それが聞かせたのです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
YCF_L:

あなたの質問に答えるために、私はあなたがこれを見ていると思います:

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

注:レガシー日付ライブラリを使用していないが、あなたが使用できることを意味あなたの質問のJava-8タグ付きのjava-時 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());
}

次の四半期の最終日を取得します。

次の四半期の最終日を取得するには、その後、あなたは自分の今までにそうように3ヶ月を追加することができます。

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());
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=6296&siteId=1