Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how efficiently initialize Calendar object

807607Nov 13 2006 — edited Nov 15 2006
How 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?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 13 2006
Added on Nov 13 2006
14 comments
2,915 views