Example usage for org.joda.time DateTimeConstants SECONDS_PER_HOUR

List of usage examples for org.joda.time DateTimeConstants SECONDS_PER_HOUR

Introduction

In this page you can find the example usage for org.joda.time DateTimeConstants SECONDS_PER_HOUR.

Prototype

int SECONDS_PER_HOUR

To view the source code for org.joda.time DateTimeConstants SECONDS_PER_HOUR.

Click Source Link

Document

Seconds in one hour (ISO)

Usage

From source file:uk.ac.susx.tag.method51.core.params.codec.concrete.DurationCodec.java

License:Apache License

@Override
public Duration decodeString(String value) throws DecodeException {
    Duration result = null;/*from   ww  w  . j ava2 s .  c  o  m*/

    long milliseconds;

    try {
        milliseconds = Long.parseLong(value);

    } catch (NumberFormatException e) {

        try {
            if (value.length() == 0) {
                throw new IllegalArgumentException("zero length interval specification");
            }
            value = value.toUpperCase();

            if (!value.matches(".*[DWMHS].*")) {
                throw new IllegalArgumentException(
                        "no time units specified, see ISO 8601 duration specification (months / years not supported): http://en.wikipedia.org/wiki/ISO_8601#Durations");
            }

            if (!value.startsWith("P")) {
                value = "P" + value;
            }

            Matcher m = Pattern.compile("[HMS]").matcher(value);
            if (!value.contains("T") && m.find()) {
                String c = m.group(0);
                int i = value.indexOf(c);
                value = value.substring(0, i - 1) + "T" + value.substring(i - 1);
            }

            Period period = ISOPeriodFormat.standard().parsePeriod(value);
            long seconds = 0;
            seconds += period.getSeconds();
            seconds += period.getMinutes() * DateTimeConstants.SECONDS_PER_MINUTE;
            seconds += period.getHours() * DateTimeConstants.SECONDS_PER_HOUR;
            seconds += period.getDays() * DateTimeConstants.SECONDS_PER_DAY;
            seconds += period.getWeeks() * DateTimeConstants.SECONDS_PER_WEEK;

            milliseconds = seconds * 1000;

        } catch (IllegalArgumentException e1) {
            throw new DecodeException("'" + value + "' is not a number, or an ISO 8601 duration", e1);
        }
    }

    result = new Duration(milliseconds);

    return result;
}