0

What i want to do is to get the difference between two events, in this case the event when a store starts to fabricate certain item and the time when the same store starts to transport it to the customers location. Im using java and hibernate to develop this WebApp. the entity is called Report ( as i will generate a report from this information) the attributes for both events are:

@Column
private Timestamp fechaFabricacion;
@Column   
private Timestamp fechaTransporte;

they are being stored on my database correctly as:

2012-09-24 13:13:10 

for fabrication start and

2012-09-24 13:13:40

for transport start.

what i want to report is that it took the store (in this case) 30 seconds to go from fabrication to transport. Now, the values are being stored perfectly, the problem i have is when i try to retrieve it from my data base and do the math, this is the code i use.

Query qr = em.createNamedQuery("Reporte.findByNumSeguimientoEntrega");
    qr.setParameter("numSeguimientoEntrega",numSeguimiento);
    List<Reporte> reportList = qr.getResultList();
    Reporte report = reportList.get(0);
    Timestamp timeStamp1 = report.getFechaInicio();
    Timestamp timeStamp2 = report.getFechaFabricacion();
    Long timeDiference =(timeStamp1.getTime()-timeStamp2.getTime())/(1000 * 60 * 60 * 24);

the problem is, that im getting 0's for both timestamps nano values as my debugger shows in this image:

what am i doing wrong here? how can i get the real value an get the time difference?

thank a lot for taking the time and reading this.

1
  • you say the difference is supposed to be "seconds"? but you are calculating "days" (which of course will be 0 if the actual difference is only 30 seconds).
    – jtahlborn
    Sep 24, 2012 at 19:26

1 Answer 1

0

You have to map Date's in your entities with the annotation:

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DATE")
public Date getDate() {
    return this.date;
}

I think that way it should work just fine.

4
  • thanks for your fast response, the problem is i need to store it with month, day,hours, minutes and seconds as a fabrication could take weeks or maybe seconds. when i try to use @Temporal(TemporalType.TIMESTAMP) it wont let me compile as the server says: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar.
    – oskar132
    Sep 24, 2012 at 19:15
  • Why don't you change it to a Date? It will store all the data about time you need. This is the way i use it and it really works for these cases. Probably with the annotation, internally it stores dates as timestamps, but the object has to be a Date once Timestamp extends Date.
    – gdfbarbosa
    Sep 24, 2012 at 20:02
  • Do you need the nano part of Timestamp?
    – gdfbarbosa
    Sep 24, 2012 at 20:08
  • well, i need to get the difference in seconds between each event.
    – oskar132
    Sep 24, 2012 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.