Here you can find the source of parseISODuration(String duration)
Parameter | Description |
---|---|
duration | the xsd:duration string. |
Parameter | Description |
---|---|
ParseException | if the duration string could not be parsed. |
public static float[] parseISODuration(String duration) throws ParseException
//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; } }