|
38 | 38 | public class InternalIsoDuration implements IsoDuration
|
39 | 39 | {
|
40 | 40 | private static final List<TemporalUnit> SUPPORTED_UNITS = unmodifiableList( asList( MONTHS, DAYS, SECONDS, NANOS ) );
|
41 |
| - private static final InternalIsoDuration ZERO = new InternalIsoDuration( 0, 0, 0, 0 ); |
42 |
| - public static final long NANOS_PER_SECOND = 1_000_000_000L; |
43 | 41 |
|
44 | 42 | private final long months;
|
45 | 43 | private final long days;
|
@@ -195,65 +193,6 @@ public int hashCode()
|
195 | 193 | @Override
|
196 | 194 | public String toString()
|
197 | 195 | {
|
198 |
| - // print the duration in iso standard format. |
199 |
| - if ( this.equals( ZERO ) ) |
200 |
| - { |
201 |
| - return "PT0S"; // no need to allocate a string builder if we know the result |
202 |
| - } |
203 |
| - StringBuilder str = new StringBuilder().append( "P" ); |
204 |
| - append( str, months / 12, 'Y' ); |
205 |
| - append( str, months % 12, 'M' ); |
206 |
| - append( str, days / 7, 'W' ); |
207 |
| - append( str, days % 7, 'D' ); |
208 |
| - if ( seconds != 0 || nanoseconds != 0 ) |
209 |
| - { |
210 |
| - str.append( 'T' ); |
211 |
| - long s = seconds % 3600; |
212 |
| - append( str, seconds / 3600, 'H' ); |
213 |
| - append( str, s / 60, 'M' ); |
214 |
| - s %= 60; |
215 |
| - if ( s != 0 ) |
216 |
| - { |
217 |
| - str.append( s ); |
218 |
| - if ( nanoseconds != 0 ) |
219 |
| - { |
220 |
| - nanos( str ); |
221 |
| - } |
222 |
| - str.append( 'S' ); |
223 |
| - } |
224 |
| - else if ( nanoseconds != 0 ) |
225 |
| - { |
226 |
| - if ( nanoseconds < 0 ) |
227 |
| - { |
228 |
| - str.append( '-' ); |
229 |
| - } |
230 |
| - str.append( '0' ); |
231 |
| - nanos( str ); |
232 |
| - str.append( 'S' ); |
233 |
| - } |
234 |
| - } |
235 |
| - if ( str.length() == 1 ) |
236 |
| - { // this was all zeros (but not ZERO for some reason), ensure well formed output: |
237 |
| - str.append( "T0S" ); |
238 |
| - } |
239 |
| - return str.toString(); |
240 |
| - } |
241 |
| - |
242 |
| - private static void append( StringBuilder str, long quantity, char unit ) |
243 |
| - { |
244 |
| - if ( quantity != 0 ) |
245 |
| - { |
246 |
| - str.append( quantity ).append( unit ); |
247 |
| - } |
248 |
| - } |
249 |
| - |
250 |
| - private void nanos( StringBuilder str ) |
251 |
| - { |
252 |
| - str.append( '.' ); |
253 |
| - int n = nanoseconds < 0 ? -nanoseconds : nanoseconds; |
254 |
| - for ( int mod = (int)NANOS_PER_SECOND; mod > 1 && n > 0; n %= mod ) |
255 |
| - { |
256 |
| - str.append( n / (mod /= 10) ); |
257 |
| - } |
| 196 | + return String.format( "P%sM%sDT%s.%sS", months, days, seconds, String.format( "%09d", nanoseconds ) ); |
258 | 197 | }
|
259 | 198 | }
|
0 commit comments