//如果你表里没有时间则会报空指针
public Body upTimeCourse(Stucourse stucourse1, String stuId) {
Stucourse sc = new Stucourse();
EntityWrapper ews = new EntityWrapper<>();
ews.eq("stu_id", stuId);
List list = sc.selectList(ews);
List lists = new ArrayList<>();
if (list.size() == 0) {
return Body.newInstance(404, "找不到此学生");
}
for (Stucourse stucourse : list) {
// ***************取出该对象的开始结束时间
String starttime = stucourse.getCourseStarttime();
String endtime = stucourse.getCourseEndtime();
StringBuffer sb = new StringBuffer();
sb.append(starttime.substring(0, 4));
sb.append(starttime.substring(5, 7));
sb.append(starttime.substring(8, 10));
sb.append(starttime.substring(11, 13));
sb.append(starttime.substring(14, 16));
long stime = Long.parseLong(sb.toString());
System.out.println("课程开始时间:" + stime);
// 将结束时间转换成可计算类型Long
StringBuffer ssb = new StringBuffer();
ssb.append(endtime.substring(0, 4));
ssb.append(endtime.substring(5, 7));
ssb.append(endtime.substring(8, 10));
ssb.append(endtime.substring(11, 13));
ssb.append(endtime.substring(14, 16));
long etime = Long.parseLong(ssb.toString());
System.out.println("课程结束时间:" + etime);
// 当前时间
StringBuffer dqsb = new StringBuffer();
SimpleDateFormat nian = new SimpleDateFormat("yyyy");// 设置日期格式
dqsb.append(nian.format(new Date()));
SimpleDateFormat yue = new SimpleDateFormat("MM");
dqsb.append(yue.format(new Date()));
SimpleDateFormat ri = new SimpleDateFormat("dd");
dqsb.append(ri.format(new Date()));
SimpleDateFormat shi = new SimpleDateFormat("HH");
dqsb.append(shi.format(new Date()));
SimpleDateFormat fen = new SimpleDateFormat("mm");
dqsb.append(fen.format(new Date()));
long nowtime = Long.parseLong(dqsb.toString());
System.out.println("当前系统时间:" + nowtime);
// 开始判断课程状态 用到三个变量 nowtime 、stime、etime
if (nowtime < stime) {
stucourse.setFanhuijieguo("课程未开始");
} else if (nowtime > stime && nowtime < etime) {
stucourse.setFanhuijieguo("课程进行中");
} else if (nowtime > etime) {
stucourse.setFanhuijieguo("课程已结束");
}
lists.add(stucourse);
}
if (lists.size() > 0) {
return Body.newInstance(lists);
}
return Body.newInstance(404, "好像出了点小问题,等会再试一下吧");
}