I wanted to add more methods to the Calendar from java.util.Calendar, but I’m having trouble using the getInstance method.
If I have an object like:
public class DateObject extends Calendar{ // other methods here }
And I do DateObject mon = DateObject.getInstance();
The code does not work. Even if I replace it with Calendar.getInstance(), I can’t convert a Calendar to my DateObject.
How do I make my DateObject use getInstance()?
getInstance()is a static method onCalendar. You cannot override static methods. You will need to provide your own method.But I would question the design of extending Calendar. You will almost certainly be better served by having your class wrap a Calendar rather than extend it. In particular, changing your calendar implementation will become very, very difficult.
Consider also, that the Calendar API is very broken in a number of respects, so perpetuating it would be a crime against baby kittens.
Have you considered an alternative date/time/calendar library which might already contain the functionality you want? For example JodaTime.