在Java中创建日历对象的临时副本 [英] Creating a temporary copy of a Calendar Object in Java
问题描述
我需要弄清楚如何创建一个临时日历对象(一个永久日历已经存在的副本),以便我可以操纵副本:tempCal.add(unit,value)。我需要保持原来的日历对象不变,所以我真的不想直接调用add(unit,value)。
I need to figure out how to create a temporary calendar object (a copy of a "permanent" calendar that already exists) so that I can manipulate the copy: tempCal.add (unit, value). I need to keep the original calendar object unchanged, so I really don't want to call add(unit, value) on it directly.
由于我尝试创建副本的尝试实际上没有工作,我当前丑陋的黑客是调用permanentCal.add(unit,value),显示所需的结果,然后调用permanentCal.add(unit,-value) - 看起来好像是unol。
Since none of my attempts at creating a copy actually worked, my current ugly hack is to call permanentCal.add(unit, value), display the desired results, then call permanentCal.add (unit, -value) -- which just seems, uncool.
推荐答案
util.Calendar
有一个 clone
方法,你可以使用它。
java.util.Calendar
has a clone
method, you could use that. All data in it is made of primitives, so you will not run into troubles.
请看看这些答案:
- Defensive copy of Calendar
- Quickest way to clone a GregorianCalendar?
这篇关于在Java中创建日历对象的临时副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!