Skip to content
Advertisement

Creating a temporary copy of a Calendar object in Java

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.

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.

Advertisement

Answer

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.

Have a look at these answers:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement