Picking up Timeinmillis and using Timestamp

Asked

Viewed 703 times

3

Doubt:

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


Option 1:

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

Option 2:

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

Problem:


I realized there’s a class called TimeStamp and I can’t pass the one System.currentTimeMillis() for her.

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

Error:

The constructor Timestamp(long) is undefined

1 answer

2


You must be importing the Timestamp incorrect, as this constructor has always existed. Check the import, the correct is java.sql.Timestamp.

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

  • 1

    Problem is the import java.security.Timestamp that I was wrong in my code, I switched it and I was able to use the Timestamp. The explanation of TimeMillis regarding the object helped a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.