java – Taking TimeInMillis and using Timestamp

Question:

Doubt:

I know two ways and capture a TimeInMillis() anyone know the difference and which 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't pass a System.currentTimeMillis() to it.

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

Mistake:

The constructor Timestamp(long) is undefined

Answer:

You must be importing the wrong Timestamp as 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's a native function and doesn't need the creation of objects. If I create a new java.util.Date() the constructor internally calls the currentTimeMillis() method, as does the Calendar instance (I just checked the GregorianCalendar ).

Scroll to Top
AllEscort