問題是:我使用的標準從我的數據庫中獲取「startDate」和「endDate」的最大值。現在我必須找到數據庫時間戳中這兩個對象之間的區別。計算兩個時間戳對象之間的差異java
有誰知道該怎麼辦?
taskTimes.java
public Object taskTimes() {
Session session = sessionFactory.openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Task.class).setProjection(Projections.max("endDate"));
Object avgDate = (Object) criteria.uniqueResult();
Criteria criteria1 = session.createCriteria(Task.class).setProjection(Projections.max("startDate"));
Object avgDate1 = (Object) criteria1.uniqueResult();
System.out.println(avgDate);
System.out.println(avgDate1);
session.close();
return avgDate;
}
Task.java
@Entity
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private int batch;
@Column
private java.sql.Timestamp startDate;
@Column
private java.sql.Timestamp endDate;
@ManyToOne
private Student student;
@ManyToOne
private Job job;
@OneToMany(mappedBy="task")
private List<Result> results;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
public Task(Long id, int batch, Timestamp startDate, Timestamp endDate, Student student, Job job,
List<Result> results) {
super();
this.id = id;
this.batch = batch;
this.startDate = startDate;
this.endDate = endDate;
this.student = student;
this.job = job;
this.results = results;
}
public java.sql.Timestamp getStartDate() {
return startDate;
}
public void setStartDate(java.sql.Timestamp startDate) {
this.startDate = startDate;
}
public java.sql.Timestamp getEndDate() {
return endDate;
}
public void setEndDate(java.sql.Timestamp endDate) {
this.endDate = endDate;
}
public Task() {
}
public int getBatch() {
return batch;
}
public void setBatch(int batch) {
this.batch = batch;
}
@Override
public String toString() {
return "Task [id=" + id;
}
'java.sql.Timestamp'繼承自'java.util.Date'。計算兩個Date對象之間的差異很簡單。我建議你回去閱讀Javadoc。 –
我無法從時間戳轉換爲日期 –
您可能無法從時間戳轉換爲java.util.Date,但應該可以從時間戳轉換爲java.sql.Date。 –