DurationFormatUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.time;

  18. import java.text.SimpleDateFormat;
  19. import java.util.ArrayList;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import java.util.GregorianCalendar;
  23. import java.util.Objects;
  24. import java.util.TimeZone;
  25. import java.util.stream.Stream;

  26. import org.apache.commons.lang3.StringUtils;
  27. import org.apache.commons.lang3.Strings;
  28. import org.apache.commons.lang3.Validate;

  29. /**
  30.  * Duration formatting utilities and constants. The following table describes the tokens
  31.  * used in the pattern language for formatting.
  32.  * <table border="1">
  33.  *  <caption>Pattern Tokens</caption>
  34.  *  <tr><th>character</th><th>duration element</th></tr>
  35.  *  <tr><td>y</td><td>years</td></tr>
  36.  *  <tr><td>M</td><td>months</td></tr>
  37.  *  <tr><td>d</td><td>days</td></tr>
  38.  *  <tr><td>H</td><td>hours</td></tr>
  39.  *  <tr><td>m</td><td>minutes</td></tr>
  40.  *  <tr><td>s</td><td>seconds</td></tr>
  41.  *  <tr><td>S</td><td>milliseconds</td></tr>
  42.  *  <tr><td>'text'</td><td>arbitrary text content</td></tr>
  43.  * </table>
  44.  *
  45.  * <strong>Note: It's not currently possible to include a single-quote in a format.</strong>
  46.  * <br>
  47.  * Token values are printed using decimal digits.
  48.  * A token character can be repeated to ensure that the field occupies a certain minimum
  49.  * size. Values will be left-padded with 0 unless padding is disabled in the method invocation.
  50.  * <br>
  51.  * Tokens can be marked as optional by surrounding them with brackets [ ]. These tokens will
  52.  * only be printed if the token value is non-zero. Literals within optional blocks will only be
  53.  * printed if the preceding non-literal token is non-zero. Leading optional literals will only
  54.  * be printed if the following non-literal is non-zero.
  55.  * Multiple optional blocks can be used to group literals with the desired token.
  56.  * <p>
  57.  * Notes on Optional Tokens:<br>
  58.  * <strong>Multiple optional tokens without literals can result in impossible to understand output.</strong><br>
  59.  * <strong>Patterns where all tokens are optional can produce empty strings.</strong><br>
  60.  * (See examples below)
  61.  * </p>
  62.  * <br>
  63.  * <table border="1">
  64.  * <caption>Example Output</caption>
  65.  * <tr><th>pattern</th><th>Duration.ofDays(1)</th><th>Duration.ofHours(1)</th><th>Duration.ofMinutes(1)</th><th>Duration.ZERO</th></tr>
  66.  * <tr><td>d'd'H'h'm'm's's'</td><td>1d0h0m0s</td><td>0d1h0m0s</td><td>0d0h1m0s</td><td>0d0h0m0s</td></tr>
  67.  * <tr><td>d'd'[H'h'm'm']s's'</td><td>1d0s</td><td>0d1h0s</td><td>0d1m0s</td><td>0d0s</td></tr>
  68.  * <tr><td>[d'd'H'h'm'm']s's'</td><td>1d0s</td><td>1h0s</td><td>1m0s</td><td>0s</td></tr>
  69.  * <tr><td>[d'd'H'h'm'm's's']</td><td>1d</td><td>1h</td><td>1m</td><td></td></tr>
  70.  * <tr><td>['{'d'}']HH':'mm</td><td>{1}00:00</td><td>01:00</td><td>00:01</td><td>00:00</td></tr>
  71.  * <tr><td>['{'dd'}']['&lt;'HH'&gt;']['('mm')']</td><td>{01}</td><td>&lt;01&gt;</td><td>(00)</td><td></td></tr>
  72.  * <tr><td>[dHms]</td><td>1</td><td>1</td><td>1</td><td></td></tr>
  73.  * </table>
  74.  * <strong>Note: Optional blocks cannot be nested.</strong>
  75.  *
  76.  * @since 2.1
  77.  */
  78. public class DurationFormatUtils {

  79.     /**
  80.      * Element that is parsed from the format pattern.
  81.      */
  82.     static final class Token {

  83.         /** Empty array. */
  84.         private static final Token[] EMPTY_ARRAY = {};

  85.         /**
  86.          * Helper method to determine if a set of tokens contain a value
  87.          *
  88.          * @param tokens set to look in
  89.          * @param value to look for
  90.          * @return boolean {@code true} if contained
  91.          */
  92.         static boolean containsTokenWithValue(final Token[] tokens, final Object value) {
  93.             return Stream.of(tokens).anyMatch(token -> token.getValue() == value);
  94.         }

  95.         private final CharSequence value;
  96.         private int count;
  97.         private int optionalIndex = -1;

  98.         /**
  99.          * Wraps a token around a value. A value would be something like a 'Y'.
  100.          *
  101.          * @param value value to wrap, non-null.
  102.          * @param optional whether the token is optional
  103.          * @param optionalIndex the index of the optional token within the pattern
  104.          */
  105.         Token(final CharSequence value, final boolean optional, final int optionalIndex) {
  106.             this.value = Objects.requireNonNull(value, "value");
  107.             this.count = 1;
  108.             if (optional) {
  109.                 this.optionalIndex = optionalIndex;
  110.             }
  111.         }

  112.         /**
  113.          * Supports equality of this Token to another Token.
  114.          *
  115.          * @param obj2 Object to consider equality of
  116.          * @return boolean {@code true} if equal
  117.          */
  118.         @Override
  119.         public boolean equals(final Object obj2) {
  120.             if (obj2 instanceof Token) {
  121.                 final Token tok2 = (Token) obj2;
  122.                 if (this.value.getClass() != tok2.value.getClass()) {
  123.                     return false;
  124.                 }
  125.                 if (this.count != tok2.count) {
  126.                     return false;
  127.                 }
  128.                 if (this.value instanceof StringBuilder) {
  129.                     return this.value.toString().equals(tok2.value.toString());
  130.                 }
  131.                 if (this.value instanceof Number) {
  132.                     return this.value.equals(tok2.value);
  133.                 }
  134.                 return this.value == tok2.value;
  135.             }
  136.             return false;
  137.         }

  138.         /**
  139.          * Gets the current number of values represented
  140.          *
  141.          * @return int number of values represented
  142.          */
  143.         int getCount() {
  144.             return count;
  145.         }

  146.         /**
  147.          * Gets the particular value this token represents.
  148.          *
  149.          * @return Object value, non-null.
  150.          */
  151.         Object getValue() {
  152.             return value;
  153.         }

  154.         /**
  155.          * Returns a hash code for the token equal to the
  156.          * hash code for the token's value. Thus 'TT' and 'TTTT'
  157.          * will have the same hash code.
  158.          *
  159.          * @return The hash code for the token
  160.          */
  161.         @Override
  162.         public int hashCode() {
  163.             return this.value.hashCode();
  164.         }

  165.         /**
  166.          * Adds another one of the value
  167.          */
  168.         void increment() {
  169.             count++;
  170.         }

  171.         /**
  172.          * Represents this token as a String.
  173.          *
  174.          * @return String representation of the token
  175.          */
  176.         @Override
  177.         public String toString() {
  178.             return StringUtils.repeat(this.value.toString(), this.count);
  179.         }
  180.     }

  181.     private static final int MINUTES_PER_HOUR = 60;

  182.     private static final int SECONDS_PER_MINUTES = 60;

  183.     private static final int HOURS_PER_DAY = 24;

  184.     /**
  185.      * Pattern used with {@link FastDateFormat} and {@link SimpleDateFormat}
  186.      * for the ISO 8601 period format used in durations.
  187.      *
  188.      * @see org.apache.commons.lang3.time.FastDateFormat
  189.      * @see java.text.SimpleDateFormat
  190.      */
  191.     public static final String ISO_EXTENDED_FORMAT_PATTERN = "'P'yyyy'Y'M'M'd'DT'H'H'm'M's.SSS'S'";

  192.     static final String y = "y";

  193.     static final String M = "M";

  194.     static final String d = "d";

  195.     static final String H = "H";

  196.     static final String m = "m";

  197.     static final String s = "s";

  198.     static final String S = "S";

  199.     /**
  200.      * The internal method to do the formatting.
  201.      *
  202.      * @param tokens  the tokens
  203.      * @param years  the number of years
  204.      * @param months  the number of months
  205.      * @param days  the number of days
  206.      * @param hours  the number of hours
  207.      * @param minutes  the number of minutes
  208.      * @param seconds  the number of seconds
  209.      * @param milliseconds  the number of millis
  210.      * @param padWithZeros  whether to pad
  211.      * @return the formatted string
  212.      */
  213.     static String format(final Token[] tokens, final long years, final long months, final long days, final long hours, final long minutes,
  214.             final long seconds,
  215.             final long milliseconds, final boolean padWithZeros) {
  216.         final StringBuilder buffer = new StringBuilder();
  217.         boolean lastOutputSeconds = false;
  218.         boolean lastOutputZero = false;
  219.         int optionalStart = -1;
  220.         boolean firstOptionalNonLiteral = false;
  221.         int optionalIndex = -1;
  222.         boolean inOptional = false;
  223.         for (final Token token : tokens) {
  224.             final Object value = token.getValue();
  225.             final boolean isLiteral = value instanceof StringBuilder;
  226.             final int count = token.getCount();
  227.             if (optionalIndex != token.optionalIndex) {
  228.               optionalIndex = token.optionalIndex;
  229.               if (optionalIndex > -1) {
  230.                 //entering new optional block
  231.                 optionalStart = buffer.length();
  232.                 lastOutputZero = false;
  233.                 inOptional = true;
  234.                 firstOptionalNonLiteral = false;
  235.               } else {
  236.                 //leaving optional block
  237.                 inOptional = false;
  238.               }
  239.             }
  240.             if (isLiteral) {
  241.                 if (!inOptional || !lastOutputZero) {
  242.                     buffer.append(value.toString());
  243.                 }
  244.             } else if (value.equals(y)) {
  245.                 lastOutputSeconds = false;
  246.                 lastOutputZero = years == 0;
  247.                 if (!inOptional || !lastOutputZero) {
  248.                     buffer.append(paddedValue(years, padWithZeros, count));
  249.                 }
  250.             } else if (value.equals(M)) {
  251.                 lastOutputSeconds = false;
  252.                 lastOutputZero = months == 0;
  253.                 if (!inOptional || !lastOutputZero) {
  254.                     buffer.append(paddedValue(months, padWithZeros, count));
  255.                 }
  256.             } else if (value.equals(d)) {
  257.                 lastOutputSeconds = false;
  258.                 lastOutputZero = days == 0;
  259.                 if (!inOptional || !lastOutputZero) {
  260.                     buffer.append(paddedValue(days, padWithZeros, count));
  261.                 }
  262.             } else if (value.equals(H)) {
  263.                 lastOutputSeconds = false;
  264.                 lastOutputZero = hours == 0;
  265.                 if (!inOptional || !lastOutputZero) {
  266.                     buffer.append(paddedValue(hours, padWithZeros, count));
  267.                 }
  268.             } else if (value.equals(m)) {
  269.                 lastOutputSeconds = false;
  270.                 lastOutputZero = minutes == 0;
  271.                 if (!inOptional || !lastOutputZero) {
  272.                     buffer.append(paddedValue(minutes, padWithZeros, count));
  273.                 }
  274.             } else if (value.equals(s)) {
  275.                 lastOutputSeconds = true;
  276.                 lastOutputZero = seconds == 0;
  277.                 if (!inOptional || !lastOutputZero) {
  278.                     buffer.append(paddedValue(seconds, padWithZeros, count));
  279.                 }
  280.             } else if (value.equals(S)) {
  281.                 lastOutputZero = milliseconds == 0;
  282.                 if (!inOptional || !lastOutputZero) {
  283.                     if (lastOutputSeconds) {
  284.                         // ensure at least 3 digits are displayed even if padding is not selected
  285.                         final int width = padWithZeros ? Math.max(3, count) : 3;
  286.                         buffer.append(paddedValue(milliseconds, true, width));
  287.                     } else {
  288.                         buffer.append(paddedValue(milliseconds, padWithZeros, count));
  289.                     }
  290.                 }
  291.                 lastOutputSeconds = false;
  292.             }
  293.             //as soon as we hit first nonliteral in optional, check for literal prefix
  294.             if (inOptional && !isLiteral && !firstOptionalNonLiteral) {
  295.                 firstOptionalNonLiteral = true;
  296.                 if (lastOutputZero) {
  297.                     buffer.delete(optionalStart, buffer.length());
  298.                 }
  299.             }
  300.         }
  301.         return buffer.toString();
  302.     }

  303.     /**
  304.      * Formats the time gap as a string, using the specified format, and padding with zeros.
  305.      *
  306.      * <p>This method formats durations using the days and lower fields of the
  307.      * format pattern. Months and larger are not used.</p>
  308.      *
  309.      * @param durationMillis  the duration to format
  310.      * @param format  the way in which to format the duration, not null
  311.      * @return the formatted duration, not null
  312.      * @throws IllegalArgumentException if durationMillis is negative
  313.      */
  314.     public static String formatDuration(final long durationMillis, final String format) {
  315.         return formatDuration(durationMillis, format, true);
  316.     }

  317.     /**
  318.      * Formats the time gap as a string, using the specified format.
  319.      * Padding the left-hand side side of numbers with zeroes is optional.
  320.      *
  321.      * <p>This method formats durations using the days and lower fields of the
  322.      * format pattern. Months and larger are not used.</p>
  323.      *
  324.      * @param durationMillis  the duration to format
  325.      * @param format  the way in which to format the duration, not null
  326.      * @param padWithZeros  whether to pad the left-hand side side of numbers with 0's
  327.      * @return the formatted duration, not null
  328.      * @throws IllegalArgumentException if durationMillis is negative
  329.      */
  330.     public static String formatDuration(final long durationMillis, final String format, final boolean padWithZeros) {
  331.         Validate.inclusiveBetween(0, Long.MAX_VALUE, durationMillis, "durationMillis must not be negative");

  332.         final Token[] tokens = lexx(format);

  333.         long days = 0;
  334.         long hours = 0;
  335.         long minutes = 0;
  336.         long seconds = 0;
  337.         long milliseconds = durationMillis;

  338.         if (Token.containsTokenWithValue(tokens, d)) {
  339.             days = milliseconds / DateUtils.MILLIS_PER_DAY;
  340.             milliseconds -= days * DateUtils.MILLIS_PER_DAY;
  341.         }
  342.         if (Token.containsTokenWithValue(tokens, H)) {
  343.             hours = milliseconds / DateUtils.MILLIS_PER_HOUR;
  344.             milliseconds -= hours * DateUtils.MILLIS_PER_HOUR;
  345.         }
  346.         if (Token.containsTokenWithValue(tokens, m)) {
  347.             minutes = milliseconds / DateUtils.MILLIS_PER_MINUTE;
  348.             milliseconds -= minutes * DateUtils.MILLIS_PER_MINUTE;
  349.         }
  350.         if (Token.containsTokenWithValue(tokens, s)) {
  351.             seconds = milliseconds / DateUtils.MILLIS_PER_SECOND;
  352.             milliseconds -= seconds * DateUtils.MILLIS_PER_SECOND;
  353.         }

  354.         return format(tokens, 0, 0, days, hours, minutes, seconds, milliseconds, padWithZeros);
  355.     }

  356.     /**
  357.      * Formats the time gap as a string.
  358.      *
  359.      * <p>The format used is ISO 8601-like: {@code HH:mm:ss.SSS}.</p>
  360.      *
  361.      * @param durationMillis  the duration to format
  362.      * @return the formatted duration, not null
  363.      * @throws IllegalArgumentException if durationMillis is negative
  364.      */
  365.     public static String formatDurationHMS(final long durationMillis) {
  366.         return formatDuration(durationMillis, "HH:mm:ss.SSS");
  367.     }

  368.     /**
  369.      * Formats the time gap as a string.
  370.      *
  371.      * <p>The format used is the ISO 8601 period format.</p>
  372.      *
  373.      * <p>This method formats durations using the days and lower fields of the
  374.      * ISO format pattern, such as P7D6TH5M4.321S.</p>
  375.      *
  376.      * @param durationMillis  the duration to format
  377.      * @return the formatted duration, not null
  378.      * @throws IllegalArgumentException if durationMillis is negative
  379.      */
  380.     public static String formatDurationISO(final long durationMillis) {
  381.         return formatDuration(durationMillis, ISO_EXTENDED_FORMAT_PATTERN, false);
  382.     }

  383.     /**
  384.      * Formats an elapsed time into a pluralization correct string.
  385.      *
  386.      * <p>This method formats durations using the days and lower fields of the
  387.      * format pattern. Months and larger are not used.</p>
  388.      *
  389.      * @param durationMillis  the elapsed time to report in milliseconds
  390.      * @param suppressLeadingZeroElements  suppresses leading 0 elements
  391.      * @param suppressTrailingZeroElements  suppresses trailing 0 elements
  392.      * @return the formatted text in days/hours/minutes/seconds, not null
  393.      * @throws IllegalArgumentException if durationMillis is negative
  394.      */
  395.     public static String formatDurationWords(
  396.         final long durationMillis,
  397.         final boolean suppressLeadingZeroElements,
  398.         final boolean suppressTrailingZeroElements) {

  399.         // This method is generally replaceable by the format method, but
  400.         // there are a series of tweaks and special cases that require
  401.         // trickery to replicate.
  402.         String duration = formatDuration(durationMillis, "d' days 'H' hours 'm' minutes 's' seconds'");
  403.         if (suppressLeadingZeroElements) {
  404.             // this is a temporary marker on the front. Like ^ in regexp.
  405.             duration = " " + duration;
  406.             final String text = duration;
  407.             String tmp = Strings.CS.replaceOnce(text, " 0 days", StringUtils.EMPTY);
  408.             if (tmp.length() != duration.length()) {
  409.                 duration = tmp;
  410.                 final String text1 = duration;
  411.                 tmp = Strings.CS.replaceOnce(text1, " 0 hours", StringUtils.EMPTY);
  412.                 if (tmp.length() != duration.length()) {
  413.                     duration = tmp;
  414.                     final String text2 = duration;
  415.                     tmp = Strings.CS.replaceOnce(text2, " 0 minutes", StringUtils.EMPTY);
  416.                     duration = tmp;
  417.                 }
  418.             }
  419.             if (!duration.isEmpty()) {
  420.                 // strip the space off again
  421.                 duration = duration.substring(1);
  422.             }
  423.         }
  424.         if (suppressTrailingZeroElements) {
  425.             final String text = duration;
  426.             String tmp = Strings.CS.replaceOnce(text, " 0 seconds", StringUtils.EMPTY);
  427.             if (tmp.length() != duration.length()) {
  428.                 duration = tmp;
  429.                 final String text1 = duration;
  430.                 tmp = Strings.CS.replaceOnce(text1, " 0 minutes", StringUtils.EMPTY);
  431.                 if (tmp.length() != duration.length()) {
  432.                     duration = tmp;
  433.                     final String text2 = duration;
  434.                     tmp = Strings.CS.replaceOnce(text2, " 0 hours", StringUtils.EMPTY);
  435.                     if (tmp.length() != duration.length()) {
  436.                         final String text3 = tmp;
  437.                         duration = Strings.CS.replaceOnce(text3, " 0 days", StringUtils.EMPTY);
  438.                     }
  439.                 }
  440.             }
  441.         }
  442.         // handle plurals
  443.         duration = " " + duration;
  444.         final String text = duration;
  445.         duration = Strings.CS.replaceOnce(text, " 1 seconds", " 1 second");
  446.         final String text1 = duration;
  447.         duration = Strings.CS.replaceOnce(text1, " 1 minutes", " 1 minute");
  448.         final String text2 = duration;
  449.         duration = Strings.CS.replaceOnce(text2, " 1 hours", " 1 hour");
  450.         final String text3 = duration;
  451.         duration = Strings.CS.replaceOnce(text3, " 1 days", " 1 day");
  452.         return duration.trim();
  453.     }

  454.     /**
  455.      * Formats the time gap as a string, using the specified format.
  456.      * Padding the left-hand side side of numbers with zeroes is optional.
  457.      *
  458.      * @param startMillis  the start of the duration
  459.      * @param endMillis  the end of the duration
  460.      * @param format  the way in which to format the duration, not null
  461.      * @return the formatted duration, not null
  462.      * @throws IllegalArgumentException if startMillis is greater than endMillis
  463.      */
  464.     public static String formatPeriod(final long startMillis, final long endMillis, final String format) {
  465.         return formatPeriod(startMillis, endMillis, format, true, TimeZone.getDefault());
  466.     }

  467.     /**
  468.      * <p>Formats the time gap as a string, using the specified format.
  469.      * Padding the left-hand side side of numbers with zeroes is optional and
  470.      * the time zone may be specified.
  471.      *
  472.      * <p>When calculating the difference between months/days, it chooses to
  473.      * calculate months first. So when working out the number of months and
  474.      * days between January 15th and March 10th, it choose 1 month and
  475.      * 23 days gained by choosing January-&gt;February = 1 month and then
  476.      * calculating days forwards, and not the 1 month and 26 days gained by
  477.      * choosing March -&gt; February = 1 month and then calculating days
  478.      * backwards.</p>
  479.      *
  480.      * <p>For more control, the <a href="https://www.joda.org/joda-time/">Joda-Time</a>
  481.      * library is recommended.</p>
  482.      *
  483.      * @param startMillis  the start of the duration
  484.      * @param endMillis  the end of the duration
  485.      * @param format  the way in which to format the duration, not null
  486.      * @param padWithZeros  whether to pad the left-hand side side of numbers with 0's
  487.      * @param timezone  the millis are defined in
  488.      * @return the formatted duration, not null
  489.      * @throws IllegalArgumentException if startMillis is greater than endMillis
  490.      */
  491.     public static String formatPeriod(final long startMillis, final long endMillis, final String format, final boolean padWithZeros,
  492.             final TimeZone timezone) {
  493.         Validate.isTrue(startMillis <= endMillis, "startMillis must not be greater than endMillis");

  494.         // Used to optimize for differences under 28 days and
  495.         // called formatDuration(millis, format); however this did not work
  496.         // over leap years.
  497.         // TODO: Compare performance to see if anything was lost by
  498.         // losing this optimization.

  499.         final Token[] tokens = lexx(format);

  500.         // time zones get funky around 0, so normalizing everything to GMT
  501.         // stops the hours being off
  502.         final Calendar start = Calendar.getInstance(timezone);
  503.         start.setTime(new Date(startMillis));
  504.         final Calendar end = Calendar.getInstance(timezone);
  505.         end.setTime(new Date(endMillis));

  506.         // initial estimates
  507.         long milliseconds = end.get(Calendar.MILLISECOND) - start.get(Calendar.MILLISECOND);
  508.         int seconds = end.get(Calendar.SECOND) - start.get(Calendar.SECOND);
  509.         int minutes = end.get(Calendar.MINUTE) - start.get(Calendar.MINUTE);
  510.         int hours = end.get(Calendar.HOUR_OF_DAY) - start.get(Calendar.HOUR_OF_DAY);
  511.         int days = end.get(Calendar.DAY_OF_MONTH) - start.get(Calendar.DAY_OF_MONTH);
  512.         int months = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
  513.         int years = end.get(Calendar.YEAR) - start.get(Calendar.YEAR);

  514.         // each initial estimate is adjusted in case it is under 0
  515.         while (milliseconds < 0) {
  516.             milliseconds += DateUtils.MILLIS_PER_SECOND;
  517.             seconds -= 1;
  518.         }
  519.         while (seconds < 0) {
  520.             seconds += SECONDS_PER_MINUTES;
  521.             minutes -= 1;
  522.         }
  523.         while (minutes < 0) {
  524.             minutes += MINUTES_PER_HOUR;
  525.             hours -= 1;
  526.         }
  527.         while (hours < 0) {
  528.             hours += HOURS_PER_DAY;
  529.             days -= 1;
  530.         }

  531.         if (Token.containsTokenWithValue(tokens, M)) {
  532.             while (days < 0) {
  533.                 days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
  534.                 months -= 1;
  535.                 start.add(Calendar.MONTH, 1);
  536.             }

  537.             while (months < 0) {
  538.                 months += 12;
  539.                 years -= 1;
  540.             }

  541.             if (!Token.containsTokenWithValue(tokens, y) && years != 0) {
  542.                 while (years != 0) {
  543.                     months += 12 * years;
  544.                     years = 0;
  545.                 }
  546.             }
  547.         } else {
  548.             // there are no M's in the format string

  549.             if (!Token.containsTokenWithValue(tokens, y)) {
  550.                 int target = end.get(Calendar.YEAR);
  551.                 if (months < 0) {
  552.                     // target is end-year -1
  553.                     target -= 1;
  554.                 }

  555.                 while (start.get(Calendar.YEAR) != target) {
  556.                     days += start.getActualMaximum(Calendar.DAY_OF_YEAR) - start.get(Calendar.DAY_OF_YEAR);

  557.                     // Not sure I grok why this is needed, but the brutal tests show it is
  558.                     if (start instanceof GregorianCalendar &&
  559.                             start.get(Calendar.MONTH) == Calendar.FEBRUARY &&
  560.                             start.get(Calendar.DAY_OF_MONTH) == 29) {
  561.                         days += 1;
  562.                     }

  563.                     start.add(Calendar.YEAR, 1);

  564.                     days += start.get(Calendar.DAY_OF_YEAR);
  565.                 }

  566.                 years = 0;
  567.             }

  568.             while (start.get(Calendar.MONTH) != end.get(Calendar.MONTH)) {
  569.                 days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
  570.                 start.add(Calendar.MONTH, 1);
  571.             }

  572.             months = 0;

  573.             while (days < 0) {
  574.                 days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
  575.                 months -= 1;
  576.                 start.add(Calendar.MONTH, 1);
  577.             }

  578.         }

  579.         // The rest of this code adds in values that
  580.         // aren't requested. This allows the user to ask for the
  581.         // number of months and get the real count and not just 0->11.

  582.         if (!Token.containsTokenWithValue(tokens, d)) {
  583.             hours += HOURS_PER_DAY * days;
  584.             days = 0;
  585.         }
  586.         if (!Token.containsTokenWithValue(tokens, H)) {
  587.             minutes += MINUTES_PER_HOUR * hours;
  588.             hours = 0;
  589.         }
  590.         if (!Token.containsTokenWithValue(tokens, m)) {
  591.             seconds += SECONDS_PER_MINUTES * minutes;
  592.             minutes = 0;
  593.         }
  594.         if (!Token.containsTokenWithValue(tokens, s)) {
  595.             milliseconds += DateUtils.MILLIS_PER_SECOND * seconds;
  596.             seconds = 0;
  597.         }

  598.         return format(tokens, years, months, days, hours, minutes, seconds, milliseconds, padWithZeros);
  599.     }

  600.     /**
  601.      * Formats the time gap as a string.
  602.      *
  603.      * <p>The format used is the ISO 8601 period format.</p>
  604.      *
  605.      * @param startMillis  the start of the duration to format
  606.      * @param endMillis  the end of the duration to format
  607.      * @return the formatted duration, not null
  608.      * @throws IllegalArgumentException if startMillis is greater than endMillis
  609.      */
  610.     public static String formatPeriodISO(final long startMillis, final long endMillis) {
  611.         return formatPeriod(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault());
  612.     }

  613.     /**
  614.      * Parses a classic date format string into Tokens
  615.      *
  616.      * @param format  the format to parse, not null
  617.      * @return array of Token[]
  618.      */
  619.     static Token[] lexx(final String format) {
  620.         final ArrayList<Token> list = new ArrayList<>(format.length());

  621.         boolean inLiteral = false;
  622.         // Although the buffer is stored in a Token, the Tokens are only
  623.         // used internally, so cannot be accessed by other threads
  624.         StringBuilder buffer = null;
  625.         Token previous = null;
  626.         boolean inOptional = false;
  627.         int optionalIndex = -1;
  628.         for (int i = 0; i < format.length(); i++) {
  629.             final char ch = format.charAt(i);
  630.             if (inLiteral && ch != '\'') {
  631.                 buffer.append(ch); // buffer can't be null if inLiteral is true
  632.                 continue;
  633.             }
  634.             String value = null;
  635.             switch (ch) {
  636.             // TODO: Need to handle escaping of '
  637.             case '[':
  638.                 if (inOptional) {
  639.                     throw new IllegalArgumentException("Nested optional block at index: " + i);
  640.                 }
  641.                 optionalIndex++;
  642.                 inOptional = true;
  643.                 break;
  644.             case ']':
  645.                 if (!inOptional) {
  646.                     throw new IllegalArgumentException("Attempting to close unopened optional block at index: " + i);
  647.                 }
  648.                 inOptional = false;
  649.                 break;
  650.             case '\'':
  651.                 if (inLiteral) {
  652.                     buffer = null;
  653.                     inLiteral = false;
  654.                 } else {
  655.                     buffer = new StringBuilder();
  656.                     list.add(new Token(buffer, inOptional, optionalIndex));
  657.                     inLiteral = true;
  658.                 }
  659.                 break;
  660.             case 'y':
  661.                 value = y;
  662.                 break;
  663.             case 'M':
  664.                 value = M;
  665.                 break;
  666.             case 'd':
  667.                 value = d;
  668.                 break;
  669.             case 'H':
  670.                 value = H;
  671.                 break;
  672.             case 'm':
  673.                 value = m;
  674.                 break;
  675.             case 's':
  676.                 value = s;
  677.                 break;
  678.             case 'S':
  679.                 value = S;
  680.                 break;
  681.             default:
  682.                 if (buffer == null) {
  683.                     buffer = new StringBuilder();
  684.                     list.add(new Token(buffer, inOptional, optionalIndex));
  685.                 }
  686.                 buffer.append(ch);
  687.             }

  688.             if (value != null) {
  689.                 if (previous != null && previous.getValue().equals(value)) {
  690.                     previous.increment();
  691.                 } else {
  692.                     final Token token = new Token(value, inOptional, optionalIndex);
  693.                     list.add(token);
  694.                     previous = token;
  695.                 }
  696.                 buffer = null;
  697.             }
  698.         }
  699.         if (inLiteral) { // i.e. we have not found the end of the literal
  700.             throw new IllegalArgumentException("Unmatched quote in format: " + format);
  701.         }
  702.         if (inOptional) { // i.e. we have not found the end of the literal
  703.             throw new IllegalArgumentException("Unmatched optional in format: " + format);
  704.         }
  705.         return list.toArray(Token.EMPTY_ARRAY);
  706.     }

  707.     /**
  708.      * Converts a {@code long} to a {@link String} with optional
  709.      * zero padding.
  710.      *
  711.      * @param value the value to convert
  712.      * @param padWithZeros whether to pad with zeroes
  713.      * @param count the size to pad to (ignored if {@code padWithZeros} is false)
  714.      * @return the string result
  715.      */
  716.     private static String paddedValue(final long value, final boolean padWithZeros, final int count) {
  717.         final String longString = Long.toString(value);
  718.         return padWithZeros ? StringUtils.leftPad(longString, count, '0') : longString;
  719.     }

  720.     /**
  721.      * DurationFormatUtils instances should NOT be constructed in standard programming.
  722.      *
  723.      * <p>This constructor is public to permit tools that require a JavaBean instance
  724.      * to operate.</p>
  725.      *
  726.      * @deprecated TODO Make private in 4.0.
  727.      */
  728.     @Deprecated
  729.     public DurationFormatUtils() {
  730.         // empty
  731.     }

  732. }