I am working on a piece functionality at work to send data between Java and .NET – the latter is an application that is in production so am limited in changing the core transport layer. I have come across an interesting problem when sending dates between these 2 systems. To cut a long story short I am using the DateTime.Ticks property to represent the DateTime on the wire however Java cannot use this directly (or vice versa) as the Epoch in both languages is different. Java bases its calendar on January 1, 1970, 00:00:00 GMT (aka “the epoch”). In .Net the “the epoch” corresponds to 12:00 A.M., January 1, 0001 GMT.
Hence using the Tick value in both systems needs to take into account eh difference in these Epochs. The magical numberto use is 62135596800000L – this is the number of Ticks from the .NET Epoch to the Java Epoch which I get using the following code:
DateTime javaEpoch = new DateTime(1970,01,01,00,00,00);
Console.WriteLine(javaEpoch.ToUniversalTime().Ticks);
I use this number in Java accordingly – all times internally in Java are represented in UTC (Date and Time in Java) :
Calendar now = new GregorianCalendar();
long lTicksJavaEpochToNow = now.getTimeInMillis() * 10000;
long lTicksMSDotNetEpochToLocal = lTicksJavaEpochToNow + 62135596800000L;
This should then convert back into the right date in .NET – however ther are some caveats like Timezones etc buf if you work in UTC then you should be ok.
What date is it today? Converting dates from .Net to Java and vise versa.