将一天添加到一个日历变量会影响其他变量
Add one Day to one Calendar variable affects others
我只需执行以下操作:
Calendar calDate = startEntryRoutine.getCalStartOfPeriod();
startEntryRoutine
是一个已经完成的线程,现在只提供 getter 方法。
.getCalStartOfPeriod()
returns 日历变量。
现在当我做的时候;
Log.d(TAG, "createExampleList: " + startEntryRoutine.getCalStartOfPeriod().get(Calendar.DAY_OF_MONTH)+"."+startEntryRoutine.getCalStartOfPeriod().get(Calendar.MONTH)+"."+startEntryRoutine.getCalStartOfPeriod().get(Calendar.YEAR));
calDate.add(Calendar.DAY_OF_MONTH, +1);
Log.d(TAG, "createExampleList: " + calDate.get(Calendar.DAY_OF_MONTH)+"."+calDate.get(Calendar.MONTH)+"."+calDate.get(Calendar.YEAR));
Log.d(TAG, "createExampleList: " + startEntryRoutine.getCalStartOfPeriod().get(Calendar.DAY_OF_MONTH)+"."+startEntryRoutine.getCalStartOfPeriod().get(Calendar.MONTH)+"."+startEntryRoutine.getCalStartOfPeriod().get(Calendar.YEAR));
结果是:
createExampleList: 16.6.2019
createExampleList: 17.6.2019
createExampleList: 17.6.2019
但是为什么呢?
发生这种情况是因为 Java 是通过引用传递的。你需要创建一个日历对象的副本来解决这个问题。
Calendar cal2 = (Calendar) cal.clone();
然后进行更改。
我只需执行以下操作:
Calendar calDate = startEntryRoutine.getCalStartOfPeriod();
startEntryRoutine
是一个已经完成的线程,现在只提供 getter 方法。
.getCalStartOfPeriod()
returns 日历变量。
现在当我做的时候;
Log.d(TAG, "createExampleList: " + startEntryRoutine.getCalStartOfPeriod().get(Calendar.DAY_OF_MONTH)+"."+startEntryRoutine.getCalStartOfPeriod().get(Calendar.MONTH)+"."+startEntryRoutine.getCalStartOfPeriod().get(Calendar.YEAR));
calDate.add(Calendar.DAY_OF_MONTH, +1);
Log.d(TAG, "createExampleList: " + calDate.get(Calendar.DAY_OF_MONTH)+"."+calDate.get(Calendar.MONTH)+"."+calDate.get(Calendar.YEAR));
Log.d(TAG, "createExampleList: " + startEntryRoutine.getCalStartOfPeriod().get(Calendar.DAY_OF_MONTH)+"."+startEntryRoutine.getCalStartOfPeriod().get(Calendar.MONTH)+"."+startEntryRoutine.getCalStartOfPeriod().get(Calendar.YEAR));
结果是:
createExampleList: 16.6.2019
createExampleList: 17.6.2019
createExampleList: 17.6.2019
但是为什么呢?
发生这种情况是因为 Java 是通过引用传递的。你需要创建一个日历对象的副本来解决这个问题。
Calendar cal2 = (Calendar) cal.clone();
然后进行更改。