1
我在建立自己的课程时有些困难,我知道有些课程已经开课了,但由于内部限制,我必须实施自己的课程。 构造函数(下面的代码)基本上接受2个日期,一个开始和一个结束,方法makeWeeks(),将这个时期划分为几个星期,我根据我学习时学习的“头尾”名单。创造我自己的期间课程时的艰难时期
继承人的代码(要点)
public class Period implements Serializable
{
private static final long serialVersionUID = 1L;
private Calendar start;
private Calendar end;
private long durationMillis;
private long startMillis;
private long endMillis;
private long MAX = 604800000; //A week in millis
private long DAY = 86400000;
private long SIX = 518400000;
private ArrayList<Week> weeks;
public Period(Calendar start, Calendar end)
{
this.start = start;
this.end = end;
startMillis = start.getTimeInMillis();
endMillis = end.getTimeInMillis();
durationMillis = endMillis - startMillis;
int startWeek = start.get(Calendar.WEEK_OF_YEAR);
int endWeek = end.get(Calendar.WEEK_OF_YEAR);
weeks = new ArrayList<Week>();
}
/**
* Construct the weeks in this period
*
* @return
*/
public ArrayList<Week> makeWeeks()
{
long borderLine;
if ((durationMillis < MAX) || (endMillis < startMillis))
{
return null;
}
Calendar endHead = Calendar.getInstance();
//If first week is not Monday then count the days that have been completed
if (start.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY)
{
int days = 0;
start = adjustCalendar(start);//Set time to 0:0:0:0
days = getDaysToMonday(start.get(Calendar.DAY_OF_WEEK));
endHead.setTimeInMillis(start.getTimeInMillis());
endHead.add(Calendar.DAY_OF_YEAR, days - 1);
weeks.add(new Week(start, endHead, days, calcCompletion(days), setWeekNumber(start)));//was endHead
borderLine = endHead.getTimeInMillis() + DAY;
}//head week set
else
{
start = adjustCalendar(start);
endHead.setTimeInMillis(start.getTimeInMillis());//Successful
borderLine = endHead.getTimeInMillis();
}
long endMillis = 0;
while ((((borderLine + SIX)) < (end.getTimeInMillis() + 1)))
{
Calendar endNext = Calendar.getInstance();
Calendar begin = Calendar.getInstance();
begin.setTimeInMillis(borderLine);
endNext.setTimeInMillis(borderLine + SIX);
weeks.add(new Week(begin, endNext, 7, true, setWeekNumber(begin)));
borderLine += MAX;
endMillis = endNext.getTimeInMillis();
}
//adjust Tail
Calendar tail = Calendar.getInstance();
tail.setTimeInMillis(endMillis + DAY);//the beginning of the tail
tail = adjustCalendar(tail);//set to 00:00:00
int daysTail = getDaysToTail(end.get(Calendar.DAY_OF_WEEK) + 1);
if (daysTail != 0)
{
weeks.add(new Week(tail, end, daysTail, calcCompletion(daysTail), setWeekNumber(end)));
}
return weeks;
}//EOF makeweeks
private int setWeekNumber(Calendar cal)
{
return cal.get(Calendar.WEEK_OF_YEAR);
}
/**
* This method returns the amount of days between the given day and the next coming Monday
*/
private int getDaysToMonday(int currentDay)
{
switch (currentDay)
{
case 1://sunday
return 1;
case 2://monday
return 0;
case 3://tuesday
return 6;
case 4://wednesday
return 5;
case 5://thursday
return 4;
case 6://friday
return 3;
case 7://saturday
return 2;
}
return 0;
}//EOF method
/**
* Returns the day to the end of the final week, always start counting from a Monday
*
* @return
*/
private int getDaysToTail(int tailDay)
{
switch (tailDay)
{
case 1://sunday
return 0;
case 2://monday
return 1;
case 3://tuesday
return 2;
case 4://wednesday
return 3;
case 5://thursday
return 4;
case 6://friday
return 5;
case 7://saturday
return 6;
}
return 0;
}
/**
* Returns the amount of weeks stored in this period, completed and non
*
* @return
*/
public int totalWeeks()
{
return weeks.size();
}
/**
* Tells us if the selected period is at least one week
* will return no result.
*
* @return
*/
public boolean isAtleastWeek()
{
long diff = end.getTimeInMillis() - start.getTimeInMillis();
if (diff < MAX)
{
return false;
}
return true;
}
/**
* Provided a week-number, gives back a Week marked by that week-number, if existing.
*
* @param weekNumber
* @return
*/
public Week getWeek(Double weekNumber)
{
int weekNumberInt = weekNumber.intValue();
for (Week week : weeks)
{
if (weekNumberInt == week.getWeekNumber())
{
return week;
}
}
return null;
}
/**
* This method will set the calendars time (H:M:S:M) to 0:0:0:0 so to be able to calculate a precise start and End,
* dates will remain unaltered
*
* @param cal
* @return
*/
private Calendar adjustCalendar(Calendar nu)
{
nu.set(Calendar.HOUR, 0);
nu.set(Calendar.MINUTE, 0);
nu.set(Calendar.SECOND, 0);
nu.set(Calendar.MILLISECOND, 0);
return nu;
}
我怕有什么不对,这工作正常在大多数情况下,但在某些情况下,(如月和2011年12月),第二周是不是收集,并在2012年3月,如果您设置为2012年3月31日结束,则不收集最后一周,但如果您选择2012年3月30日结束。 你有什么建议吗?
什么“内部限制”阻止您使用其他库?日历逻辑是一些最复杂,非常平凡的代码,可能在_existence_中,正如http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-一个奇怪的结果。 –
我需要实现我自己的,因为这将作为其他扩展的核心类,此外,这需要适应需要特定代码实现的特定体系结构,我不能使用其他时间容器 – JBoy
......即便如此,我认为“无bug日历逻辑”是至少需要十几个Jon Skeet口径专家进行几个月工作的任务;我会尽全力避免它。 –