@@ -689,35 +689,7 @@ public static int currentTimeDays() {
689
689
return (int )(System .currentTimeMillis () / Dates .MILLISECONDS_IN_DAY );
690
690
}
691
691
692
- /**
693
- * Returns the milliseconds duration representation of the specified string duration.
694
- * <p>
695
- * The duration format is expressed with the regular expression:
696
- * <pre>
697
- * -?(\d+w)?(\d+d)?(\d+h)?(\d+m)?(\d*(.\d{1,3})s)?
698
- * </pre>
699
- * With the following key for units:
700
- * <p>
701
- * @formatter:off
702
- * <table>
703
- * <caption>Unit Key</caption>
704
- * <tr><td><b>Unit</b></td><td><b>Description</b></td></tr>
705
- * <tr><td><code><b>w</b></code></td><td>Week</td></tr>
706
- * <tr><td><code><b>d</b></code></td><td>Day</td></tr>
707
- * <tr><td><code><b>h</b></code></td><td>Hour</td></tr>
708
- * <tr><td><code><b>m</b></code></td><td>Minute</td></tr>
709
- * <tr><td><code><b>s</b></code></td><td>Second</td></tr>
710
- * </table>
711
- * @formatter:on
712
- *
713
- * @implSpec The second unit duration supports 3 fractional decimal digits.
714
- * @param str The duration string.
715
- * @return The milliseconds duration representation of the specified string duration.
716
- * @throws NullPointerException If {@code str} is null.
717
- * @throws IllegalArgumentException If {@code str} does not match the regular expression format.
718
- * @see #durationToString(long)
719
- */
720
- public static long stringToDuration (final String str ) {
692
+ private static long stringToDuration (final String str , final long defaultValue , final boolean hasDefaultValue ) {
721
693
long dur = 0 ;
722
694
723
695
long v = 0 ;
@@ -741,6 +713,8 @@ else if (d == 2)
741
713
a = 10 ;
742
714
else if (d == 3 )
743
715
a = 1 ;
716
+ else if (hasDefaultValue )
717
+ return defaultValue ;
744
718
else
745
719
throw new IllegalArgumentException ("Fractional values are only supported for 3 digits of second precision: " + str );
746
720
@@ -752,29 +726,45 @@ else if (ch == 'm') {
752
726
dur += v * MILLISECONDS_IN_MINUTE ;
753
727
v = 0 ;
754
728
d = 0 ;
755
- if (dot )
729
+ if (dot ) {
730
+ if (hasDefaultValue )
731
+ return defaultValue ;
732
+
756
733
throw new IllegalArgumentException ("Fractional values are only supported for second precision: " + str );
734
+ }
757
735
}
758
736
else if (ch == 'h' ) {
759
737
dur += v * MILLISECONDS_IN_HOUR ;
760
738
v = 0 ;
761
739
d = 0 ;
762
- if (dot )
740
+ if (dot ) {
741
+ if (hasDefaultValue )
742
+ return defaultValue ;
743
+
763
744
throw new IllegalArgumentException ("Fractional values are only supported for second precision: " + str );
745
+ }
764
746
}
765
747
else if (ch == 'd' ) {
766
748
dur += v * MILLISECONDS_IN_DAY ;
767
749
v = 0 ;
768
750
d = 0 ;
769
- if (dot )
751
+ if (dot ) {
752
+ if (hasDefaultValue )
753
+ return defaultValue ;
754
+
770
755
throw new IllegalArgumentException ("Fractional values are only supported for second precision: " + str );
756
+ }
771
757
}
772
758
else if (ch == 'w' ) {
773
759
dur += v * MILLISECONDS_IN_WEEK ;
774
760
v = 0 ;
775
761
d = 0 ;
776
- if (dot )
762
+ if (dot ) {
763
+ if (hasDefaultValue )
764
+ return defaultValue ;
765
+
777
766
throw new IllegalArgumentException ("Fractional values are only supported for second precision: " + str );
767
+ }
778
768
}
779
769
else if ('0' <= ch && ch <= '9' ) {
780
770
v *= 10 ;
@@ -785,6 +775,9 @@ else if (ch == '.') {
785
775
dot = true ;
786
776
d = 0 ;
787
777
}
778
+ else if (hasDefaultValue ) {
779
+ return defaultValue ;
780
+ }
788
781
else {
789
782
throw new IllegalArgumentException ("Unsupported character " + ch + " at position " + i + ": " + str );
790
783
}
@@ -796,6 +789,70 @@ else if (ch == '.') {
796
789
return isNegative ? -dur : dur ;
797
790
}
798
791
792
+ /**
793
+ * Returns the milliseconds duration representation of the specified string duration.
794
+ * <p>
795
+ * The duration format is expressed with the regular expression:
796
+ * <pre>
797
+ * -?(\d+w)?(\d+d)?(\d+h)?(\d+m)?(\d*(.\d{1,3})s)?
798
+ * </pre>
799
+ * With the following key for units:
800
+ * <p>
801
+ * @formatter:off
802
+ * <table>
803
+ * <caption>Unit Key</caption>
804
+ * <tr><td><b>Unit</b></td><td><b>Description</b></td></tr>
805
+ * <tr><td><code><b>w</b></code></td><td>Week</td></tr>
806
+ * <tr><td><code><b>d</b></code></td><td>Day</td></tr>
807
+ * <tr><td><code><b>h</b></code></td><td>Hour</td></tr>
808
+ * <tr><td><code><b>m</b></code></td><td>Minute</td></tr>
809
+ * <tr><td><code><b>s</b></code></td><td>Second</td></tr>
810
+ * </table>
811
+ * @formatter:on
812
+ *
813
+ * @implSpec The second unit duration supports 3 fractional decimal digits.
814
+ * @param str The duration string.
815
+ * @param defaultValue The value to return if {@code str} cannot be parsed as a duration.
816
+ * @return The milliseconds duration representation of the specified string duration.
817
+ * @throws NullPointerException If {@code str} is null.
818
+ * @see #durationToString(long)
819
+ */
820
+ public static long stringToDuration (final String str , final long defaultValue ) {
821
+ return stringToDuration (str , defaultValue , true );
822
+ }
823
+
824
+ /**
825
+ * Returns the milliseconds duration representation of the specified string duration.
826
+ * <p>
827
+ * The duration format is expressed with the regular expression:
828
+ * <pre>
829
+ * -?(\d+w)?(\d+d)?(\d+h)?(\d+m)?(\d*(.\d{1,3})s)?
830
+ * </pre>
831
+ * With the following key for units:
832
+ * <p>
833
+ * @formatter:off
834
+ * <table>
835
+ * <caption>Unit Key</caption>
836
+ * <tr><td><b>Unit</b></td><td><b>Description</b></td></tr>
837
+ * <tr><td><code><b>w</b></code></td><td>Week</td></tr>
838
+ * <tr><td><code><b>d</b></code></td><td>Day</td></tr>
839
+ * <tr><td><code><b>h</b></code></td><td>Hour</td></tr>
840
+ * <tr><td><code><b>m</b></code></td><td>Minute</td></tr>
841
+ * <tr><td><code><b>s</b></code></td><td>Second</td></tr>
842
+ * </table>
843
+ * @formatter:on
844
+ *
845
+ * @implSpec The second unit duration supports 3 fractional decimal digits.
846
+ * @param str The duration string.
847
+ * @return The milliseconds duration representation of the specified string duration.
848
+ * @throws NullPointerException If {@code str} is null.
849
+ * @throws IllegalArgumentException If {@code str} does not match the regular expression format.
850
+ * @see #durationToString(long)
851
+ */
852
+ public static long stringToDuration (final String str ) {
853
+ return stringToDuration (str , 0 , false );
854
+ }
855
+
799
856
/**
800
857
* Returns the string duration representation of the specified duration in milliseconds.
801
858
* <p>
0 commit comments