public static long toLong(Object obj, final long longDefault) { if (obj instanceof Long) { return (Long) obj; } if (obj instanceof Number) { return ((Number) obj).longValue(); } else if (obj instanceof CharSequence) { String str = Str.toString(obj); if (Dates.isJsonDate(str)) { return Dates.fromJsonDate(str).getTime(); } try { return StringScanner.parseLong(str); } catch (Exception ex) { return longDefault; } } else if (obj instanceof Date) { return ((Date) obj).getTime(); } else { return toInt(obj); } }
public static int toInt(Object obj, int defaultValue) { if (obj.getClass() == int.class) { return int.class.cast(obj); } if (obj instanceof Number) { return ((Number) obj).intValue(); } else if (obj instanceof Boolean || obj.getClass() == Boolean.class) { boolean value = toBoolean(obj); return value ? 1 : 0; } else if (obj instanceof CharSequence) { try { return StringScanner.parseInt(Str.toString(obj)); } catch (Exception ex) { return defaultValue; } } return defaultValue; }
public static Date toEuroDate(String string) { String[] split = StringScanner.splitByChars(string, new char[] {'.', '\\', '/', ':'}); if (split.length == 3) { return Dates.getEuroDate(toInt(split[0]), toInt(split[1]), toInt(split[2])); } else if (split.length >= 6) { return Dates.getEuroDate( toInt(split[0]), toInt(split[1]), toInt(split[2]), toInt(split[3]), toInt(split[4]), toInt(split[5])); } else { die(String.format("Not able to parse %s into a Euro date", string)); return null; } }