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);
}
Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • LinkedIn
  • Slashdot
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • email
  • Twitter
  • FriendFeed

Related posts:

  1. ADD_MONTH vs add(Calendar.MONTH)
  2. Ruby on Rails experience of a java developer
  3. Runtime Java Stack
  4. More Runtime Java Stack
  5. Java7 Wish: Object/Bean Factory

    • 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.