Manish Patiyal
Manish Patiyal

Reputation: 4467

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.

Upvotes: 6

Views: 10910

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

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);

Upvotes: 18

Related Questions