本帖最后由 coolmiao13 于 2015-2-25 00:07 编辑
今天看完毕老师的关于时间类的视频,
自己动手写了一下关于取得范围时间段营业日的程序,发现死循环了,求助。
问题已解决!
问题代码:
- package ch01;
- import java.util.Calendar;
- /*需求:获取任意一年的二月的天数
- *需求2:获取前一天现在的时刻。
- *需求3:获取范围时间的营业日(不算周末)
- * */
- public class dateDemo {
- public static void main (String[]args){
- //MyDateOf2M(2015);
- //MyDateBeforeDay();
- int a = myDate(2015,1,13,2015,2,15);
- System.out.println(a);
- }
- //需求1
- public static void MyDateOf2M(int year){
- Calendar c = Calendar.getInstance();
- c.set(year,2,1 );
- c.add(Calendar.DAY_OF_MONTH, -1);
- System.out.println(c.get(Calendar.DAY_OF_MONTH));
- }
- //需求2
- public static void MyDateBeforeDay(){
- Calendar c = Calendar.getInstance();
- //c.add(Calendar.DAY_OF_MONTH, -1);
- System.out.println(c.get(Calendar.YEAR)+"年"+
- (c.get(Calendar.MONTH)+1)+"月"+
- c.get(Calendar.DATE)+"日"+
- c.get(Calendar.HOUR_OF_DAY)+":"+
- c.get(Calendar.MINUTE)+":"+
- c.get(Calendar.SECOND));
- }
- //需求3
- //获取范围时间的营业日(不算周末)
- public static int myDate(int ... date){
- //处理传入的参数
- if (!(date.length==6)){
- throw new ArrayIndexOutOfBoundsException("输入的时间不对");
- }
- Calendar c = Calendar.getInstance();
- //月份减1,输入的月份与电脑的月份有区别。
- date[1]--;
- date[4]--;
- //将头时间设置进Calendar对象
- c.set(date[0], date[1],date[2]);
- //取得年月日时间
- int year = c.get(Calendar.YEAR);
- int mon = c.get(Calendar.MONTH);
- int day = c.get(Calendar.DAY_OF_MONTH);
- int count = 0;
- //当前后参数时间相等跳出循环
- while (!((year==date[3])&&(mon==date[4])&&(day==date[5]))){
- //周六日增加天数,不增加count数
- if((c.get(Calendar.WEDNESDAY)==6)||
- (c.get(Calendar.WEDNESDAY)==1)){
- c.add(Calendar.DAY_OF_MONTH, 1);
- continue;
- }
- //增加天数和count数
- c.add(Calendar.DAY_OF_MONTH, 1);
- count++;
- }
- //返回统计的时间
- return count;
- }
- //方便调试写的输入当前Calendar 对象的时间。
- public static void printDate(Calendar c){
- System.out.println(c.get(Calendar.YEAR)+"年"+
- (c.get(Calendar.MONTH)+1)+"月"+
- c.get(Calendar.DATE)+"日"+
- c.get(Calendar.HOUR_OF_DAY)+":"+
- c.get(Calendar.MINUTE)+":"+
- c.get(Calendar.SECOND));
- }
- }
复制代码 经过半个小时的努力,问题解决:
代码:- package ch01;
- import java.util.Calendar;
- /*需求:获取任意一年的二月的天数
- *需求2:获取前一天现在的时刻。
- *需求3:获取范围时间的营业日(不算周末)
- * */
- public class dateDemo {
- public static void main (String[]args){
- //MyDateOf2M(2015);
- //MyDateBeforeDay();
- int a = myDate(2015,1,13,2015,1,18);
- System.out.println(a);
- }
- //需求1
- public static void MyDateOf2M(int year){
- Calendar c = Calendar.getInstance();
- c.set(year,2,1 );
- c.add(Calendar.DAY_OF_MONTH, -1);
- System.out.println(c.get(Calendar.DAY_OF_MONTH));
- }
- //需求2
- public static void MyDateBeforeDay(){
- Calendar c = Calendar.getInstance();
- //c.add(Calendar.DAY_OF_MONTH, -1);
- System.out.println(c.get(Calendar.YEAR)+"年"+
- (c.get(Calendar.MONTH)+1)+"月"+
- c.get(Calendar.DATE)+"日"+
- c.get(Calendar.HOUR_OF_DAY)+":"+
- c.get(Calendar.MINUTE)+":"+
- c.get(Calendar.SECOND));
- }
- //需求3
- //获取范围时间的营业日(不算周末)
-
- public static int myDate(int ...date){
- //处理传入的参数
- if (!(date.length==6)){
- throw new ArrayIndexOutOfBoundsException("输入的时间不对");
- }
- Calendar c = Calendar.getInstance();
- //月份减1,输入的月份与电脑的月份有区别。
- date[1]--;
- date[4]--;
- //将头时间设置进Calendar对象
- c.set(date[0], date[1],date[2]);
- int count =0;
- //当前后参数时间相等跳出循环
- while (!((c.get(Calendar.YEAR)==date[3])&&
- (c.get(Calendar.MONTH)==date[4])&&
- (c.get(Calendar.DAY_OF_MONTH)==date[5]))){
- //周六日增加天数,不增加count数
- if((c.get(Calendar.WEDNESDAY)==6)||
- (c.get(Calendar.WEDNESDAY)==1)){
- c.add(Calendar.DAY_OF_MONTH, 1);
- continue;
- }
- //增加天数和count数
- c.add(Calendar.DAY_OF_MONTH, 1);
- count++;
- }
- //返回统计的时间
- return count;
- }
- public static void printDate(Calendar c){
- System.out.println(c.get(Calendar.YEAR)+"年"+
- (c.get(Calendar.MONTH)+1)+"月"+
- c.get(Calendar.DATE)+"日"+
- c.get(Calendar.HOUR_OF_DAY)+":"+
- c.get(Calendar.MINUTE)+":"+
- c.get(Calendar.SECOND));
- }
- }
复制代码 因为在取得year,mon,day变数的时候是初始化的状态,放到循环中并不会因为定义而进行改变。
只能用老长的Calendar函数来写进循环条件,问题搞定。
|