Parses a time period into a long. Translates possible [msec|sec|min|h] suffixes : Date Parser « Data Type « Java






Parses a time period into a long. Translates possible [msec|sec|min|h] suffixes

     
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Map;


/*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */



public class Main{

  /** Millisecond conversion constants */
  private static final long MSEC = 1;
  private static final long SECS = 1000;
  private static final long MINS = 60 * 1000;
  private static final long HOUR = 60 * 60 * 1000;
  /**
   * Parses a time period into a long.
   *
   * Translates possible [msec|sec|min|h] suffixes
   *
   * For example:
   *   "1"      ->  1 (msec)
   *   "1msec   ->  1 (msec)
   *   "1sec"   ->  1000 (msecs)
   *   "1min"   ->  60000 (msecs)
   *   "1h"     ->  3600000 (msecs)
   * 
   * Accepts negative periods, e.g. "-1"
   * 
   * @param period the stringfied time period
   * @return the parsed time period as long
   * @throws NumberFormatException
   */
  public static long parseTimePeriod(String period)
  {
     try
     {
        String s = period.toLowerCase();
        long factor;
        
        // look for suffix
        if (s.endsWith("msec"))
        {
           s = s.substring(0, s.lastIndexOf("msec"));
           factor = MSEC;
        }
        else if (s.endsWith("sec"))
        {
           s = s.substring(0, s.lastIndexOf("sec"));
           factor = SECS;
        }
        else if (s.endsWith("min"))
        {
           s = s.substring(0, s.lastIndexOf("min"));
           factor = MINS;
        }
        else if (s.endsWith("h"))
        {
           s = s.substring(0, s.lastIndexOf("h"));
           factor = HOUR;
        }
        else
        {
           factor = 1;
        }  
        return Long.parseLong(s) * factor;
     }
     catch (RuntimeException e)
     {
        // thrown in addition when period is 'null'
        throw new NumberFormatException("For input time period: '" + period + "'");
     }
  }
  
  /**
   * Same like parseTimePeriod(), but guards for negative entries.
   * 
   * @param period the stringfied time period
   * @return the parsed time period as long
   * @throws NumberFormatException
   */
  public static long parsePositiveTimePeriod(String period)
  {
     long retval = parseTimePeriod(period);
     if (retval < 0)
     {
        throw new NumberFormatException("Negative input time period: '" + period + "'");
     }
     return retval;
  }


}

   
    
    
    
    
  








Related examples in the same category

1.Date parser for ISO 8601 format
2.Date Utils
3.Date To String
4.Perform date validations
5.ISO 8601 date parsing utility.
6.Parses a string representing a date by trying a variety of different parsers.
7.A method to get the current date as an array of components 0 = year, 1 = month, 2 = day
8.ISO 8601 date parsing utility. Designed for parsing the ISO subset used in Dublin Core, RSS 1.0, and Atom.
9.A simple XML parser that starts parsing right away and validates along the way.