Java SQL Time deserializeSqlTime(String text)

Here you can find the source of deserializeSqlTime(String text)

Description

Deserialize time from text.

License

Open Source License

Parameter

Parameter Description
text text to be parsed (may be <code>null</code>)

Exception

Parameter Description
IllegalArgumentException on parse error

Return

converted time, or null if passed null input

Declaration

public static Time deserializeSqlTime(String text) throws IllegalArgumentException 

Method Source Code


//package com.java2s;
import java.sql.Time;

public class Main {
    /**//from   w w  w.  ja v  a  2 s.c om
     * Deserialize time from text. Time values obey the rules of the time
     * portion of a dataTime value. This method follows standard JiBX
     * deserializer usage requirements by accepting a <code>null</code> input.
     *
     * @param text text to be parsed (may be <code>null</code>)
     *
     * @return converted time, or <code>null</code> if passed <code>null</code>
     *         input
     *
     * @throws IllegalArgumentException on parse error
     */
    public static Time deserializeSqlTime(String text) throws IllegalArgumentException {
        if (text == null) {
            return null;
        } else {
            return new Time(parseTime(text, 0, text.length()));
        }
    }

    /**
     * Parse general time value from text. Time values are expected to be in
     * W3C XML Schema standard format as hh:mm:ss.fff, with optional leading
     * sign and trailing time zone.
     *
     * @param text text to be parsed
     * @param start offset of first character of time value
     * @param length number of characters in time value
     *
     * @return converted time as millisecond value
     *
     * @throws IllegalArgumentException on parse error
     */
    public static long parseTime(String text, int start, int length) throws IllegalArgumentException {
        // validate time value following date
        long milli = 0;
        boolean valid = (length > (start + 7)) && (text.charAt(start + 2) == ':')
                && (text.charAt(start + 5) == ':');

        if (valid) {
            int hour = parseDigits(text, start, 2);
            int minute = parseDigits(text, start + 3, 2);
            int second = parseDigits(text, start + 6, 2);

            if ((hour > 23) || (minute > 59) || (second > 60)) {
                valid = false;
            } else {
                // convert to base millisecond in day
                milli = ((((hour * 60) + minute) * 60) + second) * 1000;
                start += 8;

                if (length > start) {
                    // adjust for time zone
                    if (text.charAt(length - 1) == 'Z') {
                        length--;
                    } else {
                        char chr = text.charAt(length - 6);

                        if ((chr == '-') || (chr == '+')) {
                            hour = parseDigits(text, length - 5, 2);
                            minute = parseDigits(text, length - 2, 2);

                            if ((hour > 23) || (minute > 59)) {
                                valid = false;
                            } else {
                                int offset = ((hour * 60) + minute) * 60 * 1000;

                                if (chr == '-') {
                                    milli += offset;
                                } else {
                                    milli -= offset;
                                }
                            }

                            length -= 6;
                        }
                    }

                    // check for trailing fractional second
                    if (text.charAt(start) == '.') {
                        double fraction = Double.parseDouble(text.substring(start, length));
                        milli += (fraction * 1000.0);
                    } else if (length > start) {
                        valid = false;
                    }
                }
            }
        }

        // check for valid result
        if (valid) {
            return milli;
        } else {
            throw new IllegalArgumentException("Invalid dateTime format");
        }
    }

    /**
     * Parse digits in text as integer value. This internal method is used for
     * number values embedded within lexical structures. Only decimal digits
     * can be included in the text range parsed.
     *
     * @param text text to be parsed
     * @param offset starting offset in text
     * @param length number of digits to be parsed
     *
     * @return converted positive integer value
     *
     * @throws IllegalArgumentException on parse error
     */
    private static int parseDigits(String text, int offset, int length) throws IllegalArgumentException {
        // check if overflow a potential problem
        int value = 0;

        if (length > 9) {
            // use library parse code for potential overflow
            try {
                value = Integer.parseInt(text.substring(offset, offset + length));
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException(ex.getMessage());
            }
        } else {
            // parse with no overflow worries
            int limit = offset + length;

            while (offset < limit) {
                char chr = text.charAt(offset++);

                if ((chr >= '0') && (chr <= '9')) {
                    value = (value * 10) + (chr - '0');
                } else {
                    throw new IllegalArgumentException("Non-digit in number value");
                }
            }
        }

        return value;
    }

    /**
     * Parse double value from text. This uses the W3C XML Schema format for
     * doubles, with the exception that it will accept "+NaN" and "-NaN" as
     * valid formats. This is not in strict compliance with the specification,
     * but is included for interoperability with other Java XML processing.
     *
     * @param text text to be parsed
     *
     * @return converted double value
     *
     * @throws IllegalArgumentException on parse error
     */
    public static double parseDouble(String text) throws IllegalArgumentException {
        text = text.trim();

        if ("-INF".equals(text)) {
            return Double.NEGATIVE_INFINITY;
        } else if ("INF".equals(text)) {
            return Double.POSITIVE_INFINITY;
        } else {
            try {
                return Double.parseDouble(text);
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException(ex.getMessage());
            }
        }
    }

    /**
     * Parse integer value from text. Integer values are parsed with optional
     * leading sign flag, followed by any number of digits.
     *
     * @param text text to be parsed
     *
     * @return converted integer value
     *
     * @throws IllegalArgumentException on parse error
     */
    public static int parseInt(String text) throws IllegalArgumentException {
        // make sure there's text to be processed
        text = text.trim();

        int offset = 0;
        int limit = text.length();

        if (limit == 0) {
            throw new IllegalArgumentException("Empty number value");
        }

        // check leading sign present in text
        boolean negate = false;
        char chr = text.charAt(0);

        if (chr == '-') {
            if (limit > 9) {
                // special case to make sure maximum negative value handled
                try {
                    return Integer.parseInt(text);
                } catch (NumberFormatException ex) {
                    throw new IllegalArgumentException(ex.getMessage());
                }
            } else {
                negate = true;
                offset++;
            }
        } else if (chr == '+') {
            offset++;
        }

        if (offset >= limit) {
            throw new IllegalArgumentException("Invalid number format");
        }

        // handle actual value conversion
        int value = parseDigits(text, offset, limit - offset);

        if (negate) {
            return -value;
        } else {
            return value;
        }
    }
}

Related

  1. dateTime2str(java.sql.Date date)
  2. dateTimeFormat(Date date,String pattern)
  3. DateTimeSpace(String starttime, String endtime)
  4. dateTimeToStr(java.util.Date date)
  5. daytime()
  6. double2Time(Number zahl)
  7. equalsTime(Time time1, Time time2)
  8. extractTime(Date date)
  9. getCalTime(Time startTime, Calendar cal)