YearMonth With Clock.fixed Billing Windows
Derive monthly billing windows from YearMonth.now(Clock.fixed) so leap-year February and 31-day rollovers behave deterministically in tests.
Code
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneOffset;
public class BillingCycleWindow {
static void printCycle(Clock clock) {
YearMonth current = YearMonth.now(clock);
LocalDate start = current.atDay(1);
LocalDate end = current.atEndOfMonth();
YearMonth next = current.plusMonths(1);
System.out.println(current + " [" + start + " .. " + end + "]"
+ " days=" + current.lengthOfMonth()
+ " nextEnd=" + next.atEndOfMonth());
}
public static void main(String[] args) {
// Pin clock to late UTC month-end in both leap and common years
Clock leapFeb = Clock.fixed(Instant.parse("2024-02-29T23:55:00Z"), ZoneOffset.UTC);
Clock janEdge = Clock.fixed(Instant.parse("2026-01-31T23:59:59Z"), ZoneOffset.UTC);
printCycle(leapFeb);
printCycle(janEdge);
}
}Inside the implementation — YearMonth With Clock.fixed Billing Windows
YearMonth With Clock.fixed Billing Windows keeps everything in the class BillingCycleWindow and the functions printCycle and main.
Caveats
- No lockfile entry, no version pinning — the file needs nothing beyond the runtime.
Applying it
Key routine header declares static void printCycle(Clock clock)
Start the program and main runs printCycle one after another.
Statements that matter
Clock leapFeb = Clock.fixed(Instant.parse("2024-02-29T23:55:00Z"), ZoneOffset.UTC);
Clock janEdge = Clock.fixed(Instant.parse("2026-01-31T23:59:59Z"), ZoneOffset.UTC);
YearMonth current = YearMonth.now(clock);
Interactive Tools for This Topic
Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.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.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.
FAQ
- How do you run YearMonth With Clock.fixed Billing Windows?
- Run it as a complete program — compile or interpret the file directly; there is nothing to install first.
Report a problem with this exampleOpens a pre-filled issue on GitHub.