I have a function that sometimes needs to return Date other times a DateTime (Joda-Time).
static public <T extends Object> T convertTimeForServer(DateTime toSave) { DateTime temp = null; try { temp = toSave.withZone(DateTimeZone.forID(getServerTimeZone())); } catch (Exception e) { } T toReturn = null; if (toReturn.getClass().equals(temp)) { return (T) temp;
Is this the right approach?
How to use it?
like this (timerHelper is the class name):
DateTime t = timerHelper.<DateTime>convertTimeForServer(new DateTime()); Date t2 = timerHelper.<Date>convertTimeForServer(new DateTime()); or DateTime t = (DateTime)timerHelper.convertTimeForServer(new DateTime()); Date t2 = (Date)timerHelper.convertTimeForServer(new DateTime());
And how to use this function instead?
static public <T extends Object> T current_Moment(){ return convertTimeForServer(new DateTime()); }
java generics
user72708
source share