Here you can find the source of toMilliseconds(Object value)
Parameter | Description |
---|---|
value | The value |
public static long toMilliseconds(Object value)
//package com.java2s; /**//w ww . j a v a2 s .co m * Copyright 2009-2016 Three Crickets LLC. * <p> * The contents of this file are subject to the terms of the LGPL version 3.0: * http://www.gnu.org/copyleft/lesser.html * <p> * Alternatively, you can obtain a royalty free commercial license with less * limitations, transferable or non-transferable, directly from Three Crickets * at http://threecrickets.com/ */ public class Main { /** * Converts a string to a milliseconds long, interpreting 'ms', 's', 'm', * 'h' and 'd' suffixes. Numbers are simply rounded to an integer. * <p> * Fractions can be used, and are rounded to the nearest millisecond, for * example: "1.5d". * * @param value * The value * @return The milliseconds */ public static long toMilliseconds(Object value) { if (value == null) return 0L; if (value instanceof Number) return ((Number) value).longValue(); else { String s = value.toString(); if (s.endsWith("ms")) return (long) Float.parseFloat(s.substring(0, s.length() - 2)); else if (s.endsWith("s")) return (long) (Float.parseFloat(s.substring(0, s.length() - 1)) * 1000f); else if (s.endsWith("m")) return (long) (Float.parseFloat(s.substring(0, s.length() - 1)) * 60000f); else if (s.endsWith("h")) return (long) (Float.parseFloat(s.substring(0, s.length() - 1)) * 3600000f); else if (s.endsWith("d")) return (long) (Float.parseFloat(s.substring(0, s.length() - 1)) * 86400000f); else return (long) Float.parseFloat(s); } } }