Here I see two ways.
Create a DateUtils factory class that encapsulates the construction of Calendar and Date instances. Then you can change it to a managed factory. Ie, you have a second DateUtils implementation that also provides the setCurrentTime method, and then explicitly returns the Calendar instances set for that date.
Or you can use JMockit to actually βoverrideβ your call to new GregorianCalendar , an mock instance is returned instead. I did not do this myself, but there is a description here .
Your option, splitting the method into two methods, is also a perfectly valid option. I think this is what most people, including myself, do, but it actually harms your design, especially if the class is exposed to different code.
waxwing
source share