问题是:我使用条件从我的数据库中获取" startDate"的最大值。和" endDate"。现在我必须找到数据库时间戳中这两个对象之间的区别。
有谁知道怎么做?
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;
}
答案 0 :(得分:0)
根据java.sql.Timestamp的API,它提供了一个方法getTime()。
返回自此Timestamp对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。
要计算差异,您可以结合使用此方法和#include <stdio.h>
int main (void)
{
static int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
}
。
答案 1 :(得分:0)
如果对象的类型为Timestamp,则将它们转换为Timestamp
Timestamp avgDateTs = (Timestamp)avgDate;
Timestamp avgDateTs1 = (Timestamp)avgDate1;
然后以毫秒为单位获取时间。
long timeInMills = avgDateTs.getTime();
long timeInMills1 = avgDateTs1.getTime();
执行毫秒值的差异,这会产生毫秒差异。
然后您可以除以1000以获得以秒为单位的差异。
long differenceInSecs = (timeInMills1-timeInMills)/1000;