Quickest way to clone a GregorianCalendar?

北城以北 提交于 2019-11-28 11:53:34

java.util.Calendar has overridden clone() which is working, so use it. Furthermore, Calendar doesn't have deep data hierarchy - its data are mainly ints.

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

Specifically, the quickest line of code to copy a Calendar is:

GregorianCalendar newCalendar = (Calendar)(oldCalendar.clone());

Uh, clone() sucks.

Is it that hard? You only have to set 3 things, I believe, the time, the time zone, and the locale. All those fields have getters and setters. Make a quite utility method to return a copy?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!