在java中,如何获取从01/01开始的日期不同于01/01的年份的上一个/当前季度和年份的开始和结束日期

pprl5pva  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(383)

我正在尝试实现一种返回以下内容的方法:
当前季度日期
上一季度日期
本年
上年
从2001年1月1日开始,我就可以用以下方法计算出一个典型的年份:

//Current quarter start and end date
      aCalendar.setTime(this.startdate);
    aCalendar.set(Calendar.MONTH, aCalendar.get(Calendar.MONTH) / 3 * 3);
    aCalendar.set(Calendar.DAY_OF_MONTH, 1);
    this.quarterStartDate = aCalendar.getTime();

    aCalendar.setTime(this.startdate);
    aCalendar.set(Calendar.MONTH, aCalendar.get(Calendar.MONTH) / 3 * 3 + 2);
    aCalendar.set(Calendar.DAY_OF_MONTH, aCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    this.quarterEndDate = aCalendar.getTime();
    if (this.quarterEndDate.getTime() > currentCalendar.getTimeInMillis())
        this.quarterEndDate = currentCalendar.getTime();

    // previous quarter
    aCalendar.setTime(this.startdate);
    aCalendar.add(Calendar.MONTH, -3);
    aCalendar.set(Calendar.MONTH, aCalendar.get(Calendar.MONTH) / 3 * 3 + 2);
    aCalendar.set(Calendar.DAY_OF_MONTH, aCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    this.previousQuarterEndDate = aCalendar.getTime();

    aCalendar.setTime(this.startdate);
    aCalendar.add(Calendar.MONTH, -3);
    aCalendar.set(Calendar.MONTH, aCalendar.get(Calendar.MONTH) / 3 * 3);
    aCalendar.set(Calendar.DAY_OF_MONTH, 1);
    this.previousQuarterStartDate = aCalendar.getTime();

    // current year
    aCalendar.setTime(this.startdate);
    aCalendar.set(Calendar.MONTH, 11); 
    aCalendar.set(Calendar.DAY_OF_MONTH, 31); 
    this.currentYearEndDate = aCalendar.getTime();

    if (this.currentYearEndDate.getTime() > currentCalendar.getTimeInMillis())
        this.currentYearEndDate = currentCalendar.getTime();

    aCalendar.setTime(this.startdate);
    aCalendar.set(Calendar.MONTH, 0); 
    aCalendar.set(Calendar.DAY_OF_MONTH, 1); 
    this.currentYearStartDate = aCalendar.getTime();

    // previous year
    aCalendar.setTime(this.currentYearEndDate);
    aCalendar.set(Calendar.YEAR, aCalendar.get(Calendar.YEAR) - 1);
    aCalendar.set(Calendar.MONTH, 11); // 
    aCalendar.set(Calendar.DAY_OF_MONTH, 31); 
    this.previousYearEndDate = aCalendar.getTime();

    aCalendar.setTime(this.currentYearStartDate);
    aCalendar.set(Calendar.YEAR, aCalendar.get(Calendar.YEAR) - 1);
    aCalendar.set(Calendar.MONTH, 0); // 
    aCalendar.set(Calendar.DAY_OF_MONTH, 1); 
    this.previousYearStartDate = aCalendar.getTime();

然而,我想解决的是,如果一年的开始日期与季度不同,例如2018年1月3日


**Q1**= 03/01/2018 -05/31/2018
**Q2**= 06/01/2018- 08/30/2018
**Q3**= 09/01/2018 - 11/30/2018
**Q4**= 12/01/2018 - 02/28/2019

我的问题是如何返回当前/上一季度和年度的开始和结束日期。例如,假设今天的日期是2018年10月19日,则应返回以下内容:
本季度:开始日期=2018年9月1日结束日期=2018年10月19日
上一季度:开始日期=6/1/2018结束日期=8/30/2018
本年度:开始日期=2018年3月1日结束日期=2018年10月19日
上一个year:start_date =3/12017结束日期=2/28/2018

cwtwac6a

cwtwac6a1#

java.time文件

final Month yearStart = Month.MARCH;
    final int yearStartValue = yearStart.getValue();

    LocalDate currentDate = LocalDate.of(2018, Month.OCTOBER, 19);

    // Current quarter start and end date
    // First find 0-based month witin quarter; 0, 1 or 2 
    int monthInQuarter = (currentDate.getMonthValue() + 12 - yearStartValue) % 3;
    LocalDate currentQuarterStart
            = currentDate.withDayOfMonth(1).minusMonths(monthInQuarter);
    LocalDate currentQuarterEndInclusive
            = currentQuarterStart.plusMonths(3).minusDays(1);
    System.out.println("Current quarter:  " + currentQuarterStart
            + " through " + currentQuarterEndInclusive);

    // previous quarter
    LocalDate previousQuarterStart = currentQuarterStart.minusMonths(3);
    LocalDate previousQuarterEndInclusive = currentQuarterStart.minusDays(1);
    System.out.println("Previous quarter: " + previousQuarterStart
            + " through " + previousQuarterEndInclusive);

    // current year
    // Find 0-based month in year
    int monthInYear = (currentDate.getMonthValue() + 12 - yearStartValue) % 12;
    LocalDate currentYearStart
            = currentDate.withDayOfMonth(1).minusMonths(monthInYear);
    LocalDate currentYearEndInclusive
            = currentYearStart.plusYears(1).minusDays(1);
    System.out.println("Current year:     " + currentYearStart
            + " through " + currentYearEndInclusive);

    // previous year
    LocalDate previousYearStart = currentYearStart.minusYears(1);
    LocalDate previousYearEndInclusive = currentYearStart.minusDays(1);
    System.out.println("Previous year:    " + previousYearStart
            + " through " + previousYearEndInclusive);

从3月1日开始的一年,当前日期为2018年10月19日,以上代码片段输出:

Current quarter:  2018-09-01 through 2018-11-30
Previous quarter: 2018-06-01 through 2018-08-31
Current year:     2018-03-01 through 2019-02-28
Previous year:    2017-03-01 through 2018-02-28

你不应该想用 Calendar 班级。它有几个设计问题,已经过时很久了。我用的是 LocalDate 来自java.time,现代java日期和时间api。与a相反 CalendarLocalDate 表示一个没有时间和时区的日期,所以我们需要它。
由于时间间隔通常是半开放的-从开始包含到结束独占-我补充说 Inclusive 结束日期变量的名称,以明确我们在这里使用的是闭合间隔。
link:oracle tutorial:说明如何使用的日期和时间 java.time .

相关问题