IDmikael IDmikael - 1 year ago 103
Android Question

problems with assignment Calendar values

i have such problem:

i have Calendar object in Main activity:

public static Calendar today = Calendar.getInstance();

in my second activity i need to set several alarms, so i did next:

for(int i = 0; i < alarmCounter; i++){
Calendar calendar =;

calendar.set(Calendar.HOUR_OF_DAY, alarmHours.get(i));
calendar.set(Calendar.MINUTE, alarmMinutes.get(i));
calendar.set(Calendar.SECOND, 0);

if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 7);

but when if condition is satisfied and 7 days are added to
, these 7 days are also added to
. Why it happens? How can i solve it?

It's important for me to save values of
because i change it's values in another method and it's not always equals current date.

Answer Source

This is a memory pointer problem.

Both calendar and are pointing at the same Calendar istance.

If you want to keep them separated you can use the clone() method:

Calendar calendar = (Calendar); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download