克隆GregorianCalendar的最快方法? [英] Quickest way to clone a GregorianCalendar?
问题描述
我试图制作一个对象的深层副本,包括一个 GregorianCalendar
实例。我总是警惕使用 clone()
,它似乎没有被覆盖在这里,所以我只是做字段的复制字段。理想情况下,有一个复制构造函数,我可以这样使用:
I'm trying to make a deep copy of an object, including a GregorianCalendar
instance. I'm always wary of using clone()
and it doesn't seem to have been overridden here, so I'm just doing the copy field by field. Ideally, there'd be a copy constructor, which I could use like so:
GregorianCalendar newCalendar = new GregorianCalendar(oldCalendar);
不幸的是,我在API中找不到任何这样的功能,我需要得到一个确切的副本。所以,要制作这些日历之一的副本,你会怎么做?我在这里缺少一些简单的快捷键?
Unfortunately I can't find any such functionality in the API and am stuck trying to figure out which fields I need to get an exact copy. So, to make a copy of one of these calendars, how would you do it? Am I missing some simple shortcut here?
推荐答案
java.util.Calendar
已覆盖 clone()
这是工作,所以使用它。此外,日历
没有深层数据层次结构 - 其数据主要是 int
s。
java.util.Calendar
has overridden clone()
which is working, so use it. Furthermore, Calendar
doesn't have deep data hierarchy - its data are mainly int
s.
要扩展答案,可以调用 SerializationUtils.clone(..)
(从 commons-lang )对任何对象进行深层复制,如果整个数据层次实现 Serializable
To extend the answer, you can call SerializationUtils.clone(..)
(from commons-lang) on any object which makes a deep copy, if the whole data hierarchy implements Serializable
这篇关于克隆GregorianCalendar的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!