I need help. I have been trying to figure out why java util date is 5 hours behind after converting from C#
ticks.
in C#
, the date is 6/8/2013 11:02:07 AM, I convert this date into ticks then pass it to java as long
.
code snippet:
taken:
- long TICKS_AT_EPOCH = 621355968000000000L;
- long TICKS_PER_MILLISECOND = 10000;
java.util.Date date = new java.util.Date((ctime - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND);
Now java util date is Sat Jun 08 06:02:07 CDT 2013
Notice that the hour is 5 hours difference.
Any suggestions why?
You are constructing a java.util.Date
based on milliseconds since 1/1/1970 UTC. You appear to be correcting from the fact that .net's System.DateTime.Ticks
are based on 1/1/0001 and are 10,000 ticks to a millisecond. That is correct, but you have forgotten to adjust to UTC.
In .Net, the value coming from DateTime.Ticks
is highly dependent on the DateTime.Kind
property. There are three possible kinds of DateTime
values.
DateTimeKind.Utc
- This kind means that the value represents UTC time. It usually comes from a call to DateTime.UtcNow
, but can also be constructed directly, and often is. For example, you might be retrieving UTC times from a database. You can feed the ticks from here directly into your conversion, and it will work.
DateTimeKind.Local
- This usually comes from a call to DateTime.Now
. The values are representative of the local time zone. You will need to convert to UTC before checking the ticks. You can do the following:
DateTime dt = DateTime.Now;
int utcTicks = dt.ToUniversalTime().Ticks;
Be aware that if the time happens during a daylight saving "fall-back" style transition, the result might be incorrect. The DateTime
class has no idea about time zones. It just reflects the current local clock. If the value in dt
is ambiguous, ToUniversalTime()
will assume that the value is representative of standard time, even if you just retrieved it while in daylight time. This is just one of the many confusing and probablematic aspects of DateTime
in .net.
DateTimeKind.Unspecified
- This is the most common kind of DateTime
you will encounter, and usually comes from DateTime.Parse()
or a constructor like new DateTime(...)
. Unfortunately, there is nothing in here that will tell you about the time zone these dates are representative of. You can still try calling .ToUniversalTime()
, but the framework will make the assumption that these times are representative of your local time zone, as if the kind was Local
. That assumption could be completely wrong, depending on how you sourced the data. There really is no safe way to transform an Unspecified
DateTime
to a UTC value (ticks or otherwise).
There are some solutions, such as using DateTimeOffset
instead of DateTime
, or using the Noda Time library instead of the built-in types. You can read more about these problems here and here.
The time is not 5 hours behind, it's exactly the same time. The problem is with the way you print it.
You need to tell C# and Java to use the same time-zone when converting the date to string. One of them is using UTC and the other CDT.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With