本文介绍了使用NodaTime解析DateTime字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用NodaTime解析Zoned DateTime字符串?我目前正在使用LocalDateTimePattern进行解析,但我认为产生的DateTime值将位于服务器的时区中.

  var模式= LocalDateTimePattern.CreateWithInvariantCulture(dateTimePattern);var parseResult = pattern.Parse(dateTimeString);如果(!parseResult.Success){//引发异常或您想执行的任何操作} 

当我查看野田时间API .但是,我无法使用它.我错过了什么吗?

解决方案

您没有说您正在使用哪个版本的Noda Time,但我怀疑它是1.1.0(即最新发布的版本)./p>

ZonedDateTimePattern 仅在Noda Time的开发版本中可用(发行时将变为1.2.0);如路线图所述,1.2.0将包括更好的文本处理,包括对 ZonedDateTime OffsetDateTime .

恐怕在1.1.x中无法直接解析 ZonedDateTime (如限制部分),尽管您可以分别解析 Instant 和时区名称,并构造一个手动设置ZonedDateTime .

How can we parse a Zoned DateTime string using NodaTime? I am currently using LocalDateTimePattern for parsing, but I think that the DateTime value yielded would be in the Server's timezone.

var pattern = LocalDateTimePattern.CreateWithInvariantCulture(dateTimePattern);

var parseResult = pattern.Parse(dateTimeString);
if (!parseResult.Success)
{
   // throw an exception or whatever you want to do
}

I came across ZonedDateTime pattern when I was looking at the Noda Time API. But, I am not able to use it. Am I missing out something?

解决方案

You don't say which version of Noda Time you're using, but I suspect it's 1.1.0 (that is, the latest released version).

ZonedDateTimePattern is only available in the development version of Noda Time (which will become 1.2.0 when it's released); as mentioned in the roadmap, 1.2.0 will include better text handling, including parsing for ZonedDateTime and OffsetDateTime.

I'm afraid that in 1.1.x there's no way to parse a ZonedDateTime directly (as noted in the limitations section of the 1.1.x user guide), though you could parse an Instant and a timezone name separately, and construct a ZonedDateTime by hand.

这篇关于使用NodaTime解析DateTime字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:43