Picking up TimeInMillis and using Timestamp

3

Doubt:

I know two ways and capturing a TimeInMillis() does anyone know the difference and which one is the most efficient? What if there is a third way to capture this value?

Option 1:

System.out.println(Calendar.getInstance().getTimeInMillis());

Option 2:

System.out.println(System.currentTimeMillis());

Problem:

I noticed that there is a class called TimeStamp and I can not pass the System.currentTimeMillis() to it.

long time = System.currentTimeMillis();
Timestamp timestamp = new Timestamp(time);

Error:

The constructor Timestamp(long) is undefined
    
asked by anonymous 28.02.2014 / 14:56

1 answer

2

You should be importing the incorrect Timestamp , since this constructor has always existed. Check the import , the correct one is java.sql.Timestamp .

About getting the time in milliseconds, System.currentTimeMillis() is faster because it is a native function and does not require object creation. If you create a new java.util.Date() the constructor internally calls the currentTimeMillis() method, as well as the instance of Calendar (I checked only the GregorianCalendar ).

    
28.02.2014 / 15:08