如何将 XMLGregorianCalendar 重置为新值
我每天要处理数百万次的日期工作。我创建了一个 XMLGregorianCalendar 来处理来自 XML 提要的日期,如下所示:
XMLGregorianCalendar xCalEst = null;
xCalEst = DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-08-09T21:50:00Z");
日期字符串将来自另一个源。因为我需要经常这样做,所以出于性能原因,我想,我应该将日历创建为静态的,并且只创建一次。问题是没有(简单)方法可以使用新的传入日期字符串重置日历。
IE:我希望能够做的是: xCalEst.reset("2011-08-09T21:55:00Z");
我是否过于关心性能并且每次都让对象被创建和销毁,或者是否有一种简单的方法可以做到这一点?
请注意,我是一名老 C 程序员,刚刚开始接触 Java。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未尝试过,但是像
setTime
和setTimeZone< /code>
可用。仅当您按顺序发送消息(例如在一个线程中)时,此方法才有效。由于 Java(与 C 不同)是一种面向对象的语言,其自然行为是创建和使用对象。您应该首先证明
XMLGregorianCalendar
的顺序创建是性能/内存瓶颈,然后尝试优化。一般来说,不要低估垃圾收集的力量:-)根据您的示例,您应该首先考虑“缓存”DataTypeFactory:
I did never try it, but methods like
setTime
andsetTimeZone
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 ofXMLGregorianCalendar
'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: