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.
The getInstance() method in Calendar class is used to get a calendar using the current time zone and locale of the system. Syntax: public static Calendar getInstance() Parameters: The method does not take any parameters. Return Value: The method returns the calendar.
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:
(Not for Android)
Switch to java 8's immutable LocalDateTime, the successor of Calendar/Date. This API is an immense improvement, may be a bit overwhelming at first.
There a method like minusDays(long)
will return a new date. Which makes for thread-safeness and all. For instance being able to share a value without fear of someone altering it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With