2016-11-11 243 views
-2

問題是:我使用的標準從我的數據庫中獲取「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; 
    } 
+1

'java.sql.Timestamp'繼承自'java.util.Date'。計算兩個Date對象之間的差異很簡單。我建議你回去閱讀Javadoc。 –

+0

我無法從時間戳轉換爲日期 –

+0

您可能無法從時間戳轉換爲java.util.Date,但應該可以從時間戳轉換爲java.sql.Date。 –

回答

0

java.sql.Timestamp的API它提供了一個方法的getTime()。

返回此Timestamp對象表示的自1970年1月1日00:00:00 GMT以來的毫秒數。

要計算差異,您可以結合使用此方法和Math.abs

0

如果對象是TIMESTAMP類型,類型轉換他們時間戳

Timestamp avgDateTs = (Timestamp)avgDate; 
Timestamp avgDateTs1 = (Timestamp)avgDate1; 

然後獲得以毫秒爲單位的時間。

long timeInMills = avgDateTs.getTime(); 
long timeInMills1 = avgDateTs1.getTime(); 

做一個毫秒值的差異,那會給你以毫秒爲單位的差異。

然後,您可以除以1000以秒爲單位獲得差異。

long differenceInSecs = (timeInMills1-timeInMills)/1000; 
相關問題