Creating a temporary copy of a Calendar object in Java

Tags: ,



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.

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:



Source: stackoverflow