IsoFields Quarter Bucketing
Bucket a date into fiscal quarters with IsoFields: QUARTER_OF_YEAR labels the quarter and DAY_OF_QUARTER reconstructs its first and last day.
Code
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.IsoFields;
public class QuarterBucket {
public static void main(String[] args) {
LocalDate close = LocalDate.of(2026, 11, 20);
int quarter = close.get(IsoFields.QUARTER_OF_YEAR); // 1..4
int dayOfQuarter = close.get(IsoFields.DAY_OF_QUARTER); // 1..92
LocalDate quarterStart = close.with(IsoFields.DAY_OF_QUARTER, 1);
LocalDate quarterEnd = quarterStart.plusMonths(3).minusDays(1);
System.out.println("quarter: " + quarter);
System.out.println("day of quarter: " + dayOfQuarter);
System.out.println("range: " + quarterStart + " .. " + quarterEnd);
// 财报口径:季度标签与该季度结束月份
YearMonth quarterEndMonth = YearMonth.from(quarterEnd);
System.out.println("label: Q" + quarter + "/" + close.getYear()
+ " end " + quarterEndMonth);
}
}How it works
Structurally, expect the class QuarterBucket and the function main and nothing else.
Applying it
Entry signature: public static void main(String[] args)
The file announces its steps in the output: quarter:, day of quarter:, range:.
Statements that matter
int dayOfQuarter = close.get(IsoFields.DAY_OF_QUARTER); // 1..92
LocalDate quarterStart = close.with(IsoFields.DAY_OF_QUARTER, 1);
LocalDate quarterEnd = quarterStart.plusMonths(3).minusDays(1);
YearMonth quarterEndMonth = YearMonth.from(quarterEnd);
Interactive Tools for This Topic
Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.ISO 8601 Duration ParserParse ISO 8601 durations like PT1H30M into year, month, week, day, and time components with exact or approximate total seconds.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does IsoFields Quarter Bucketing print when run?
- Execute the whole file; each printed line is prefixed with a label like `quarter:`, `day of quarter:`, `range:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.