0

I have a program that I am converting from C# to Java and have run into a strange issue:

The C# application outputs a 'windows timestamp' via the following code:

private static readonly DateTime epochDateTime = new DateTime(1970, 1, 1); double totalSeconds = seconds + nanosecs / 1000000000.0; wintimestamp = epochDateTime.AddSeconds(totalSeconds).ToFileTimeUtc(); 

I'm having trouble trying to duplicate the functionality of "ToFileTimeUtc" in Java. I need to keep it since the existing applications that use the data generated from this application are expecting the same data format.

I have tried some this in java and got it to match up. But I would like a deeper explanation as to what I'm doing.

 int second = 1589913034; int nanosec = 24; double totalSeconds = second + nanosec / 1_000_000_000.0; System.out.println(totalSeconds); long wintimestamp = (long) (116444736000000000L + totalSeconds * 10_000_000); System.out.println(wintimestamp); 

I'm pretty sure this value: 116444736000000000L is the epoch equivalence of the 1601 Win32 epoch to the regular java epoch. I got this from doing:

 epochDateTime.AddSeconds(totalSeconds).ToFileTimeUtc(); 

Then I finally started playing around with the multiplier until I got close to the value from the C# call finally landing on 10 million that got me the exact same number. Not sure why though.

2
  • First, go with the Java 8 Time API. Commented May 19, 2020 at 19:34
  • I was just playing around in a test class trying to replicate. I'll use time in the end. Commented May 19, 2020 at 19:37

1 Answer 1

1

If you read the documentation of DateTime.ToFileTimeUtc() you will find:

A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC).

100-nanoseconds is 1 / 10_000_000 seconds.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was just arriving at that same conclusion and popped back to this question to answer it myself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.