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.println;
}
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点外的时间长度,单位:秒