java.util.Calendar.getInstance() // danger

An icon from gnome-themes-extras-0.9.0.tar.bz2...Image via Wikipedia

Do not use the method for in your frequently used methods.
instead of creating new instances use

Calendar.setTime(DATE)

otherwise each instance use lots of memory: 432 Bytes

UPDATED:

DO NOT USE LIKE THIS

while(condition){
Calendar calendar = Calendar.getInstance();
calendar.setTime(someDate)
int month = calendar.get(Calendar.MONTH);
}

USE LIKE THIS

Calendar calendar = Calendar.getInstance();
while(condition){
calendar.setTime(someDate)
int month = calendar.get(Calendar.MONTH);
}

Related Posts

  1. ADD_MONTH vs add(Calendar.MONTH)
  2. Runtime Java Stack
  3. Which java stack do you use ?
    • Stephen Colebourne
    • December 9th, 2008

    You might want to try Joda-Time – http://joda-time.sourceforge.net

  1. hi stephen,
    I wish I could use .but this is one of the old project with “do not touch” title

  2. Just make sure you don’t use the Calendar across threads, or synchronize its use.

  3. @Brian
    All calendar instance were local in a method,

  1. No trackbacks yet.