Factory to construct a FineTime out of many kinds of objects Any suggestions to help refactor this method? I thought maybe some kind of factory to allocate the right type?

public static FineTime getFineTime(Object o) {
    if (o == null) {
        return null;
    }
    //FineTime
    if (o instanceof FineTime) {
        return (FineTime ) o;
    }
    //Long
    if (o instanceof Long) {
        return new FineTime((Long ) o);
    }
    //Int
    if (o instanceof Integer) {
        return new FineTime((Integer ) o);
    }
    //Date
    if (o instanceof Date) {
        try {
            return DateConverter.dateToFineTime((Date ) o);
        } catch (IllegalArgumentException iae) {
            //Try another converter
            LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(Date) failed", iae);
        }
    }
    //String
    if (o instanceof String) {
        String date = (String ) o;
        //TAI
        try {
            return _stfFormatTai.parse(date);
        } catch (IllegalArgumentException iae) {
            //Try another converter
            LOGGER.log(Level.FINE, "_stfFormatTai.parse(String) failed", iae);
        }
        //UTC
        try {
            return _stfFormatUtc.parse(date);
        } catch (IllegalArgumentException iae) {
            //Try another converter
            LOGGER.log(Level.FINE, "_stfFormatUtc.parse(String) failed", iae);
        }
        //dd-MMM-yyyy HH:mm:ss
        try {
            return DateConverter.dateToFineTime(_sdfFormat1.parse(date));
        } catch (ParseException pe) {
            //Try another converter
            LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(_sdfFormat1.parse(String) failed", pe);
        } catch (IllegalArgumentException iae) {
            //Try another converter
            LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(_sdfFormat1.parse(String) failed", iae);
        }
        //dd-MM-yyyy HH:mm:ss
        try {
            return DateConverter.dateToFineTime(_sdfFormat2.parse(date));
        } catch (ParseException pe) {
            //Try another coverter
            LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(_sdfFormat2.parse(String) failed", pe);
        } catch (IllegalArgumentException iae) {
            //Try another converter
            LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(_sdfFormat2.parse(String) failed", iae);
        }
    }
    LOGGER.info("Could not convert " + StringUtil.asString(o) + " to FineTime");
    //No converter found, return 'null'
    return null;
}
Overloading!

public static FineTime getFineTime(Long o){
    if(o == null){ return null; }
    return new FineTime(o);
}

public static FineTime getFineTime(Integer o){
    if(o == null){ return null; }
    return new FineTime(o);
}

public static FineTime getFineTime(FineTime o){
    return o;
}

public static FineTime getFineTime(Date o){
    if(o == null){ return null; }
    try {
        return DateConverter.dateToFineTime(o);
    } catch (IllegalArgumentException iae) {
        //Try another converter
        LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(Date) failed", iae);
        return null;
    }
}

public static FineTime getFineTime(String date){
    if(date == null){ return null; }
    //TAI
    try {
        return _stfFormatTai.parse(date);
    } catch (IllegalArgumentException iae) {
        //Try another converter
        LOGGER.log(Level.FINE, "_stfFormatTai.parse(String) failed", iae);
    }
    //UTC
    try {
        return _stfFormatUtc.parse(date);
    } catch (IllegalArgumentException iae) {
        //Try another converter
        LOGGER.log(Level.FINE, "_stfFormatUtc.parse(String) failed", iae);
    }
    //dd-MMM-yyyy HH:mm:ss
    try {
        return DateConverter.dateToFineTime(_sdfFormat1.parse(date));
    } catch (ParseException pe) {
        //Try another converter
        LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(_sdfFormat1.parse(String) failed", pe);
    } catch (IllegalArgumentException iae) {
        //Try another converter
        LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(_sdfFormat1.parse(String) failed", iae);
    }
    //dd-MM-yyyy HH:mm:ss
    try {
        return DateConverter.dateToFineTime(_sdfFormat2.parse(date));
    } catch (ParseException pe) {
        //Try another coverter
        LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(_sdfFormat2.parse(String) failed", pe);
    } catch (IllegalArgumentException iae) {
        //Try another converter
        LOGGER.log(Level.FINE, "DateConverter.dateToFineTime(_sdfFormat2.parse(String) failed", iae);
    }
    return null;
}


And a default implementation

public static FineTime getFineTime(Object o){
    LOGGER.info("Could not convert " + StringUtil.asString(o) + " to FineTime");
    //No converter found, return 'null'
    return null;
}


And gone are most of your troubles.

The problem in this case, however, is that you do need to know what type your object has. You cannot "just" insert an object and get a value out. To do so anyway seems to imply bad design.

If you wanted to go that direction anyway, consider making a map of Class, Converter of sorts.

Something else you could do is leave the method you have right now as is, and just wrap your String and Date conversions in a separate function. That would significantly simplify the method as well, boiling down to "if integer, finetime via integer, if long, finetime via long, if string, finetime via string, if date, finetime via date, else log error and return null".