Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current UTC timestamp in seconds

Tags:

java

timestamp

How to get current time-stamp in UTC/GMT and then How to convert this time-stamp into seconds ? Itry this but its thrw exception

    SimpleDateFormat sdfu  = new SimpleDateFormat("yyyy-MM-dd kk:mm");
    Date udate = sdfu.parse(dateAndTimeUTC);
    long timeInMillisSinceEpoch123 = udate.getTime(); 
    long durationinSeconds2 = timeInMillisSinceEpoch123 / 1000;
    System.out.println("Time in Seconds UTC: " + durationinSeconds2);

Is there any better way to convert UTC/GMT time-stamp into seconds?

like image 352
kreya Avatar asked Oct 11 '13 09:10

kreya


People also ask

What is current UTC time in seconds?

Current UTC timestamp to time is 13:56:19.

How do you find the timestamp in seconds?

getTime(); long durationinSeconds2 = timeInMillisSinceEpoch123 / 1000; System. out. println("Time in Seconds UTC: " + durationinSeconds2);

How do you find the current UTC timestamp?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.

How do you convert UTC to timestamp?

Getting the UTC timestamp Use the datetime. datetime. now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC.


2 Answers

With Java 8+, you can use:

Instant.now().getEpochSecond()
like image 112
yuen26 Avatar answered Oct 19 '22 17:10

yuen26


get current seconds system long timestamp = System.currentTimeMillis() / 1000;

like image 32
Dat Nguyen Avatar answered Oct 19 '22 18:10

Dat Nguyen