Java Timestamp and MySQL Timestamp

3

Problem:

I am storing a timestamp in MYSQL, but when I redeem this timestamp I am losing the thousandths.

Java timestamp with thousandths.

MYSQLtimestampwithoutthousandths.

Why does this occur?

How to solve this?

I need the return with these thousandths.

edited:

I believe that mysql 5.1.62, do not save the milliseconds, so part of my solution would be to capture the timeStamp and remove the milliseconds, what efficient way to do this?

    
asked by anonymous 19.08.2014 / 16:15

2 answers

3

Answering your question:

  

What is the best way to get a timestamp in Java where you can always zero the milliseconds?

For cases:

  

System.currentTimeMillis (), Calendar.getInstance (). getTimeInMillis (), new Date (). getTime (); and new Timestamp (). getTime ();

You say you do not want to remove it, I'm not sure how you used it, but the simplest way I imagine it is dividing by 1000 and then multiplying by 1000. So you ignore the last three digits of your date , which correspond to milliseconds. So:

new Timestamp((System.currentTimeMillis()/1000)*1000);

Example:

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

public class TesteTimestamp {
    public static void main(String[] args) {
        timestampComMili(System.currentTimeMillis());
        timestampSemMili(System.currentTimeMillis());

        timestampComMili(Calendar.getInstance().getTimeInMillis());
        timestampSemMili(Calendar.getInstance().getTimeInMillis());

        timestampComMili(new Date().getTime());
        timestampSemMili(new Date().getTime());

        //timestampComMili(new Timestamp().getTime());
        //timestampSemMili(new Timestamp().getTime());
        /* O código acima não é possível, pois não existe um 
         * construtor padrão a classe Timestamp. Veja em:
         * http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html
         */
    }
    public static void timestampComMili(long l) {
        System.out.println("Com mili: " + new Timestamp(l));
    }
    public static void timestampSemMili(long l) {
        System.out.println("Sem mili: " + new Timestamp((l/1000)*1000));
    }
}

Result:

  

With mili: 2014-09-16 09: 48: 41,186
  No mili: 2014-09-16 09: 48: 41.0
  With mili: 2014-09-16 09: 48: 41,198
  No mili: 2014-09-16 09: 48: 41.0
  With mili: 2014-09-16 09: 48: 41,198
  No mili: 2014-09-16 09: 48: 41.0

    
16.09.2014 / 14:52
1

No MySQL 5.6 you could already achieve milliseconds precision.

try

select sysdate(6) will return 2013-04-16 13:47:56.273434

and

select sysdate(3) will return 2013-04-16 13:47:56.273

But if you do not want to migrate to MySQL 5.6 , you can use the DATE_FORMAT() function.

Here's an example:

DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p') resultado: Aug 19 2014 12:45 PM
DATE_FORMAT(NOW(),'%m-%d-%Y')          resultado: 08-19-2014
DATE_FORMAT(NOW(),'%d %b %y')          resultado: 19 Aug 14
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')    resultado: 19 Aug 2014 12:47:10:214
    
20.08.2014 / 17:50