0

I'm going to be doing some work with dates millions of times per day. I've created an XMLGregorianCalendar to handle the dates from an XML feed as such:

XMLGregorianCalendar xCalEst = null;
xCalEst = DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-08-09T21:50:00Z");

Where the date string will be coming from another source. Since I need to be doing this a lot I'm thinking, for performance reasons, I should create the calendar as static and just create it once. The problem is that there is no (easy) way to reset the calendar with a new incoming date string.

IE: What I would like to be able to do is something like: xCalEst.reset("2011-08-09T21:55:00Z");

Am I overly concerned with performance and just let the objects get created and destroyed each time or is there a simple way to do this?

Please note I'm an old C programmer and just starting out with Java.

home
  • 12,468
  • 5
  • 46
  • 54
Rod Meyer
  • 568
  • 4
  • 7

1 Answers1

0

I did never try it, but methods like setTime and setTimeZone are available. This approach will only work if you send the messages sequentially, e.g. in one thread. As Java is (unlike C) an object oriented language its natural behaviour is creating and working with objects. You should first prove that sequential creation of XMLGregorianCalendar's is a performance / memory bottleneck, then try to optimize. In general, do not underestimate the power of garbage collection :-)

Given your example you should first consider to 'cache' the DataTypeFactory:

DatatypeFactory dtf = DatatypeFactory.newInstance();
dtf.newXMLGregorianCalendar("2011-08-09T21:50:00Z");
home
  • 12,468
  • 5
  • 46
  • 54