java 克隆 GregorianCalendar 的最快方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2499113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Quickest way to clone a GregorianCalendar?
提问by wds
I'm trying to make a deep copy of an object, including a GregorianCalendarinstance. 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实例。我总是对使用持谨慎态度,clone()它似乎没有在这里被覆盖,所以我只是逐个字段地进行复制。理想情况下,会有一个复制构造函数,我可以这样使用:
GregorianCalendar newCalendar = new GregorianCalendar(oldCalendar);
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?
不幸的是,我在 API 中找不到任何这样的功能,并且一直在试图找出我需要获取精确副本的字段。那么,要复制其中一个日历,您会怎么做?我在这里错过了一些简单的快捷方式吗?
回答by Bozho
java.util.Calendarhas overridden clone()which is working, so use it. Furthermore, Calendardoesn't have deep data hierarchy - its data are mainly ints.
java.util.Calendar已覆盖clone()哪个有效,因此请使用它。此外,Calendar没有很深的数据层次结构——它的数据主要是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
为了扩展答案,您可以SerializationUtils.clone(..)(从commons-lang)调用任何进行深度复制的对象,如果整个数据层次结构实现Serializable
回答by Everyone_Else
Specifically, the quickest line of code to copy a Calendar is:
具体来说,复制日历最快的代码行是:
GregorianCalendar newCalendar = (Calendar)(oldCalendar.clone());
回答by Basil Bourque
tl;dr
tl;博士
Use the modern java.timeclasses that supplanted GregorianCalendar.
使用现代的java.time类取代GregorianCalendar.
GregorianCalendar.from( // Convert from modern object to legacy class object.
myGregorianCalendar.toZonedDateTime() // Convert from legacy to modern.
) // Returns a new `GregorianCalendar` object.
But better to stop using GregorianCalendaraltogether, and just use ZonedDateTime.
但最好GregorianCalendar完全停止使用,而只使用ZonedDateTime.
ZonedDateTime
ZonedDateTime
The terrible GregorianCalendarclass was years ago supplanted by the java.timeclasses defined in JSR 310. Specifically replaced by the ZonedDateTimeclass.
可怕的GregorianCalendar类在几年前被JSR 310 中定义的java.time类取代。专门由ZonedDateTime类取代。
You can convert back and forth using new to…/from…methods added to the old classes.
您可以使用添加到旧类的新to…/from…方法来回转换。
ZonedDateTime zdt = myGregorianCalendar.toZonedDateTime() ;
Going the other direction.
走向另一个方向。
GregorianCalendar myGregorianCalendar = GregorianCalendar.from( zdt ) ;
To address your Question specifically, combine these two conversions to get another GregorianCalendarobject.
要专门解决您的问题,请将这两个转换结合起来以获取另一个GregorianCalendar对象。
GregorianCalendar gc = GregorianCalendar.from( myGregorianCalendar.toZonedDateTime() ) ;
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*classes.
您可以直接与数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Most of the java.timefunctionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.timeclasses.
- For earlier Android (<26), the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9、Java SE 10、Java SE 11及更高版本 - 标准 Java API 的一部分,具有捆绑实现。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 大多数java.time功能在ThreeTen-Backport 中被反向移植到 Java 6 & 7 。
- 安卓
- 更高版本的 Android 捆绑实现java.time类。
- 对于早期的 Android(<26),ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval,YearWeek,YearQuarter,和更多。
回答by piyo
Uh, clone()sucks.
呃,clone()糟透了。
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?
有那么难吗?你只需要设置三件事,我相信,时间、时区和语言环境。所有这些字段都有 getter 和 setter。制作一个非常实用的方法来返回副本?

