?
Solved

System.currentTimeMillis() affected by Day Light Savings and Leap Second adjustments

Posted on 2016-09-08
15
Medium Priority
?
110 Views
Last Modified: 2016-09-14
Hi Experts,

System.currentTimeMillis() will it be affected by day light savings?
if yes what is the alternative for this method?
0
Comment
Question by:srikotesh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +3
15 Comments
 
LVL 97

Expert Comment

by:John Hurst
ID: 41789381
If the regular time zone in Windows does not work for you, check the time zone variations and support articles in this Microsoft Article.

https://support.microsoft.com/en-ca/kb/914387

and get TZEdit (time zone editor) here:

https://blogs.msdn.microsoft.com/mthree/2007/02/07/your-questions-where-can-i-find-tzedit-exe-to-edit-time-zone-info/
0
 
LVL 16

Expert Comment

by:gurpsbassi
ID: 41789423
Yes I believe you cannot use that method to get the current time.
You can easily test it by modifying the regional settings on your computer to be BST for example.

Have a look at LocalDateTime.now() in Java8.
0
 
LVL 97

Expert Comment

by:John Hurst
ID: 41789432
TZEdit just changes the time zone so that Daylight savings time changes properly.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:gurpsbassi
ID: 41789497
@John Hurst I think the author is asking how to get the current time using Java API.
0
 
LVL 14

Accepted Solution

by:
CPColin earned 1004 total points
ID: 41789627
Per the documentation for that method, the value returned is "the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC." Since it's measuring using UTC, it is not affected by Daylight Saving Time changes. You know this has to be true because, when you call new Date(), the object you get back knows whether DST was in effect or not.

You can test this yourself by parsing two date strings, before and after DST ends, like "2016-09-06 01:30:00 PDT" and "2016-09-06 01:30:00 PST" and comparing the values Date.getTime() returns. The values should differ by the number of milliseconds in an hour.
0
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 332 total points
ID: 41789683
System.currentTimeMillis() will it be affected by day light savings?

No. If you were to call that method 1 second before DST change and again 1 second after the change, then the difference between the 2 values would indicate 2 seconds (ie. 2000ms) NOT 1 hour and 2 seconds. And again when you come out of DST, the value won't go backwards by 1 hour.
0
 
LVL 2

Author Comment

by:srikotesh
ID: 41790101
Hi CPColin,

I can see 1 hr difference.

public static void main(String args[]) throws ParseException{
		String PST ="2016-09-06 01:30:00 PDT"; 
		String PDT ="2016-09-06 01:30:00 PST";
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
		Date pstDate = formatter.parse(PST);
		Date pdtDate = formatter.parse(PDT);
		System.out.println("pst "+pstDate.getTime());
		System.out.println("pdt "+pdtDate.getTime());
		long val = pdtDate.getTime()-pstDate.getTime();
		System.out.println("val "+val/1000);
	}
o/p:
pst 1473150600000
pdt 1473154200000
val 3600

Open in new window

0
 
LVL 2

Author Comment

by:srikotesh
ID: 41790107
Hi mccarl,

I can see 1 hr 2sec difference.
String PST ="2016-09-06 01:30:01 PDT";
String PDT ="2016-09-06 01:30:03 PST";
0
 
LVL 2

Author Comment

by:srikotesh
ID: 41790112
I am concluding there is no day light impact when i call System.currentTimeMillis().
0
 
LVL 16

Assisted Solution

by:gurpsbassi
gurpsbassi earned 332 total points
ID: 41790219
I am concluding there is no day light impact when i call System.currentTimeMillis().

where is the proof?
0
 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 332 total points
ID: 41790717
As cpcolin said the currentTimeMillis() is not affected by daylight savings. However as showed earlier a date object will be. The getTime() on a date will return the time since 1970 like cpcolin said, but since PDTand PST are different times right now you get an hour difference.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 41790765
Hi mccarl,

I can see 1 hr 2sec difference.
String PST ="2016-09-06 01:30:01 PDT";
String PDT ="2016-09-06 01:30:03 PST";

Yes, but I wasn't talking about Date objects at all, just System.currentTimeMillis()
1
 
LVL 14

Expert Comment

by:CPColin
ID: 41791377
gurpsbassi,
where is the proof?
It's in srikotesh's two comments prior to the one you replied to. He tried the experiments and they confirmed what we suspected.
0
 
LVL 16

Expert Comment

by:gurpsbassi
ID: 41791926
@CPColin I must be going mad. I thought he was trying to use System.currentTimeMillis() not Date()
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41791933
The java.util.Date class is a wrapper for the number of milliseconds since the Unix epoch (the no-argument Date() constructor calls System.currentTimeMillis() to initialize the value it wraps). That's why I recommended using it for the experiment; the values Date.getTime() would return from the parsed date strings would show what System.currentTimeMillis() would have returned, had we called it at that instant.

Sorry I didn't explain my reasoning there.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question