Duration Parts Read Human Budgets
Split a Duration into day, hour, minute, and second parts with toXxxPart methods, where each part is a remainder and toHours is the total.
Code
import java.time.Duration;
public class SLAReport {
public static void main(String[] args) {
// 1 天 2 小时 30 分 15 秒的 SLA 预算
Duration budget = Duration.parse("P1DT2H30M15S");
// toHours() 是总小时数:1 天折成 24 + 2 = 26
System.out.println("total hours: " + budget.toHours());
// toXxxPart 是"分量":除掉更大单位后的余数,用于 1d 02:30:15 这种展示
System.out.println("days: " + budget.toDaysPart());
System.out.println("hours: " + budget.toHoursPart());
System.out.println("minutes: " + budget.toMinutesPart());
System.out.println("seconds: " + budget.toSecondsPart());
// toSeconds() 仍是总数:150 秒的 toSecondsPart 是余数 30
Duration wait = Duration.ofSeconds(150);
System.out.println("total seconds: " + wait.toSeconds());
System.out.println("seconds part: " + wait.toSecondsPart());
// 拼一段 "2d 03:05:09" 风格的报表文案
String rendered = "%dd %02d:%02d:%02d".formatted(
budget.toDaysPart(), budget.toHoursPart(),
budget.toMinutesPart(), budget.toSecondsPart());
System.out.println("rendered: " + rendered);
}
}What the code does
Duration Parts Read Human Budgets is built around the class SLAReport and the function main.
Watch out for
- Nothing to add to the project before running it.
Using it
Signature: public static void main(String[] args)
The run emits 8 labeled sections — total hours:, days:, hours:, minutes:, seconds:, total seconds:, seconds part:, rendered: — one per step.
Key statements
Duration budget = Duration.parse("P1DT2H30M15S");
Duration wait = Duration.ofSeconds(150);
Interactive Tools for This Topic
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.Date Difference CalculatorCalculate the number of days, weeks, whole months, and years between two ISO dates, computed in UTC.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.
FAQ
- What does Duration Parts Read Human Budgets print when run?
- Invoke the file as-is — the `total hours:`, `days:`, `hours:` prefixes identify which step printed each line.
Report a problem with this exampleOpens a pre-filled issue on GitHub.