提问人:Yosif Chumpov 提问时间:6/6/2023 最后编辑:Yosif Chumpov 更新时间:6/6/2023 访问量:68
DateTime.ParseExact 在日语中抛出 System.FormatException
DateTime.ParseExact throws System.FormatException in japanese
问:
我有以下抛出System.FormatException的代码。异常消息已本地化为日语,因此我怀疑这可能是由于计算机上的某些本地设置造成的。任何可能导致该问题的建议?
下面是简化代码:
string format = "yyyy-MM-dd HH-mm-ss";
using (var stream = new FileStream(@"...\New Text Document.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
stream.Seek(0, SeekOrigin.Begin);
stream.SetLength(0);
var test = new Test();
test.AccessDateTime = DateTime.Now.ToString(format);
DataContractSerializer serializer = new DataContractSerializer(typeof(Test));
var settings = new XmlWriterSettings { Indent = true };
using (var w = XmlWriter.Create(stream, settings))
{
serializer.WriteObject(w, openFiles);
}
}
DateTime d;
using (var stream = new FileStream(@"...\New Text Document.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
DataContractSerializer serializer = new DataContractSerializer(typeof(Test));
Test test = (Test)serializer.ReadObject(reader, true);
d = DateTime.ParseExact(test.AccessDateTime, format, CultureInfo.InvariantCulture);
}
[DataContract(Name = "Test")]
public class Test
{
[DataMember(Name = "AccessDateTime")]
public string AccessDateTime { get; set; }
}
迄今为止,我试图传递日本文化。ToString(..) 方法,但一切按预期工作,ParseExact 工作正常。
答: 暂无答案
上一个:SQL 日期时间列表示不正确
评论
DateTime.ToString(String)
使用当前区域性(可能是日语),但在转换回 时使用的是固定区域性。你想在这里做什么?在格式化日期时,也可以使用固定区域性吗?DateTime