Java Date ISO Parse parseISODuration(String duration)

Here you can find the source of parseISODuration(String duration)

Description

Parses the specified xsd:duration string.

License

BSD License

Parameter

Parameter Description
duration the xsd:duration string.

Exception

Parameter Description
ParseException if the duration string could not be parsed.

Return

an array containing the parsed values.

Declaration

public static float[] parseISODuration(String duration) throws ParseException 

Method Source Code


//package com.java2s;
/*/*w  w  w .j  a  v  a2  s.  c o m*/
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import java.text.ParseException;

public class Main {
    /**
     * Parses the specified xsd:duration string.
     * <p/>
     * Returns an array of length 7:
     * <ul>
     * <li>[0] plus/minus (1 or -1)</li>
     * <li>[1] years</li>
     * <li>[2] months</li>
     * <li>[3] days</li>
     * <li>[4] hours</li>
     * <li>[5] minutes</li>
     * <li>[6] seconds</li>
     * </ul>
     *
     * @param duration the xsd:duration string.
     * @return an array containing the parsed values.
     * @throws ParseException if the duration string could not be parsed.
     */
    public static float[] parseISODuration(String duration) throws ParseException {
        // duration format (-)PnYnMnDTnHnMnS
        float[] values = new float[7];
        int index = 0;

        // check minus sign
        if (duration.charAt(index) == '-') {
            values[0] = -1;
            index++;
        } else {
            values[0] = 1;
        }

        // check 'P' designator
        if (duration.charAt(index) == 'P') {
            index++;
        } else {
            throw new ParseException("missing 'P' designator at [" + index + "]", index);
        }

        char designator;
        int offset;
        int slot;
        boolean time = false;
        boolean pending = false;
        String value = null;

        while (index < duration.length()) {
            // get designator
            designator = duration.charAt(index);

            // time designator
            if (designator == 'T') {
                time = true;

                index++;
                continue;
            }

            // field designator
            if (isDesignator(designator)) {
                if (!pending) {
                    throw new ParseException("missing value at [" + index + "]", index);
                }

                slot = getDesignatorSlot(designator, time);
                try {
                    values[slot] = getDesignatorValue(designator, value);
                } catch (NumberFormatException e) {
                    throw new ParseException("malformed value at [" + (index - value.length()) + "]",
                            (index - value.length()));
                }

                pending = false;
                index++;
                continue;
            }

            // field value
            offset = getNumberOffset(duration, index);
            if (offset == index) {
                throw new ParseException("missing value at [" + index + "]", index);
            }
            if (offset == duration.length()) {
                throw new ParseException("missing designator at [" + offset + "]", offset);
            }

            value = duration.substring(index, offset);
            index = offset;
            pending = true;
        }

        return values;
    }

    private static boolean isDesignator(char c) {
        return c == 'Y' || c == 'M' || c == 'D' || c == 'H' || c == 'S';
    }

    private static int getDesignatorSlot(char designator, boolean time) {
        switch (designator) {
        case 'Y':
            return 1;
        case 'M':
            return time ? 5 : 2;
        case 'D':
            return 3;
        case 'H':
            return 4;
        case 'S':
            return 6;
        default:
            return -1;
        }
    }

    private static float getDesignatorValue(char designator, String value) throws NumberFormatException {
        if (designator != 'S') {
            Integer.parseInt(value);
        }

        return Float.parseFloat(value);
    }

    private static int getNumberOffset(String string, int offset) {
        int index;
        char current;
        for (index = offset; index < string.length(); index++) {
            current = string.charAt(index);
            if (!Character.isDigit(current) && current != '.') {
                break;
            }
        }

        return index;
    }
}

Related

  1. parseIsoDate(String str)
  2. parseIsoDate2(String s)
  3. parseIsoDateAndTimeString(String aDateString, String aTimeString)
  4. parseIsoDateInput(String str)
  5. parseIsoDateTime(String isoDateTime)
  6. parseIsoString(String time)
  7. parseTimestampIso8601(String timestamp)
  8. strongCheckIso8601Date(String date)
  9. time2StringISO(Date date)