我有一个数据库,我从中检索显示提醒的时间(这是我的任务)。收到的时间我放入计时器,但它不是在此时显示提醒,而是在当前时刻显示。有什么问题?
rs = stmt.executeQuery(sql);
while(rs.next()){
time=rs.getString ("time");
}
timer = new Timer(); // Instantiate Timer Object
timer.schedule(new Task(), timeFormat.parse(time));
例如,显示提醒的时间是17:10:00。但它显示在17:00:00
答案 0 :(得分:0)
首先,您必须获取currentDatetime和reminderDatetime之间的时间差并获取毫秒,然后在timer.schedule(new Task(),timediff)函数中传递该毫秒。 就像下面一样,
private long ScheduleTime(String strpostDate) {
long hrs = 0;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date curDate = null;
Date postDate = null;
String curdate = formatter.format(new Date());
try {
curDate = (Date) formatter.parse(curdate);
postDate = (Date) formatter.parse(strpostDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long diffInMs = postDate.getTime() - curDate.getTime();
return diffInMs;
}
你的功能
rs = stmt.executeQuery(sql);
while(rs.next()){
datetime=rs.getString ("time");
}
timer = new Timer(); // Instantiate Timer Object
timer.schedule(new Task(), ScheduleTime(datetime));