Difference between revisions of "Find the number of minutes in a HH:MM:SS time string"

From CodeCodex

 
(Java)
Line 3: Line 3:
 
<pre>
 
<pre>
 
/**
 
/**
* Turns a period of time into the number of minutes containted there in (eg. 06:30:15 returns 360.25)
+
* Turns a period of time into the number of minutes represented
 +
        * (eg. 06:30:15 returns 360.25)
 
* @param hourFormat The string containing the hour format "HH:MM:SS"
 
* @param hourFormat The string containing the hour format "HH:MM:SS"
 
* @return The number of minutes represented, or -1 if the date could not be processed.
 
* @return The number of minutes represented, or -1 if the date could not be processed.

Revision as of 19:29, 14 September 2006

Implementations

Java

	/**
	 * Turns a period of time into the number of minutes represented
         * (eg. 06:30:15 returns 360.25)
	 * @param hourFormat The string containing the hour format "HH:MM:SS"
	 * @return The number of minutes represented, or -1 if the date could not be processed.
	 */
	public static double parseTimeToMinutes(String hourFormat) {
		
		double minutes = 0;
		String[] split = hourFormat.split(":");
		
		try {
			
			minutes += Double.valueOf(split[0])*60;
			minutes += Double.valueOf(split[1]);
			minutes += Double.valueOf(split[2])/60;
			return minutes;
		
		} catch (Exception e) {
			return -1;
		}
		
	}