Here you can find the source of toLong(String time)
Parameter | Description |
---|---|
time | String with formatted time stamp; |
public static long toLong(String time)
//package com.java2s; //License from project: Apache License public class Main { /**/*w w w. j a v a 2s .c o m*/ * Parses given line for formatted time values and saves the result into given variable * * @param time String with formatted time stamp; * @return parsed time as <b>Long</b> in case of successful parse, <i>-1</i> otherwise. */ public static long toLong(String time) { return toLong(time, 0); } /** * Parses given line for formatted time values and saves the result into given variable * * @param time String with formatted time stamp; * @param defaultTime Default time value; * @return parsed time as <b>Long</b> in case of successful parse, <i>-1</i> otherwise. */ public static long toLong(String time, long defaultTime) { double parsedTime = 0.0; long result = 0; time = time.trim(); boolean exc = false; try { parsedTime = Long.parseLong(time); result = Long.parseLong(time); } catch (Throwable t1) { try { String ch = time.substring(time.length() - 1); String timeToParse = time.substring(0, time.length() - 1); try { parsedTime = Long.parseLong(timeToParse); } catch (Throwable t) { parsedTime = Double.parseDouble(timeToParse); } if (ch.equals("s")) { try { result = (long) (1000.0 * parsedTime); } catch (Throwable t2) { exc = true; } } else if (ch.equals("m")) { try { result = (long) (60000.0 * parsedTime); } catch (Throwable t2) { exc = true; } } else if (ch.equals("h")) { try { result = (long) (3600000.0 * parsedTime); } catch (Throwable t2) { exc = true; } } else if (ch.equals("d")) { try { result = (long) (3600000.0 * 24.0 * parsedTime); } catch (Throwable t2) { exc = true; } } else if (ch.equals("w")) { try { result = (long) (3600000.0 * 24.0 * 7.0 * parsedTime); } catch (Throwable t2) { exc = true; } } else { // Report.error(TimeUtil.class, String.format( // "Unknown character received while parsing time line. Cannot parse argument '%s' in line '%s'. Time was set to milliseconds", ch, // time)); return (long) parsedTime; } } catch (Throwable t2) { exc = true; } } if (exc) { return defaultTime; } else { if (result < 0) { // Report.error(TimeUtil.class, String.format("Cannot be negative. Value '%s' was sent instead of '%s'", parsedTime, time)); // return (long) parsedTime; return defaultTime; } else { return result; } } } }