how efficiently initialize Calendar object
807607Nov 13 2006 — edited Nov 15 2006How can we efficiently initialize a Calendar object to a specific date, using a java.util.Date object?
If I use:
// assume dMyDate is a java.util.Date object.
cal = Calendar.getInstance();
cal.setTime(dMyDate);
it seems inefficient, because the getInstance() method does unnecessary work initializing the calendar to the current date-time. The same happens with:
cal = new GregorianCalendar();
What we need is:
cal = new GregorianCalendar(dMyDate); // not legal!!!
or a raw constructor that does no initialization at all.
I thought maybe initializing the calendar with integers would be more efficient, assuming simple assignments were done:
cal = new GregorianCalendar(0, 0, 0);
but it is hard to tell. I believe the Calendar object does some arithmetic and logic checks to validate the date and adjust it for epoch, etc.
What is the most efficient way to initialize a Calendar object from a Date object?