Topic: 大家看这个算法怎么写?(题目:String get(Date start_time, long time))

  Print this page

1.大家看这个算法怎么写?(题目:String get(Date start_time, long time)) Copy to clipboard
Posted by: pursuer2004
Posted on: 2005-07-20 17:25

题目:string get(Date start_time, long time)
//传进两个参数start_time起始时间yyyymmddhhmmss,time从开始时间的时长 单位秒s
计算在0点到7点的时长h1,和除此之外的时长h2
return string(h1)+" "+string(h2)


请使用准确的文字描述作为标题

Frankly I don't understand what this get method is for...
计算在0点到7点的时长h1??Question

2.Re:大家看这个算法怎么写?(题目:String get(Date start_time, long time)) [Re: pursuer2004] Copy to clipboard
Posted by: pursuer2004
Posted on: 2005-07-21 16:44

public static long get_peak_duration(Date begin_time,long duration)
  {
    //默认duration时间小于28天
    Date end_time;
    long start_h, start_m, start_s,end_h, end_m, end_s;
    long start, end;
    long days,h,m,s;
    long p, np=(24-7)*3600;//p---时长 np---n 天的时长
    days = (duration/3600)/24;
    if (days > 27)
    {
      System.err.println("-----通话时间溢出-----");
      System.exit(-1);
    }
    h = (duration/3600)%24;
    m = (duration%3600)/60;
    s = duration%60;
    Calendar times = Calendar. getInstance();;
    times.setTime(begin_time);
    start_h=times.get(Calendar.HOUR_OF_DAY);
    start_m=times.get(Calendar.MINUTE);
    start_s=times.get(Calendar.SECOND);
    end_s=(start_s+s);
    end_m=(start_m+m);
    end_h=(start_h+h);
  /**/  System.out.println("start time "+start_h+" "+start_m+" "+start_s);
    if (end_s>=60)
    {
      end_s=end_s-60;
      end_m++;
    }
    if (end_m>=60)
    {
      end_m=end_m-60;
      end_h++;
    }
    if (end_h>=24)
    {
      end_h=end_h-24;
      //days++;
    }
  /**/  System.out.println("end times "+end_h+" "+end_m+" "+end_s);
    System.out.println("days "+days);
  //  System.out.println("start_h="+start_h+"end_h="+end_h);
    np=np*days;
    start=start_h*3600+start_m*60+start_s;
    end=end_h*3600+end_m*60+end_s;
    if (start>=0&&start<7*3600)
    {
      if (end<start)
        p=(24-7)*3600+np;
      else if (end>=start&&end<7*3600)
        p=np;
      else
        p=(end_h-7)*3600+end_m*60+end_s+np;
    }
    else
    {
      if (end>=start&&end<24*3600){
        p=(end_h-start_h)*3600+(end_m-start_m)*60
          +(end_s-start_s)+np;
        System.out.printlnCamera;
      }
      else if (end<7*3600)
        p=(24-start_h)*3600+(0-start_m)*60
          +(0-start_s)+np;
      else
        p=(24-start_h)*3600+(0-start_m)*60
          +(0-start_s)
         +(end_h-start_h)*3600+(end_m-start_m)*60
          +(end_s-start_s)  
          +np;
    }
    
    return p;
    
  }


算时间好烦阿
算的是除了0-7点外的时间长度,单位:秒


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923