I have a problem with the difference in getting the time which has arisen this week, possibly due to Daylight Savings Time in the U.S. There is an hour difference in C# and Java (Android). I want the Java to behave the same as the C#.
Windows 10 - C#
value = DateTime.Now;
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
System.Diagnostics.Trace.WriteLine("span: " + (int)span.TotalSeconds);
Unix epoch seconds:
1489764265
Which converts to:
Fri, 17 Mar 2017 15:24:25 GMT
Java - Android Studio - Run on Samsung T5330-NU
Date date = new Date(); // current date and time
Integer.toString(ConvertToTimestamp(date))
Unit epoch seconds:
1489760686
Which converts to:
Fri, 17 Mar 2017 14:24:46 GMT
The seconds (:25 vs :46) is fine. The hours are the problem (15 vs 14) Somehow daylight savings time must be involved.
What can be done?
Using this to convert Unix epoch time:
http://www.onlineconversion.com/unix_time.htm
Edit:
Original Java code gets current date/time like so:
Calendar cal = Calendar.getInstance();
Based on the comment by @AkosNagy, I tried:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("MST"));
and
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("MDT"));
but there is no difference.
Edit:
Changed the title because this is in the end a question about C# and why the C# is not giving the correct time.
Edit: This is the line I was looking for:
Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;