/** tries to convert given object to a Duration, parsing strings, treating numbers as millis, etc; * throws IAE if not convertable */ public static Duration of(Object o) { if (o==null) return null; if (o instanceof Duration) return (Duration)o; if (o instanceof String) return parse((String)o); if (o instanceof Number) return millis((Number)o); try { // this allows it to work with groovy TimeDuration Method millisMethod = o.getClass().getMethod("toMilliseconds"); return millis((Long)millisMethod.invoke(o)); } catch (Exception e) { // probably no such method } throw new IllegalArgumentException("Cannot convert "+o+" (type "+o.getClass()+") to a duration"); }