Skip to main content
added 411 characters in body
Source Link
Maksim
  • 511
  • 7
  • 17

Having in mind that 1 tick equals 100 ns and using the notation of Eugene Beresovsky solution here is another solution using the Duration class, together with a conversion from .Net ticks to Instant.

static final Instant dotNetEpoch = ZonedDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();   // Converts Instant to .NET Tick static long toDotNetDateTimeTicks(Instant i) { Duration d =Duration.between(dotNetEpoch, i); long secTix =Math.multiplyExact(d.getSeconds(), 10_000_000) ; long nanoTix = d.getNano()/ 100 ; long tix = Math.addExact(secTix,nanoTix); return tix; } // Converts .NET Tick to Instant static Instant toInstantFromDotNetDateTimeTicks(long dotNetTicks) { long millis =Math.floorDiv(dotNetTicks,10000) ; long restTicks = Math.floorMod(dotNetTicks,10000); long restNanos =restTicks*100; return dotNetEpoch.plusMillis(millis).plusNanos(restNanos); } 

Having in mind that 1 tick equals 100 ns and using the notation of Eugene Beresovsky solution here is another solution using the Duration class.

static final Instant dotNetEpoch = ZonedDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant(); static long toDotNetDateTimeTicks(Instant i) { Duration d =Duration.between(dotNetEpoch, i); long secTix =Math.multiplyExact(d.getSeconds(), 10_000_000) ; long nanoTix = d.getNano()/ 100 ; long tix = Math.addExact(secTix,nanoTix); return tix; } 

Having in mind that 1 tick equals 100 ns and using the notation of Eugene Beresovsky solution here is another solution using the Duration class, together with a conversion from .Net ticks to Instant.

static final Instant dotNetEpoch = ZonedDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();   // Converts Instant to .NET Tick static long toDotNetDateTimeTicks(Instant i) { Duration d =Duration.between(dotNetEpoch, i); long secTix =Math.multiplyExact(d.getSeconds(), 10_000_000) ; long nanoTix = d.getNano()/ 100 ; long tix = Math.addExact(secTix,nanoTix); return tix; } // Converts .NET Tick to Instant static Instant toInstantFromDotNetDateTimeTicks(long dotNetTicks) { long millis =Math.floorDiv(dotNetTicks,10000) ; long restTicks = Math.floorMod(dotNetTicks,10000); long restNanos =restTicks*100; return dotNetEpoch.plusMillis(millis).plusNanos(restNanos); } 
deleted 20 characters in body
Source Link
Maksim
  • 511
  • 7
  • 17

Having in mind that 1 tick equals 100 ns and using the notation of Eugene Beresovsky solution here is another solution using the Duration class.

static final Instant dotNetEpoch = ZonedDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant(); static long toDotNetDateTimeTicks(Instant i) { Duration d =Duration.between(dotNetEpoch, i); long secTix =Math.multiplyExact(d.getSeconds(), 10_000_000) ; long nanoTix = Math.multiplyExact(d.getNano(),/ 100) ; long tix = Math.addExact(secTix,nanoTix); return tix; } 

Having in mind that 1 tick equals 100 ns and using the notation of Eugene Beresovsky solution here is another solution using the Duration class.

static final Instant dotNetEpoch = ZonedDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant(); static long toDotNetDateTimeTicks(Instant i) { Duration d =Duration.between(dotNetEpoch, i); long secTix =Math.multiplyExact(d.getSeconds(), 10_000_000) ; long nanoTix = Math.multiplyExact(d.getNano(), 100) ; long tix = Math.addExact(secTix,nanoTix); return tix; } 

Having in mind that 1 tick equals 100 ns and using the notation of Eugene Beresovsky solution here is another solution using the Duration class.

static final Instant dotNetEpoch = ZonedDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant(); static long toDotNetDateTimeTicks(Instant i) { Duration d =Duration.between(dotNetEpoch, i); long secTix =Math.multiplyExact(d.getSeconds(), 10_000_000) ; long nanoTix = d.getNano()/ 100 ; long tix = Math.addExact(secTix,nanoTix); return tix; } 
Source Link
Maksim
  • 511
  • 7
  • 17

Having in mind that 1 tick equals 100 ns and using the notation of Eugene Beresovsky solution here is another solution using the Duration class.

static final Instant dotNetEpoch = ZonedDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant(); static long toDotNetDateTimeTicks(Instant i) { Duration d =Duration.between(dotNetEpoch, i); long secTix =Math.multiplyExact(d.getSeconds(), 10_000_000) ; long nanoTix = Math.multiplyExact(d.getNano(), 100) ; long tix = Math.addExact(secTix,nanoTix); return tix; }