Java Regex Time Validate parseTime(String s)

Here you can find the source of parseTime(String s)

Description

parse Time

License

Open Source License

Declaration

public static long parseTime(String s) 

Method Source Code

//package com.java2s;
/*  This file is part of Catacombs.
    // www  .ja v a 2  s  .co m
Catacombs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
Catacombs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Catacombs.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author John Keay  <>(@Steeleyes, @Blockhead2)
 * @copyright Copyright (C) 2011
 * @license GNU GPL <http://www.gnu.org/licenses/>
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static long parseTime(String s) {
        Pattern p = Pattern.compile("(\\d+)([smhd])");
        Matcher m = p.matcher(s);
        long num = 0;
        while (m.find()) {
            int i = Integer.parseInt(m.group(1));
            String unit = m.group(2);
            //    System.out.println("[Catacombs]   parse "+unit+" "+i);

            if (unit.equals("m"))
                i = i * 60;
            if (unit.equals("h"))
                i = i * 60 * 60;
            if (unit.equals("d"))
                i = i * 60 * 60 * 24;
            num += i;
        }
        //System.out.println("[Catacombs] Parse "+s+" = "+num+"sec(s)");
        return num * 1000;
    }
}

Related

  1. parseTime(String str)
  2. parseTime(String t)
  3. parseTime(String value)
  4. parseTimeDifference(String input)