我是一个使用java.util.Calendar对象的应用程序。我在日历对象上进行多次设置,也基于某些条件。但是,在它结束时,所有最后计算的日期/时间值看起来都不同。然后我想出调用set方法不会计算日历对象上的值,直到你调用get方法。请参阅下面的Javadocs
获取和设置日历字段值
可以通过调用set方法来设置日历字段值。在需要计算其时间值(Epoch的毫秒数)或日历字段的值之前,不会解释日历中设置的任何字段值。调用get,getTimeInMillis,getTime,add和roll涉及这样的计算。
所以我的问题是,如果在设置下一个值/常量之前调用getters之前,Calendar对象上是否有任何设置值的模式?
如果删除System.out语句,下面的代码会有所不同。
声明 - > Calendar computeTime,int skipCycle,int [] days,int dayOfMonth,int weekOfMonth,int monthInYear,int hourToFire
computeTime.add( Calendar.YEAR, skipCycle );
computeTime.set( Calendar.MONTH, monthInYear );
System.out.println( "getcomputeTime:2 " + computeTime.getTime() );
computeTime.set( Calendar.DAY_OF_MONTH, 1 );
System.out.println( "getcomputeTime:3 " + computeTime.getTime() );
computeTime.set( Calendar.HOUR_OF_DAY, 00 );
System.out.println( "getcomputeTime:4 " + computeTime.getTime() );
computeTime.set( Calendar.MINUTE, 00 );
System.out.println( "getcomputeTime:5 " + computeTime.getTime() );
computeTime.set( Calendar.SECOND, 00 );
System.out.println( "getcomputeTime:6 " + computeTime.getTime() );
int maxValue = computeTime.getActualMaximum(Calendar.WEEK_OF_MONTH);
weekOfMonth = weekOfMonth < maxValue ? weekOfMonth : maxValue;
computeTime.set( Calendar.WEEK_OF_MONTH, weekOfMonth );
System.out.println( "getcomputeTime:7 " + computeTime.getTime() );
if( computeTime.get( Calendar.DAY_OF_WEEK ) > days[ 0 ] )
computeTime.set( Calendar.WEEK_OF_MONTH, weekOfMonth + 1 );
System.out.println( "getcomputeTime:8 " + computeTime.getTime() );
computeTime.set( Calendar.DAY_OF_WEEK, days[ 0 ] );
computeTime.add( Calendar.HOUR_OF_DAY, hourToFire );
System.out.println( "getcomputeTime:9 " + computeTime.getTime() );