JasperSoft Studio如何将分钟数格式化为hh:mm "h"格式打印?
hh:mm "h" in JasperSoft Studio Great question! Since you're working with an Integer total minutes variable ($V{Total_Time}) in your production report and want to replace that clunky conditional formula with something cleaner, here are a few minimal-change solutions tailored to JasperSoft Studio:
1. Quick Fix: Replace with String.format() (Lowest Effort)
This is the simplest swap for your existing formula—no extra setup needed, just replace your current text box expression with:
String.format("%02d:%02d h", $V{Total_Time}/60, $V{Total_Time}%60)
Why this works:
- The
%02dspecifier automatically formats numbers to two digits, padding with a leading zero if needed (so you don't need thatIFcheck for single-digit minutes). - It’s concise, readable, and drops straight into your existing text box without modifying variables or report structure.
2. For Durations Over 24 Hours: Use DurationFormatUtils
If your total time could exceed 24 hours (e.g., 1500 minutes = 25 hours), a standard date formatter will reset hours to 0 after 24. Instead, use Apache Commons' DurationFormatUtils (already included in most JasperSoft Studio setups):
org.apache.commons.lang.time.DurationFormatUtils.formatDuration($V{Total_Time}*60*1000, "HH:mm") + " h"
Why this works:
- It’s designed specifically for formatting elapsed time, so it will display
25:00 hinstead of01:00 hfor 1500 minutes. - No extra library imports required—JasperSoft typically bundles Apache Commons out of the box.
3. Jasper Best Practice: Separate Expression and Formatting
If you prefer to follow Jasper's recommended pattern (separating data logic from display), try this:
- Update your text box expression to convert minutes to a timestamp (note: only for durations ≤24 hours):
new Date($V{Total_Time}*60*1000) - Go to the text box's Properties > Pattern field and set it to:
HH:mm 'h'
Why this works:
- Keeps your expression clean (no string concatenation) and makes future format changes trivial—just edit the pattern instead of rewriting the formula.
- Jasper handles the zero-padding automatically behind the scenes.
Key Notes:
- If your total time will never exceed 24 hours, options 1 or 3 are your best bets for minimal changes.
- For extended durations, stick with option 2 to avoid incorrect hour wrapping.
内容的提问来源于stack exchange,提问作者Willas

