Here you can find the source of getMillisFromHours(String hours)
Parameter | Description |
---|---|
hours | a parameter |
public static long getMillisFromHours(String hours)
//package com.java2s; //License from project: Open Source License public class Main { /**/* ww w . j a v a 2 s . c o m*/ * Edited this method by adding a nested try catch block that will parse input * when the use enters the time in ##h##m format, it will also handle any format * in ##H##M, ##h, ##H, ##m, ##M. In order to maintain original functionality, I * editied the catch block to perform the actions for parsing user input in ##h##m format. * In the case of invalid format, 0 is returned. * * @author Aaron Musengo * @param hours * @return long milliseconds */ public static long getMillisFromHours(String hours) { if (hours == null || hours.length() == 0) { return 0; } try { double numHours = Double.parseDouble(hours); if (numHours < 0.0) { return 0; } double millisDouble = (numHours * 3600 * 1000); return (long) millisDouble; } catch (NumberFormatException e) { String hour = new String(); String minute = new String(); String temp = new String(); for (int i = 0; i < hours.length(); i++) { if (hours.charAt(i) == 'h' || hours.charAt(i) == 'H') { hour = temp; temp = ""; } else if (hours.charAt(i) == 'm' || hours.charAt(i) == 'M') { minute = temp; temp = ""; } else if (hours.charAt(i) < '0' || hours.charAt(i) > '9') { return 0; } else { temp += hours.charAt(i); } } Double numHours = 0.0; Double numMinutes = 0.0; if (hour.length() != 0) { numHours = Double.parseDouble(hour); } else { hour = "0"; } if (minute.length() != 0) { numMinutes = Double.parseDouble(minute); numMinutes = numMinutes / 60.0; } numHours += numMinutes; double millisDouble = (numHours * 3600 * 1000); return (long) millisDouble; } } }