[英]C# DateTime.Ticks equivalent in Java
What is the Java equivalent of DateTime.Ticks in C#?什么是 C# 中的 DateTime.Ticks 的 Java 等效项?
DateTime dt = new DateTime(2010, 9, 14, 0, 0, 0);
Console.WriteLine("Ticks: {0}", dt.Ticks);
What will be the equivalent of above mentioned code in Java? Java 中上述代码的等价物是什么?
Well, java.util.Date/Calendar only have precision down to the millisecond:好吧,java.util.Date/Calendar 的精度只有毫秒:
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).如果您需要在 .NET 刻度值和
Date
/ Calendar
之间进行转换,您基本上需要执行缩放(刻度到毫秒)和偏移(1st Jan 1AD 到 1st Jan 1970)。
Java's built-in date and time APIs are fairly unpleasant. Java 的内置日期和时间 API 相当令人不快。 I'd personally recommend that you use Joda Time instead.
我个人建议您改用Joda Time 。 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.请注意,这构造了一个日期/日历,表示 2019/9/14 的UTC时刻。 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.
.NET 表示有点模糊 - 您可以创建两个相同的 DateTime 值,除了它们的“种类”(但因此代表不同的时刻),它们会声称是相等的。 It's a bit of a mess :(
有点乱:(
In Java is:在 Java 中是:
long TICKS_AT_EPOCH = 621355968000000000L;
long tick = System.currentTimeMillis()*10000 + TICKS_AT_EPOCH;
System.nanoTime() gives you nanoseconds in Java (since 1.6). System.nanoTime() 在 Java 中为您提供纳秒(从 1.6 开始)。 You'll still need to shift/rescale, but no precision will be lost.
您仍然需要移动/重新缩放,但不会丢失任何精度。
Base on Jon Skeet I developed this class基于 Jon Skeet 我开发了这个类
import java.util.Calendar;
import java.util.Date;
public class DateHelper {
private static final long TICKS_AT_EPOCH = 621355968000000000L;
private static final long TICKS_PER_MILLISECOND = 10000;
public static long getUTCTicks(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return (calendar.getTimeInMillis() * TICKS_PER_MILLISECOND) + TICKS_AT_EPOCH;
}
public static Date getDate(long UTCTicks){
return new Date((UTCTicks - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND);
}
}
It works for me这个对我有用
And for those of us showing up trying to get the current number of ticks as defined by the UUID specification:对于我们这些试图获取 UUID 规范定义的当前刻度数的人来说:
/**
Returns the current tick count.
Ticks are the number of 100 ns intervals since October 15, 1582
@return
*/
private static long getUtcNowTicks() {
final long UNIX_EPOCH_TICKS = 122192928000000000L; //Number of ticks from 10/16/1582 to 1/1/1970
Instant i = Clock.systemUTC().instant(); //get the current time
long ticks = UNIX_EPOCH_TICKS; // number of ticks as of 1/1/1970
ticks += i.getEpochSecond()*10000000; //number of whole seconds (converted to ticks) since 1/1/1970
ticks += i.getNano() / 100; //number of ticks since the start of the second
return ticks;
/*
Some interesting tick values
Date Ticks
========== ==================
10/15/1582 0 Start of UUID epoch; the date we switched to the Gregorian calendar)
1/01/1601 5748192000000000 Start of Windows epoch (start of 1st Gregorian 400-year cycle)
12/30/1899 100101312000000000 Start of Lotus 123, Excel, VB, COM, Delphi epoch
1/01/1900 100103040000000000 Start of SQL Server epoch
1/01/1970 122192928000000000 Start of UNIX epoch
1/01/2000 131659776000000000
1/01/2010 134815968000000000
1/01/2020 137971296000000000
1/19/2038 143714420469999999 UNIX Y2k38 problem (January 19, 2038 3:14:07 am)
*/
}
To convert .Net Ticks to millis in java use this :要在 java 中将 .Net Ticks 转换为 millis,请使用以下命令:
static final long TICKS_PER_MILLISECOND = 10000;
long ticks = 450000000000L; // sample tick value
long millis = (ticks / TICKS_PER_MILLISECOND);
There are 10,000 ticks in a millisecond, and C# considers the beginning of time January 1, 0001 at midnight.一毫秒内有 10,000 个滴答声,而 C# 认为时间的开始时间是 0001 年 1 月 1 日午夜。 Here's a one-liner which converts an Instant to ticks.
这是一个将 Instant 转换为刻度的单线。
public static long toTicks(Instant i)
{
return Duration.between(Instant.parse("0001-01-01T00:00:00.00Z"), i).toMillis() * 10000;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.