这是我的defaultuser
服务类我希望差异以hh:mm:ss
格式存储在数据库中(我使用spring mvc + hibernate)
@Override
@Transactional
public LoginLog punchLogoutTime(User user) {
LoginLog log = getOpenUserLoginLog(user);
log.setLogoutTime(new Date());
long diff=log.getLogoutTime().getTime()-log.getLoginTime().getTime();
log.setTotalTime(diff);
log = loginLogDao.update(log);
return log;
}
这是我的pojo课程。我花了很长时间。
@Column(name = "total_time")
private Long totalTime;
public Long getTotalTime() {
return totalTime;
}
public void setTotalTime(Long difference) {
this.totalTime = difference;
}
请告诉我适当的答案。谢谢:)
答案 0 :(得分:0)
//assume diff is in millisec
DateFormat df = new SimpleDateFormat("dd:MM:yy:HH:mm:ss");
System.out.println(df.format(diff));
//remove dd:mm:yy if you want
答案 1 :(得分:0)
修改您的实体类(如下所示)以指定Date的格式。
//Create Table SQL in SQL Server
CREATE TABLE "DBO"."Test"(
Id INT PRIMARY KEY NOT NULL,
difftime TIME not null
)
//Entity Code
@Entity(name = "Test")
@Table(name = "Test")
public class Test {
@Id
@Column(name = "Id")
private int id;
@Column(name = "difftime")
@DateTimeFormat(pattern = "hh:mm:ss")
private Date time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
//用于保存的控制器代码
@Override
@Transactional
public LoginLog punchLogoutTime(User user) {
LoginLog log = getOpenUserLoginLog(user);
log.setLogoutTime(new Date());
long diff=log.getLogoutTime().getTime()-log.getLoginTime().getTime();
Date totalTimeIn=new Date();
totalTimeIn.setTime(diff);
log.setTotalTime(totalTimeIn);
log = loginLogDao.update(log);
return log;
}