8

I have a function that sometimes has to return a 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;//Return DATETIME
    } else {
        return (T) temp.toDate();//Return DATE
    }
}

Is it the right approach?
How to use it?

like this (timerHelper is the name of class):

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());
}
7
  • 1
    Just curious: What happens if you return DateTime always? Commented Jun 19, 2015 at 13:34
  • No because in the code sometimes I need a Date other times a DateTime Commented Jun 19, 2015 at 13:36
  • 4
    Well, in those times when you need a date, call t.toDate(). Commented Jun 19, 2015 at 13:37
  • toReturn.getClass() will always throw a NullPointerException Commented Jun 19, 2015 at 13:37
  • 1
    you can just return DateTime always and manipulate it afterward to fit your needs, whats complicated about that! Commented Jun 19, 2015 at 13:37

3 Answers 3

6

I suspect you're being too clever trying to use generics here. Because you don't have polymorphism on return types doesn't mean you should resort to generics to try and achieve that effect.

You can implement this simply as two methods: public static Date convertToDateForServer(DateTime toSave) {...} and public static DateTime convertToDateTimeForServer(DateTime toSave) {...}. The calling code seems to know what it wants, so it can simply call the method needed. If there really is a complex commonality to both methods, make a private method that both can call internally.

Sign up to request clarification or add additional context in comments.

Comments

2

If Java 8 is available you could always implement an Either using the new Optional class.

Comments

2

This is one of the tricky areas of Generics. The only way to get this to work would be to take a Class argument, so the method knows what type of object to create. It can't know at the moment, because of Type Erasure.

Alternatively (much simpler) is to always return DateTime and do away with generics here.

The client will always know what it wants, and if the client wants a Date, it can create one from the DateTime far more easily than what you are trying to do.

Example:

Client 1 wants a DateTime:

DateTime result = service.convertTimeForServer(dt);

Client 2 wants a Date:

Date result = service.convertTimeForServer(dt).toDate();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.