我正在使用java 1.7,如何查找某个特定日期属于某个会计年度的季度,该季度可以从任何月份的第1天开始(JAN-DEC),还需要该季度的开始日期和结束日期。
即假设2017财年(2017年4月1日至2018年3月31日),则2017年6月26日属于Q1(第1季),季度开始日期为2017年10月1日,截止日期为2017年12月31日..等等
答案 0 :(得分:1)
在Java 7中,你可以这样做:
public static void main(String[] args) {
// Dates are DD/MM/YYYY
String date_string = "26/06/2017";
String quarter_start_string = "01/04/2017";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date in_date = sdf.parse(date_string);
Date qstart = sdf.parse(quarter_start_string);
boolean found = false;
int quarter = 1;
Calendar in_date_cal = GregorianCalendar.getInstance();
in_date_cal.setTime(in_date);
Calendar fq_start = GregorianCalendar.getInstance();
fq_start.setTime(qstart);
Calendar fq_end = GregorianCalendar.getInstance();
fq_end.setTime(qstart);
fq_end.add(GregorianCalendar.MONTH, 3 );
while (!found) {
if (in_date_cal.hashCode() >= fq_start.hashCode() &&
in_date_cal.hashCode() <= fq_end.hashCode()) {
break;
}
fq_start.add(GregorianCalendar.MONTH, 3 );
fq_end.add(GregorianCalendar.MONTH, 3 );
quarter++;
}
System.out.println("Quarter# is " + quarter);
System.out.println("Quarter Start is " + sdf.format(fq_start.getTime()));
System.out.println("Quarter End is " + sdf.format(fq_end.getTime()));
}
对于使用Java 8来解决此问题的人,您可以使用LocalDate
:
public static void main(String[] args) throws ParseException {
// Dates are DD/MM/YYYY
String date_string = "26/06/2017";
String quarter_start_string = "01/04/2017";
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate ld_in = LocalDate.parse(date_string, f);
LocalDate ld_qstart = LocalDate.parse(quarter_start_string, f);
LocalDate fq_start = ld_qstart;
LocalDate fq_end = ld_qstart.plusMonths(3);
boolean found = false;
int quarter = 1;
while (!found) {
if (ld_in.hashCode() >= fq_start.hashCode() &&
ld_in.hashCode() <= fq_end.hashCode()) {
break;
}
fq_start = fq_start.plusMonths(3);
fq_end = fq_start.plusMonths(3);
quarter++;
}
System.out.println("Quarter# is " + quarter);
System.out.println("Quarter Start is " + fq_start);
System.out.println("Quarter End is " + fq_end);
}
答案 1 :(得分:0)
java.time是现代的Java数据和时间API,可为您提供大部分所需的信息。只有它死了不知道您的财政年度。您要查询的四分之一要比java.time支持的ISO日历系统中使用的四分之一少。因此,我的诀窍是从日期中减去四分之一,然后查询java.time中的四分之一和年份。这将为您提供所需的电话号码。
在代码中,尝试不同的日期:
LocalDate[] exampleDates = {
LocalDate.of(2020, Month.JANUARY, 1),
LocalDate.of(2020, Month.JUNE, 30),
LocalDate.of(2020, Month.AUGUST, 22),
LocalDate.of(2020, Month.OCTOBER, 1),
LocalDate.of(2021, Month.MARCH, 31),
};
for (LocalDate date : exampleDates) {
LocalDate syntheticQuarterDate = date.minus(1, IsoFields.QUARTER_YEARS);
// Get quarter number and year as int
int quarter = syntheticQuarterDate.get(IsoFields.QUARTER_OF_YEAR);
int year = syntheticQuarterDate.getYear();
System.out.format("Quarter: %d; year: %d%n", quarter, year);
}
输出为:
Quarter: 4; year: 2019 Quarter: 1; year: 2020 Quarter: 2; year: 2020 Quarter: 3; year: 2020 Quarter: 4; year: 2020
假设您想要一个像Q42019
这样的字符串,而不是数字。在这种情况下,最好使用格式化程序:
DateTimeFormatter quarterFormatter
= DateTimeFormatter.ofPattern("QQQuuuu", Locale.ENGLISH);
现在每个日期执行一次:
// Print a string like "Q42019"
String quarterString = syntheticQuarterDate.format(quarterFormatter);
System.out.println(quarterString);
使用与以前相同的日期进行输出:
Q42019 Q12020 Q22020 Q32020 Q42020
java.time在Java 7上运行良好。它至少需要 Java 6 。
org.threeten.bp
导入日期和时间类。java.time
。java.time
向Java 6和7(JSR-310的ThreeTen)的反向端口。答案 2 :(得分:-1)
使用日历:
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
区:
int q = cal.get(Calendar.MONTH)%3;
System.out.println("Quarter: Q" + (q + 1));
开始季度:
cal.set(Calendar.MONTH, 3*q);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date start = cal.getTime();
System.out.println("Start: " + start);
季末:
cal.add(Calendar.MONTH, 3);
cal.add(Calendar.DATE, -1);
Date end = cal.getTime();
System.out.println("End: " + end);