@@ -131,7 +131,20 @@ public static DurationFormat.Style detect(String value) {
131
131
if (SIMPLE_PATTERN .matcher (value ).matches ()) {
132
132
return DurationFormat .Style .SIMPLE ;
133
133
}
134
- throw new IllegalArgumentException ("'" + value + "' is not a valid duration" );
134
+ throw new IllegalArgumentException ("'" + value + "' is not a valid duration, cannot detect any known style" );
135
+ }
136
+
137
+ public static long longValueFromUnit (Duration duration , ChronoUnit unit ) {
138
+ return switch (unit ) {
139
+ case NANOS -> duration .toNanos ();
140
+ case MICROS -> duration .toNanos () / 1000L ;
141
+ case MILLIS -> duration .toMillis ();
142
+ case SECONDS -> duration .toSeconds ();
143
+ case MINUTES -> duration .toMinutes ();
144
+ case HOURS -> duration .toHours ();
145
+ case DAYS -> duration .toDays ();
146
+ default -> throw new IllegalArgumentException ("'" + unit .name () + "' is not a supported ChronoUnit for simple duration representation" );
147
+ };
135
148
}
136
149
137
150
private static final Pattern ISO_8601_PATTERN = Pattern .compile ("^[+-]?[pP].*$" );
@@ -162,6 +175,11 @@ private static Duration parseSimple(String text, @Nullable ChronoUnit fallbackUn
162
175
}
163
176
}
164
177
178
+ private static String printSimple (Duration duration , @ Nullable ChronoUnit unit ) {
179
+ unit = (unit == null ? ChronoUnit .MILLIS : unit );
180
+ return longValueFromUnit (duration , unit ) + suffixFromUnit (unit );
181
+ }
182
+
165
183
/* package-private */ static ChronoUnit unitFromSuffix (String suffix ) {
166
184
return switch (suffix .toLowerCase ()) {
167
185
case "ns" -> ChronoUnit .NANOS ;
@@ -184,22 +202,8 @@ private static Duration parseSimple(String text, @Nullable ChronoUnit fallbackUn
184
202
case MINUTES -> "m" ;
185
203
case HOURS -> "h" ;
186
204
case DAYS -> "d" ;
187
- default -> throw new IllegalArgumentException ("'" + unit + "' is not a supported ChronoUnit for simple duration representation" );
205
+ default -> throw new IllegalArgumentException ("'" + unit . name () + "' is not a supported ChronoUnit for simple duration representation" );
188
206
};
189
207
}
190
208
191
- private static String printSimple (Duration duration , ChronoUnit unit ) {
192
- long longValue = switch (unit ) {
193
- case NANOS -> duration .toNanos ();
194
- case MICROS -> duration .toNanos () / 1000L ;
195
- case MILLIS -> duration .toMillis ();
196
- case SECONDS -> duration .toSeconds ();
197
- case MINUTES -> duration .toMinutes ();
198
- case HOURS -> duration .toHours ();
199
- case DAYS -> duration .toDays ();
200
- default -> throw new IllegalArgumentException ("'" + unit + "' is not a supported ChronoUnit for simple duration representation" );
201
- };
202
-
203
- return longValue + suffixFromUnit (unit );
204
- }
205
209
}
0 commit comments