C# Datetime.Ticks Equivalent in Java

C# DateTime.Ticks equivalent in Java

Well, java.util.Date/Calendar only have precision down to the millisecond:

Calendar calendar = Calendar.getInstance();    
calendar.set(Calendar.MILLISECOND, 0); // Clear the millis part. Silly API.
calendar.set(2010, 8, 14, 0, 0, 0); // Note that months are 0-based
Date date = calendar.getTime();
long millis = date.getTime(); // Millis since Unix epoch

That's the nearest effective equivalent. If you need to convert between a .NET ticks value and a Date/Calendar you basically need to perform scaling (ticks to millis) and offsetting (1st Jan 1AD to 1st Jan 1970).

Java's built-in date and time APIs are fairly unpleasant. I'd personally recommend that you use Joda Time instead. If you could say what you're really trying to do, we can help more.

EDIT: Okay, here's some sample code:

import java.util.*;

public class Test {

private static final long TICKS_AT_EPOCH = 621355968000000000L;
private static final long TICKS_PER_MILLISECOND = 10000;

public static void main(String[] args) {
long ticks = 634200192000000000L;

Date date = new Date((ticks - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND);
System.out.println(date);

TimeZone utc = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(utc);
calendar.setTime(date);
System.out.println(calendar);
}
}

Note that this constructs a Date/Calendar representing the UTC instant of 2019/9/14. The .NET representation is somewhat fuzzy - you can create two DateTime values which are the same except for their "kind" (but therefore represent different instants) and they'll claim to be equal. It's a bit of a mess :(

convert date to c# ticks in java

ticks = 621355968000000000L+javaMillis*10000;

Java 8 Time - Equivalent of .NET DateTime.MaxValue.Ticks

UPDATE Original answer didn't account for offset difference between Java epoch (1970) and .NET ticks (0001). Corrected!

For reference, Long.MAX_VALUE (Java) is:

9,223,372,036,854,775,807

In .NET, DateTime.MaxValue is:

9999-12-31 23:59:59.9999999
3,155,378,975,999,999,999 ticks1 (~ 1/3 of long)

In Java 8, Instant.MAX is:

+1000000000-12-31 23:59:59.999999999
31,556,889,864,403,199,999,999,999 nanos (overflows long)
315,568,898,644,031,999,999,999 ticks2 (overflows long)
31,556,889,864,403,199,999 millis (overflows long)
31,556,889,864,403,199 seconds (~ 1/292 of long)

For reference, your value of 2519303419199999999 is:

2016-08-23 13:28:00
636,075,556,800,000,000 ticks1 (~ 1/14 of long)
14,719,588,800,000,000 ticks2 (~ 1/626 of long)
1) Since 0001-01-01 (.NET)     2) Since 1970-01-01 (Java)

As you can see, Instant.MAX in "ticks" will not fit in a long. Not even milliseconds will fit.

More importantly Instant.MAX is not the same value as DateTime.MaxValue.

I would suggest you just create a constant for the value, e.g.

public static final long DATETIME_MAXVALUE_TICKS = 3155378975999999999L; // .NET: DateTime.MaxValue.Ticks

That way you'll get same string values as you .NET code:

public static final long EPOCH_OFFSET = 62135596800L; // -Instant.parse("0001-01-01T00:00:00Z").getEpochSecond()

private static long getTicks(Instant instant) {
long seconds = Math.addExact(instant.getEpochSecond(), EPOCH_OFFSET);
long ticks = Math.multiplyExact(seconds, 10_000_000L);
return Math.addExact(ticks, instant.getNano() / 100);
}

public static void main(String[] args) {
Instant startTime = Instant.parse("2016-08-23T13:28:00Z");
String s = String.format("%19d", DATETIME_MAXVALUE_TICKS - getTicks(startTime));
System.out.println(s);
}

Output:
2519303419199999999

How to convert C# DateTime to Java DateTime

I haven't checked if it satisfies all your requirements regarding the output, but I think it will give enough pointers to help you out. Depending on your needs you need a ZonedDateTime (which has a timezone), or a LocalDateTime, which is the date as people speak about it in a country.

  private String getDate(long value) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime start = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0);
LocalDateTime lastWeek = now.minusDays(6);
LocalDateTime date = start.plus(value, ChronoUnit.MILLIS);

if (lastWeek.isBefore(date)) {
DayOfWeek dayOfWeek = date.getDayOfWeek();
if (dayOfWeek == now.getDayOfWeek()) {
return "Today";
} else {
return dayOfWeek.name();
}
}

return date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
}

I've also took the liberty to convert the code style to what is usual in Java, which is the placing of the opening brace, the capitalization of functions.

More on the date/time classes can be found in the Oracle Trail on date/time.

What is the equivalent of DateTime.FromOADate() in Java (double to Datetime in Java)

Did you realize that your binary data is the binary represantation of an OLE Automation date value?

So instead of getting long, you should get a double value from your array.

var b = new byte[8];
b[0] = 0x20;
b[1] = 0x64;
b[2] = 0xa8;
b[3] = 0xac;
b[4] = 0xb6;
b[5] = 0x65;
b[6] = 0xe4;
b[7] = 0x40;
var dbl = BitConverter.ToDouble(b, 0);
var dt = DateTime.FromOADate(dbl);
Console.WriteLine("{0:s}", dt);

Result is :

2014-05-14T17:00:21

I think the valid question should be: What is the equivalent of DateTime.FromOADate() in Java ?

Answer is:

public static Date fromDoubleToDateTime(double OADate) 
{
long num = (long) ((OADate * 86400000.0) + ((OADate >= 0.0) ? 0.5 : -0.5));
if (num < 0L) {
num -= (num % 0x5265c00L) * 2L;
}
num += 0x3680b5e1fc00L;
num -= 62135596800000L;

return new Date(num);
}


Related Topics



Leave a reply



Submit