使用 Java 在给定上一季度结束日期的情况下查找下一个季度结束日期

Lor*_*ren 5 java datetime date java-8 apache-commons-dateutils

我有上个季度的季度结束日期让它成为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
Run Code Online (Sandbox Code Playgroud)

YCF*_*F_L 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
Run Code Online (Sandbox Code Playgroud)

注意:不要使用旧版 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());
}
Run Code Online (Sandbox Code Playgroud)

获取下个季度的最后一天

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

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());
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,“IsoFields.QUARTER_OF_YEAR”是正确的选择...之前已经投票过,不能再这样做了;-) (3认同)
  • @YCF_L,现在好了,测试通过了。 (2认同)

Bas*_*que 5

tl;博士

使用ThreeTen-Extra 中的YearQuarter类。

YearQuarter                                       // A class available in the ThreeTen-Extra library.
.from(                                            // Factory method rather than calling `new`. 
    LocalDate.of( 2020 , Month.SEPTEMBER , 30 )   // Returns a `LocalDate` object, represent a date-only value without a time-of-day and without a time zone.
)                                                 // Returns a `YearQuarter` object.
.plusQuarters( 1 )                                // Perform date-math, resulting in a new `YearQuarter` object (per immutable objects pattern). 
.atEndOfQuarter()                                 // Determine the date of last day of this year-quarter.
.toString()                                       // Generate text in standard ISO 8601 format.
Run Code Online (Sandbox Code Playgroud)

2020-12-31

org.threeten.extra.YearQuarter

ThreeTen-EXTRA库提供了扩展的功能的类java.time内置到Java 8和更高等级。其中一个类是YearQuarter代表特定年份中的特定季度。季度按日历年定义:1 月至 3 月、4 月至 6 月、7 月至 9 月、10 月至 12 月。

LocalDate localDate = LocalDate.of( 2020 , Month.SEPTEMBER , 30 ) ;
YearQuarter yearQuarter = YearQuarter.from( localDate ) ;
Run Code Online (Sandbox Code Playgroud)

通过在我们当前的年度季度增加一个季度来进入下一个季度。

java.timeThreeTen-EXTRA类使用不可变对象。因此,在添加时我们不会改变(“变异”)原始对象,而是生成一个新对象。

YearQuarter followingYearQuarter = yearQuarter.plusQuarters( 1 ) ;
Run Code Online (Sandbox Code Playgroud)

确定该季度的最后一天。

LocalDate lastDateOfFollowingYearQuarter = followingYearQuarter.atEndOfQuarter() ;
Run Code Online (Sandbox Code Playgroud)

关于java.time

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

现在处于维护模式Joda-Time项目建议迁移到java.time类。

您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*

从哪里获得 java.time 类?

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多