Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get time in milliseconds based on a given time zone (Local time zone)

It seems to be a wrong question but hear me out. When i call System.currentTimeMillis() it gives me seconds which when I convert to human readable format from an online milliseconds to date converter gives me GMT or UTC time but not my local time. Is there a way to get the current local time in millisec as I see there is a change of millsec value on online converter when I enter my local time in it.

To summarize, my problem is to get the current local time in milliseconds which some how is different from what I get using System.currentTimeMillis() function in java.

like image 962
Manish Patiyal Avatar asked Dec 11 '15 04:12

Manish Patiyal


People also ask

How do I get system time in milliseconds?

To get the current time in milliseconds, you just need to convert the output of Sys. time to numeric, and multiply by 1000.

How do you convert timestamps to milliseconds?

Using timedelta. A simple solution is to get the timedelta object by finding the difference of the given datetime with Epoch time, i.e., midnight 1 January 1970. To obtain time in milliseconds, you can use the timedelta. total_seconds() * 1000 .

What does system currentTimeMillis () return UTC?

json"(February 26, 2019 12:00:00 AM) and that need to be accessed from android app once device's System. currentTimeMillis() returns exact time.

How does system currentTimeMillis () work?

currentTimeMillis() method returns the current time in milliseconds. The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.


1 Answers

You can use TimeZone.getOffset(long) which gives the amount of time in milliseconds to add to UTC to get local time. Something like,

long date = System.currentTimeMillis();
int offset = TimeZone.getDefault().getOffset(date);
System.out.printf("%d + %d = %d%n", date, offset, date + offset);
like image 168
Elliott Frisch Avatar answered Sep 21 '22 12:09

Elliott Frisch