获取当前财政年度的开始日期
Get start date of current financial year
在英国,纳税年度是从每年的 4 月 6 日到 4 月 5 日。我想获取当前纳税年度的开始日期(作为 LocalDate
),例如,如果今天是 2020 年 4 月 3 日,那么 return 2019 年 4 月 6 日,如果今天是 2020 年 4 月 8 日,然后 return 2020 年 4 月 6 日。
我可以使用如下逻辑计算它:
date = a new LocalDate of 6 April with today's year
if (the date is after today) {
return date minus 1 year
} else {
return date
}
但是有没有我可以使用的方法,它不那么复杂并且使用更简洁、也许是实用的风格?
有几种不同的方法,但很容易以非常实用的方式实现您已经指定的逻辑:
private static final MonthDay FINANCIAL_START = MonthDay.of(4, 6);
private static LocalDate getStartOfFinancialYear(LocalDate date) {
// Try "the same year as the date we've been given"
LocalDate candidate = date.with(FINANCIAL_START);
// If we haven't reached that yet, subtract a year. Otherwise, use it.
return candidate.isAfter(date) ? candidate.minusYears(1) : candidate;
}
这非常简洁明了。请注意,它 不 使用当前日期 - 它接受一个日期。这使得它更容易测试。当然,调用它并提供当前日期很容易。
使用 java.util.Calendar,您可以获得给定日期所在财政年度的开始和结束日期。
在印度,财政年度从 4 月 1 日开始,到 3 月 31 日结束,
对于 2020-21 财政年度,日期为 2020 年 4 月 1 日
public static Date getFirstDateOfFinancialYear(Date dateToCheck) {
int year = getYear(dateToCheck);
Calendar cal = Calendar.getInstance();
cal.set(year, 3, 1); // 1 April of Year
Date firstAprilOfYear = cal.getTime();
if (dateToCheck.after(firstAprilOfYear)) {
return firstAprilOfYear;
} else {
cal.set(year - 1, 3, 1);
return cal.getTime();
}
}
在你的情况下设置 cal.set(year, 0, 1); // 每年 1 月 1 日
public static Date getLastDateOfFinancialYear(Date dateToCheck) {
int year = getYear(dateToCheck);
Calendar cal = Calendar.getInstance();
cal.set(year, 2, 31); // 31 March of Year
Date thirtyFirstOfYear = cal.getTime();
if (dateToCheck.after(thirtyFirstOfYear)) {
cal.set(year + 1, 2, 31);
return cal.getTime();
} else {
return thirtyFirstOfYear;
}
}
在你的例子中设置 cal.set(year, 11, 31); // 每年 12 月 31 日
在英国,纳税年度是从每年的 4 月 6 日到 4 月 5 日。我想获取当前纳税年度的开始日期(作为 LocalDate
),例如,如果今天是 2020 年 4 月 3 日,那么 return 2019 年 4 月 6 日,如果今天是 2020 年 4 月 8 日,然后 return 2020 年 4 月 6 日。
我可以使用如下逻辑计算它:
date = a new LocalDate of 6 April with today's year
if (the date is after today) {
return date minus 1 year
} else {
return date
}
但是有没有我可以使用的方法,它不那么复杂并且使用更简洁、也许是实用的风格?
有几种不同的方法,但很容易以非常实用的方式实现您已经指定的逻辑:
private static final MonthDay FINANCIAL_START = MonthDay.of(4, 6);
private static LocalDate getStartOfFinancialYear(LocalDate date) {
// Try "the same year as the date we've been given"
LocalDate candidate = date.with(FINANCIAL_START);
// If we haven't reached that yet, subtract a year. Otherwise, use it.
return candidate.isAfter(date) ? candidate.minusYears(1) : candidate;
}
这非常简洁明了。请注意,它 不 使用当前日期 - 它接受一个日期。这使得它更容易测试。当然,调用它并提供当前日期很容易。
使用 java.util.Calendar,您可以获得给定日期所在财政年度的开始和结束日期。
在印度,财政年度从 4 月 1 日开始,到 3 月 31 日结束, 对于 2020-21 财政年度,日期为 2020 年 4 月 1 日
public static Date getFirstDateOfFinancialYear(Date dateToCheck) {
int year = getYear(dateToCheck);
Calendar cal = Calendar.getInstance();
cal.set(year, 3, 1); // 1 April of Year
Date firstAprilOfYear = cal.getTime();
if (dateToCheck.after(firstAprilOfYear)) {
return firstAprilOfYear;
} else {
cal.set(year - 1, 3, 1);
return cal.getTime();
}
}
在你的情况下设置 cal.set(year, 0, 1); // 每年 1 月 1 日
public static Date getLastDateOfFinancialYear(Date dateToCheck) {
int year = getYear(dateToCheck);
Calendar cal = Calendar.getInstance();
cal.set(year, 2, 31); // 31 March of Year
Date thirtyFirstOfYear = cal.getTime();
if (dateToCheck.after(thirtyFirstOfYear)) {
cal.set(year + 1, 2, 31);
return cal.getTime();
} else {
return thirtyFirstOfYear;
}
}
在你的例子中设置 cal.set(year, 11, 31); // 每年 12 月 31 日