Given a timestamp in milliseconds, i want to convert it to an timestamp in seconds.
I want to do it in java
Given a timestamp in milliseconds, i want to convert it to an timestamp in seconds.
I want to do it in java
You can use TimeUnit
to convert times, like this:
public static void main(String[] args) {
long timestampMillis = System.currentTimeMillis();
long timestampSeconds = TimeUnit.MILLISECONDS.toSeconds(timestampMillis);
System.out.println("Seconds: " + timestampSeconds);
}
The advantage with this, compared to simply dividing by 1000, is that you can easily adjust it to other units (days, hours, minutes) by calling other toXXX
methods.
You need to simply divide the milliseconds value by 1000 and you will get the value in seconds.
Something like this:
int seconds = (int) (milliseconds / 1000) ;