克隆 GregorianCalendar 的最快方法是什么?

Quickest way to clone a GregorianCalendar?(克隆 GregorianCalendar 的最快方法是什么?)
本文介绍了克隆 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(),所以使用它.此外,Calendar 没有很深的数据层次——它的数据主要是 ints.

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

要扩展答案,您可以调用 SerializationUtils.clone(...) (来自 Apache commons-lang)在任何进行深拷贝的对象上,如果整个数据层次结构实现 Serializable.

To extend the answer, you can call SerializationUtils.clone(…) (from Apache commons-lang) on any object which makes a deep copy, if the whole data hierarchy implements Serializable.

这篇关于克隆 GregorianCalendar 的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Month is not printed from a date - Java DateFormat(月份不是从日期打印的 - Java DateFormat)
java get week of year for given a date(java获取给定日期的一年中的一周)
Get the number of days, weeks, and months, since Epoch in Java(获取自 Java 中的 Epoch 以来的天数、周数和月数)
Is there a good way to get the date of the coming Wednesday?(有什么好方法可以得到即将到来的星期三的日期吗?)
Calendar view for Android GingerBread and before (APIlt;11)(Android GingerBread 及之前的日历视图 (API11))
How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和检索日期)