I am using java 1.7, How to find the quarter to which a particular date belong for a Fiscal year, which can start from 1st of any month (JAN-DEC) and also need the start date
Use a calendar:
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
Quarter:
int q = cal.get(Calendar.MONTH)%3;
System.out.println("Quarter: Q" + (q + 1));
Begining of quarter:
cal.set(Calendar.MONTH, 3*q);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date start = cal.getTime();
System.out.println("Start: " + start);
End of quarter:
cal.add(Calendar.MONTH, 3);
cal.add(Calendar.DATE, -1);
Date end = cal.getTime();
System.out.println("End: " + end);
In Java 7, you could do:
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()));
}
For people coming to this question using Java 8, you can use 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);
}
java.time, the modern Java data and time API, gives you most of what you are asking for. Only it dies not know your financial year. The quarter you are asking for is one less that the quarter used in the ISO calendar system supported by java.time. So my trick is to subtract one quarter from your date and then query java.time about the quarter and year. That will give you the numbers you require.
In code, trying different dates:
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);
}
Output is:
Quarter: 4; year: 2019 Quarter: 1; year: 2020 Quarter: 2; year: 2020 Quarter: 3; year: 2020 Quarter: 4; year: 2020
Suppose you wanted a string like Q42019
rather than the numbers. In this case it’s best to use a formatter:
DateTimeFormatter quarterFormatter
= DateTimeFormatter.ofPattern("QQQuuuu", Locale.ENGLISH);
Now for each date do:
// Print a string like "Q42019"
String quarterString = syntheticQuarterDate.format(quarterFormatter);
System.out.println(quarterString);
Output using the same dates as before:
Q42019 Q12020 Q22020 Q32020 Q42020
java.time works nicely on Java 7. It just requires at least Java 6.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).