Here you can find the source of timeToLong(String inputStr)
Parameter | Description |
---|---|
inputStr | a parameter |
public static long timeToLong(String inputStr)
//package com.java2s; //License from project: Apache License public class Main { public static final long ONE_WEEK_IN_MILISECONDS = 604800000L; public static final long ONE_DAY_IN_MILISECONDS = 86400000L; public static final long ONE_HOUR_IN_MILISECONDS = 3600000L; public static final long ONE_MINUTE_IN_MILISECONDS = 60000L; public static final long ONE_SECOND_IN_MILISECONDS = 1000L; /**// w w w . j a va 2 s . c om * * @param inputStr * @return */ public static long timeToLong(String inputStr) { String newStr = inputStr.replaceAll(" ", ""); // Remove all whitespaces long time = 0; int weekIndex2; int dayIndex2; int hourIndex2; int minuteIndex2; int secondIndex2; if (newStr.indexOf('w') != -1) { boolean foundChar = false; int tempIndex = weekIndex2 = newStr.indexOf('w'); while (tempIndex > 0 && !foundChar) { tempIndex--; if (!Character.isDigit(newStr.charAt(tempIndex))) { tempIndex++; break; } } time += Integer.parseInt(newStr .substring(tempIndex, weekIndex2)) * ONE_WEEK_IN_MILISECONDS; } if (newStr.indexOf('d') != -1) { boolean foundChar = false; int tempIndex = dayIndex2 = newStr.indexOf('d'); while (tempIndex > 0 && !foundChar) { tempIndex--; if (!Character.isDigit(newStr.charAt(tempIndex))) { tempIndex++; break; } } time += Integer .parseInt(newStr.substring(tempIndex, dayIndex2)) * ONE_DAY_IN_MILISECONDS; } if (newStr.indexOf('h') != -1) { boolean foundChar = false; int tempIndex = hourIndex2 = newStr.indexOf('h'); while (tempIndex > 0 && !foundChar) { tempIndex--; if (!Character.isDigit(newStr.charAt(tempIndex))) { tempIndex++; break; } } time += Integer.parseInt(newStr .substring(tempIndex, hourIndex2)) * ONE_HOUR_IN_MILISECONDS; } if (newStr.indexOf('m') != -1) { boolean foundChar = false; int tempIndex = minuteIndex2 = newStr.indexOf('m'); while (tempIndex > 0 && !foundChar) { tempIndex--; if (!Character.isDigit(newStr.charAt(tempIndex))) { tempIndex++; break; } } time += Integer.parseInt(newStr.substring(tempIndex, minuteIndex2)) * ONE_MINUTE_IN_MILISECONDS; } if (newStr.indexOf('s') != -1) { boolean foundChar = false; int tempIndex = secondIndex2 = newStr.indexOf('s'); while (tempIndex > 0 && !foundChar) { tempIndex--; if (!Character.isDigit(newStr.charAt(tempIndex))) { tempIndex++; break; } } time += Integer.parseInt(newStr.substring(tempIndex, secondIndex2)) * ONE_SECOND_IN_MILISECONDS; } return time; } }