向服务提供者公开Java服务提供者接口(Expose a Java Service Provider Interface to Service Providers)
我正在尝试为插件/应用程序创建一个框架,我想我已经决定使用Java中的服务提供程序接口(SPI)来实现这一点。 最终的愿景是人们可以下载插件(如应用程序商店),然后将jar放入文件夹,他们将使用现有程序。
所以我有一个
PluginInterface
类,它将成为我的SPI。 我希望所有插件都实现这个类(它有基本方法,如doesAccept
,handle
等)。 由于第三方将创建服务提供者, 我如何让他们在他们的代码中引用这个Java接口(以便他们可以实现它),而不实际给他们主应用程序的源代码?I'm trying to create a framework for plugin/apps, and I think I've settled on using a Service Provider Interface (SPI) in Java to achieve that. The ultimate vision is that people can download plugins (like an app store) and then just drop the jars into a folder, and they'll work with the existing program.
So I have a class
PluginInterface
that will be my SPI. I want all plugins to implement this class (it has basic methods likedoesAccept
,handle
, etc.). Since 3rd parties will be creating the Service Providers, how can I let them reference this Java interface in their code (so they can implement it), without actually giving them the source code of the main application?
原文:https://stackoverflow.com/questions/30135177
最满意答案
现有答案中有很好的信息,但让我尝试一个实用的总结:
System.DateTime.ToFileTime()
和System.DateTime.ToFileTimeUtc()
之间的区别唯一重要的是当实例的.Kind
属性等于Unspecified
,即,如果不清楚时间值是表示本地时间还是UTC时间。
这个信息实际上是Hans Passant的一个答案,他自己删除了。换句话说:对于其
.Kind
属性等于Local
或Utc
(唯一其他可能的值)的System.DateTime
实例,System.DateTime.ToFileTime()
和System.DateTime.ToFileTimeUtc()
行为相同 - 这是OP经历的,因为他的输入值是.Kind
Local
(File.GetCreationTime()
返回的那种)。请注意,设计的相关
System.DateTimeOffset
类型始终带有显式时区信息,因此从不存在歧义,这就是为什么该类型只有.ToFileTime()
方法。正如其他人所说, 返回值始终代表UTC时间 :
根据定义,文件时间值总是表示UTC中的时间点。
因此,
.ToFileTime()
和.ToFileTimeUtc()
创建一个时间点,表示为自1601年1月1日午夜以来100ns间隔的计数。为了补充OP自己的例子,其中
.ToFileTime()
和.ToFileTimeUtc()
的结果不同:// Create a DateTime instance with .Kind equal to Unspecified; // use the earliest date that can be represented as a file time. DateTime dtUnspecified = DateTime.Parse("1601-01-01"); Console.WriteLine( dtUnspecified.ToFileTime() + "\n" + dtUnspecified.ToFileTimeUtc() );
美国东部时区的样本输出:
180000000000 // 1601-01-01T05:00:00Z - 5 AM UTC 0 // 1601-01-01T00:00:00Z - midnight UTC
There's good information in the existing answers, but let me attempt a pragmatic summary:
The only time the distinction between
System.DateTime.ToFileTime()
andSystem.DateTime.ToFileTimeUtc()
matters is when the instance's.Kind
property equalsUnspecified
, i.e., if it's unclear whether the time value represents a local or UTC time.
This information was actually in an answer by Hans Passant that he himself deleted.In other word: for
System.DateTime
instances whose.Kind
property equals eitherLocal
orUtc
(the only other possible values),System.DateTime.ToFileTime()
andSystem.DateTime.ToFileTimeUtc()
behave identically - this is what the OP experienced, because his input value was of.Kind
Local
(the kind returned byFile.GetCreationTime()
).Note that the related
System.DateTimeOffset
type by design always carries explicit time zone information, so there's never ambiguity, which is why that type only has a.ToFileTime()
method.As others have noted, the return value always represents a UTC time:
File time values by definition, invariably represent a point in time in UTC.
Thus, both
.ToFileTime()
and.ToFileTimeUtc()
create a point in time expressed as the count of 100ns intervals since midnight January 1, 1601 UTC.To complement the OP's own example with one where
.ToFileTime()
and.ToFileTimeUtc()
do differ in outcome:// Create a DateTime instance with .Kind equal to Unspecified; // use the earliest date that can be represented as a file time. DateTime dtUnspecified = DateTime.Parse("1601-01-01"); Console.WriteLine( dtUnspecified.ToFileTime() + "\n" + dtUnspecified.ToFileTimeUtc() );
Sample output in the US Eastern time zone:
180000000000 // 1601-01-01T05:00:00Z - 5 AM UTC 0 // 1601-01-01T00:00:00Z - midnight UTC
相关问答
更多-
从DateTime开始: long DateTime.Ticks 至DateTime from long: new DateTime(long) To long from DateTime: long DateTime.Ticks To DateTime from long: new DateTime(long)
-
WPF XAML StringFormat DateTime:输出错误的文化?(WPF XAML StringFormat DateTime: Output in wrong culture?)[2022-01-01]
请参阅我对StringFomat本地化问题的回答 Please see my answer on StringFomat Localization problem -
你可以使用这个: DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss"); You can use this: DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");
-
好吧便便。 它可以这么简单吗? public static long ToFileTimeUtc(DateTime dateTime) { return dateTime.ToFileTimeUtc(); } public static DateTime FromFileTimeUtc(long fileTimeUtc) { return DateTime.FromFileTimeUtc(fileTimeUtc); } 注释? 我可以不这样做吗? Well poo. Could it be as ...
-
DateTime类似乎不支持区域数据作为区域名称。 但Time类正确地做到了这一点。 所以要么这样做: require 'date' require 'time' Time.parse('...').strftime('%Z') 或者,如果你已经有了DateTime格式的数据,那么: Time.parse(DateTime.parse('...').to_s).strftime('%Z') The DateTime class does not seem to support zone data as ...
-
最简单的方法 - 像这样扩展你的模型: public class PersonModel { public int Id {get; set;} public DateTime BirthDay {get;set;} // add this property and you get it. You can rename it if you want public string BirthDate {get {return this.BirthDay.ToShourtDateS ...
-
DateTime ToFileTime和ToFileTimeUtc具有相同的输出(DateTime ToFileTime and ToFileTimeUtc have same output)[2022-11-04]
现有答案中有很好的信息,但让我尝试一个实用的总结: System.DateTime.ToFileTime()和System.DateTime.ToFileTimeUtc()之间的区别唯一重要的是当实例的.Kind属性等于Unspecified ,即,如果不清楚时间值是表示本地时间还是UTC时间。 这个信息实际上是Hans Passant的一个答案,他自己删除了。 换句话说:对于其.Kind属性等于Local或Utc (唯一其他可能的值)的System.DateTime实例, System.DateTime. ... -
在解决方案中,在解析日期时间时会隐式使用本地时区 。 你应该用 val t = DateTime.parse("07-01-86", DateTimeFormat.forPattern(pattern).withZoneUTC()) 强制在UTC区域中创建DateTime 。 然后,millis是520560000000 。 无需再执行toDateTime(DateTimeZone) 。 否则,与您的建设 val t = DateTime.parse("07-01-86", DateTimeFormat. ...
-
Date.Now.ToFileTime.ToString命令(VB.NET)出现问题(Trouble with Date.Now.ToFileTime.ToString command (VB.NET))[2022-08-30]
如果您只想使用您提交的代码,它将是: Private Sub writeLog() Using outfile As StreamWriter = New StreamWriter(String.Format("{0:yyyyMMdd_HHmmss}_log_detailed.txt", Now), True) outfile.Write(sb_detailled.ToString()) End Using Using outfile As StreamWrite ... -
DATETIME比较?(DATETIME comparisons?)[2021-08-13]
我自己刚刚发现了这个,但这里有一个简单的例子,告诉你它是如何工作的。 @ GordonLinoff的答案既不在这里也不在那里,因为你的问题并不是关于utc_timestamp()返回的格式。 你真正问的是当你在时间戳上使用数字操作数+和-时,MySQL会返回什么格式。 我倾向于同意你的观点,文档在这个主题上有点模糊。 但这就是我发现的。 您可以自己构建此视图,以更简单的方式查看示例。 create view cbhview as select utc_timestamp() as nowtime, ...