Saturday 15 February 2014

C# Ticks convert to java util date; date is 5 hours behind why? -


I need help C # after 5 hours after being replaced by ticks why use Java I am trying to know what is the date.

In C # , date 6 / 8/2013 11:02:07 AM, I change this date to ticks if it is long . Pass as in Java

Code snippet:

taken: - tall TICKS_AT_EPOCH = 621355968000000000 L; - tall TICKS_PER_MILLISECOND = 10000; Java.util.Date date = new java.util.Date ((ctime - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND);

Now the date of use of Java is Sat. June 08 06:02:07 CDT 2013

Note that the time of the hour is a 5 hour difference.

Why any suggestions?

Creating a java.util.Date on your behalf You are recovering from 1/1/1970 UTC to milliseconds. You are recovering from the fact that .net is based on System.DateTime.Ticks 1/1/0001 and is at 10,000 millimeters milliseconds. That's right, but you forgot to adjust UTC.

In Net, the value coming from DateTime.Ticks is highly dependent on DateTime.Kind There are three possible types of property DateTime value.

  • DatedTimeCind.UTC - This means that the value represents UTC time. It usually comes from a call from DateTime.UtcNow , but it can be made directly, and often it is. For example, you can recover UTC time from the database, you can tick it directly into your conversion from here, and it will work.

  • DateTime.Local - This is usually the call from DateTime.Now . Values ​​are representative of the local time zone. You need to convert to UTC before checking the check. You can do the following:

      DateTimeDT = DateTime.Now; Int utcTicks = dt.ToUniversalTime () Tics;   

    Keep in mind that if time is a daylight saving "fall" style occurs during the transition, then the result may be incorrect date time class about time zone There is no information in this. It shows the current local clock. If the value in dt is ambiguous, ToUniversalTime () will assume the value is representative of standard time , even if you have yet received it This is one of the many confusing and potential aspects of .net in DateTime in at the time of day .

  • DateTimeCind Unspecified - This is the most common type of date time , and usually comes from DateTime.Parse () or new DateTime (... ) . Unfortunately, there is nothing here that will tell you about the time zone that these dates are representative. You can still try to call . Universal Time () , but the framework will make this assumption that these times are representative of your local time zone, such as local this assumption may be completely incorrect, on this Depending on how you thought the data. There are some solutions, such as < Using the code> DateTimeOffset instead of DateTime , or using the library instead of the underlying type. You can read more about these problems.

No comments:

Post a Comment