本帖最后由 老包 于 2010-8-16 17:31 编辑
开放代码草稿:
时间信息的接口TimePointIntf.java, 抽象类TimeRelAbs.java和实现类realizeTimeRel.java及其输出结果,关于时间点的早,晚,时间差以及时间段早,晚和重叠等。
&&&&&&&&&&&&&&&&&&
TimePointIntf.java
package intfc;
import java.util.*;
//developed by Hanfei Bao, BMKI lab and MiForum, China
public interface TimePointIntf {
public String TimePointStr = null;
//a start or end time point of a period
public String StartTimePointStr = null;
public String EndTimePointStr = null;
public String TestTimePointStr = null;
public String DataRecordTimePointStr = null;
public int TimePointInt =0;
//a start or end time point of a period
public int StartTimePointInt =0;
public int EndTimePointInt = 0;
public int TestTimePointInt = 0;
public int DataRecordTimePointInt = 0;
}
&&&&&&&&&&&&&&&&&&
TimeRelAbs.java
package intfc;
import java.util.*;
// developed by Hanfei Bao, BMKI lab and MiForum, China
abstract class TimeRelAbs implements TimePointIntf {
//period means a segment on the time axis
//time length or duration is a relative time segment
//time point has form YYYYMMDD(HHMMDD) representing YYYY-MM-DD(-HH-MM-DD)
public int hasTimeLength(int TimePoint1,int TimePoint2){
int TimeLength = TimePoint2 - TimePoint1;
return TimeLength;
};
public String TimePointEarlier(int TimePointA,int TimePointB){
if (TimePointA < TimePointB){
return "This TimePoint is earlier than that!";
}
else
return "This TimePoint is not earlier than That!";
};
public String TimePointlater(int TimePointA,int TimePointB){
if (TimePointA > TimePointB){
return "This TimePoint is later than that!";
}
else
return "This TimePoint is not later than that!";
};
public String TimePointSameAS(int TimePointA,int TimePointB){
if (TimePointA == TimePointB){
return "This TimePoint is same as that!";
}
else
return "This TimePoint is not same as that!";
};
public void TimePointDifference(int TimePointA,int TimePointB){
int TPD = TimePointA - TimePointB;
if (TPD < 0){
System.out.println("TimePointA is "+Math.abs(TPD)+" earlier than TimePointB ");
}
else if (TPD > 0){
System.out.println("TimePointA is "+Math.abs(TPD)+" later than TimePointB ");
}
else if (TPD == 0){
System.out.println("TimePointA is the same as TimePointB ");
}
}
public void TimePeriodEarlier(int TimePointA1,int TimePointA2,int TimePointB1,int TimePointB2){
int STimePointOfPeriodA = TimePointA1;
int ETimePointOfPeriodA = TimePointA2;
int STimePointOfPeriodB = TimePointB1;
int ETimePointOfPeriodB = TimePointB2;
int TimeLengthOfPeriodA = ETimePointOfPeriodA - STimePointOfPeriodA;
int TimeLengthOfPeriodB = ETimePointOfPeriodB - STimePointOfPeriodB;
int TimeLengthOfPeriodBA = Math.abs(ETimePointOfPeriodB - STimePointOfPeriodA);
//Period ealier, later or overlapped?
if (STimePointOfPeriodA < STimePointOfPeriodB){
System.out.println("PeriodA happens earlier than PeriodA");
if (TimeLengthOfPeriodBA < (TimeLengthOfPeriodA+TimeLengthOfPeriodB) ){
System.out.println("and PeriodA is overlapped by PeriodB");
}
else if (TimeLengthOfPeriodBA > (TimeLengthOfPeriodA+TimeLengthOfPeriodB)){
System.out.println("and PeriodA isn't overlapped by PeriodB");
}
else
System.out.println("PeriodB started when PeriodB ended.");
}
//Period is in another perion?
if (STimePointOfPeriodA < STimePointOfPeriodB && ETimePointOfPeriodA > ETimePointOfPeriodB){
System.out.println("PeriodB is in or covered by PeriodA.");
}
}
}
&&&&&&&&&&&&&&&&&&&&
realizeTimeRel.java
package intfc;
import java.util.*;
//developed by Hanfei Bao, BMKI lab and MiForum, China
public class realizeTimeRel extends TimeRelAbs{
public static void main(String[] args) {
realizeTimeRel rTR = new realizeTimeRel();
//
int TimePoint1 = 20100320;
int TimePoint2 = 20100524;
String whichEarlier = rTR.TimePointEarlier(TimePoint1,TimePoint2);
System.out.println(whichEarlier );
//
int sTimePeriod1 = 20100119;
int eTimePeriod1 = 20100621;
int sTimePeriod2 = 20100318;
int eTimePeriod2 = 20100815;
rTR.TimePeriodEarlier(sTimePeriod1,eTimePeriod1,sTimePeriod2,eTimePeriod2);
}
}
&&&&&&&&&&&&&&&&&&&&
output
This TimePoint is earlier than that!
PeriodA happens earlier than PeriodA
and PeriodA is overlapped by PeriodB |