Java Second Parse parseSeconds(String value, int defaultValue)

Here you can find the source of parseSeconds(String value, int defaultValue)

Description

Returns value as a positive integer, or 0 if it is negative, or defaultValue if it cannot be parsed.

License

Apache License

Declaration

public static int parseSeconds(String value, int defaultValue) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from   ww w .ja  va 2s  .  c  o m*/
     * Returns {@code value} as a positive integer, or 0 if it is negative, or
     * {@code defaultValue} if it cannot be parsed.
     */
    public static int parseSeconds(String value, int defaultValue) {
        try {
            long seconds = Long.parseLong(value);
            if (seconds > Integer.MAX_VALUE) {
                return Integer.MAX_VALUE;
            } else if (seconds < 0) {
                return 0;
            } else {
                return (int) seconds;
            }
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
}

Related

  1. centisecondsPerTick(final int tps)
  2. parseHHMMSSToSeconds(String txt)
  3. parseTimeStringAsSeconds(String timeString)
  4. parseW3CDateWithFractionalSeconds(String dateString)