全球化与DateTime.ParseExact [英] Globalization versus DateTime.ParseExact

查看:75
本文介绍了全球化与DateTime.ParseExact的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,


Jay和Herfried告诉我每次使用CDate时使用

datetime.parseexact始终是最好的方法字符串到日期时间

转换。他们不知道为什么我只能听他们,因为他们知道更好。


他们还说在商业情况下最好使用

datetime.parseexact来改变文化,而不是使用全球化

设置。我没有给他们这个样本,只告诉我的情况如何在我看来必须完成。杰伊在另一个线程中给了商业样本,他告诉我使用datetime.parseexact就是这样的方式。然而,在那个帖子中也没有告诉他们为什么以及为什么这更好,只有他,Herfried和一个C#MVP告诉了这一点,因此

它更好。


我从它做了一个小代码示例,让它更显示,可以

有人告诉我如何使用datetime.parseexact做得更好作为Jay

和Herfried不断讲述,为什么在这种文件可以来自世界各地的情况下更好地使用。
>

公共类测试

共享子主()

Dim d As新文件

Dim o As New订单

''接下来是XML文件或任何文件中的设置

d.culture =" nl-NL"

d .deliverydate =" 25 Aug 2004"

d.orderdate =" 10 juli 2004"

d.price ="? 111,10"

d.quantity =" 10.000"

将此错误视为错误= ConvertDocumentToOrder(d,o)

''自己进行调试打印

''这是另一个文件

d.culture =" en-US"

d.deliverydate =" 2004年8月25日 

d.orderdate =" 2004年7月10日"

d.price =" $ 111.10"

d。 quantity =" 10,000"

thisErrors = ConvertDocumentToOrder(d,o)

''自己进行调试打印

End Sub
公共共享函数ConvertDocumentToOrder(ByVal d As document,_

ByVal o as order)as Errors

将myError视为新错误

尝试

Threading.Thread.CurrentThread.CurrentCulture = _

New Globalization.CultureInfo(d.culture)

o.orderdate = CDate (d.orderdate)

o.deliverydate = CDate(d.deliverydate)

o.price = CDec(d.price)

o .quantity = CInt (d.quantity)

o.culture = d.culture

返回myError

Catch ex As Exception''无论错误设置如何

''评价(myerror,ex)

返回myError

最后

Threading.Thread.CurrentThread.CurrentCulture = _

Globalization.CultureInfo.InstalledUICulture

结束尝试

结束功能

结束班级

公共类文件

Public orderdate As String

公共交付日期为字符串

公开数量为字符串

公开价格作为字符串

公共文化为字符串

结束类

公共类订单''通常使用使用验证的属性

Public orderdate As DateTime

Public deliverydate As DateTime

公开数量为整数

公开价格为十进制

公共文化作为字符串

结束类

公共类错误

公共利益作为字符串

公共子新()

无论=""

结束子

结束类


我不能说有可能存在这样的情况:

datetime.parseexact更易于使用,但是在我看来,当你

确切地知道将在文档中使用的字符串的格式。

Hello everybody,

Jay and Herfried are telling me every time when I use CDate that using the
datetime.parseexact is always the best way to do String to datetime
conversions. They don''t tell why only that I have to listen to them because
they know it better.

They told also that in a business situation it is better to use
datetime.parseexact for changing cultures and not to use the globalization
setting. I did not give them this sample, only told the situation how that
in my opinion has to be done. Jay gave the business sample by the way in
another thread, where he told that using datetime.parseexact was the way to
go for that. However did in that thread as well not tell how and why it was
better, only that he, Herfried and a C# MVP were telling that and therefore
it was better.

I have made a little code example from it, to make it more showable, can
someone tell me how it can be done better using datetime.parseexact as Jay
and Herfried constantly are telling, and why that is better to use in this
kind of situations where the documents can come from all over the world.

Public Class test
Shared Sub main()
Dim d As New document
Dim o As New order
''The next is the setting from an XML file or whatever document
d.culture = "nl-NL"
d.deliverydate = "25 aug 2004"
d.orderdate = "10 juli 2004"
d.price = "? 111,10"
d.quantity = "10.000"
Dim thisErrors As Errors = ConvertDocumentToOrder(d, o)
''Do the debugprinting yourself
''This is another document
d.culture = "en-US"
d.deliverydate = "aug 25 2004"
d.orderdate = "july 10 2004"
d.price = "$ 111.10"
d.quantity = "10,000"
thisErrors = ConvertDocumentToOrder(d, o)
''Do the debugprinting yourself
End Sub
Public Shared Function ConvertDocumentToOrder(ByVal d As document, _
ByVal o As order) As Errors
Dim myError As New Errors
Try
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo(d.culture)
o.orderdate = CDate(d.orderdate)
o.deliverydate = CDate(d.deliverydate)
o.price = CDec(d.price)
o.quantity = CInt(d.quantity)
o.culture = d.culture
Return myError
Catch ex As Exception ''However whatever error set
''evaluate(myerror, ex)
Return myError
Finally
Threading.Thread.CurrentThread.CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture
End Try
End Function
End Class
Public Class document
Public orderdate As String
Public deliverydate As String
Public quantity As String
Public price As String
Public culture As String
End Class
Public Class order ''Normally with properties where validation is used
Public orderdate As DateTime
Public deliverydate As DateTime
Public quantity As Integer
Public price As Decimal
Public culture As String
End Class
Public Class Errors
Public whatever As String
Public Sub New()
whatever = ""
End Sub
End Class

With what I don''t say that there can be probably situations where
datetime.parseexact are simpler to use, however that in my opinion when you
know exactly the format of the string(s), that will be used in the document.

推荐答案

111.10"

d.quantity =" 10,000"

thisErrors = ConvertDocumentToOrder(d,o)

''自己进行调试打印
End Sub

公共共享函数ConvertDocumentToOrder(ByVal d As document,_

ByVal o as order)As Errors

Dim myError为新错误

尝试

Threading.Thread.CurrentThread.CurrentCulture = _

New Globalization.CultureInfo(d.culture)

o.orderdate = CDate(d.orderdate)

o.deliverydate = CDate(d.deliverydate)

o .price = CDec(d.price)

o.quantity = CInt(d.quantity)

o.culture = d.culture

返回myError

Catch ex As Exception''无论错误设置是什么

''评价(myerror,ex)

返回myError

最后

Threading.Thread.CurrentThread.CurrentCulture = _

Globalization.CultureInfo.InstalledUICulture

结束尝试

结束功能

结束类

公共类文件

公共orderdate为字符串

Public deliverydate As String

公开数量为字符串

公开价格为字符串

公共文化为字符串

结束类

公共类订单''通常使用使用验证的属性

公共订单日期作为日期时间

公共交付日期作为日期时间

公开数量作为整数

公开价格为十进制

公共文化为字符串

结束班级

公共类错误

公开任何字符串

公共子新()

无论=""

结束次级

结束班级


我不能说有可能会出现这样的情况

datetime.parseexact更易于使用,但是在我看来,当你确定字符串的格式时,将会在文档中使用。
111.10"
d.quantity = "10,000"
thisErrors = ConvertDocumentToOrder(d, o)
''Do the debugprinting yourself
End Sub
Public Shared Function ConvertDocumentToOrder(ByVal d As document, _
ByVal o As order) As Errors
Dim myError As New Errors
Try
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo(d.culture)
o.orderdate = CDate(d.orderdate)
o.deliverydate = CDate(d.deliverydate)
o.price = CDec(d.price)
o.quantity = CInt(d.quantity)
o.culture = d.culture
Return myError
Catch ex As Exception ''However whatever error set
''evaluate(myerror, ex)
Return myError
Finally
Threading.Thread.CurrentThread.CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture
End Try
End Function
End Class
Public Class document
Public orderdate As String
Public deliverydate As String
Public quantity As String
Public price As String
Public culture As String
End Class
Public Class order ''Normally with properties where validation is used
Public orderdate As DateTime
Public deliverydate As DateTime
Public quantity As Integer
Public price As Decimal
Public culture As String
End Class
Public Class Errors
Public whatever As String
Public Sub New()
whatever = ""
End Sub
End Class

With what I don''t say that there can be probably situations where
datetime.parseexact are simpler to use, however that in my opinion when you
know exactly the format of the string(s), that will be used in the document.


Cor Ligthert <无************ @ planet.nl> schrieb:
"Cor Ligthert" <no************@planet.nl> schrieb:
公共类测试
共享子主()
Dim d As New document
Dim o As new order
''接下来是从XML文件或任何文件设置
d.culture =" nl-NL"
d.deliverydate =" 25 Aug 2004"
d.orderdate =" 10 juli 2004"
d.price ="? 111,10
d.quantity =" 10.000"
将此错误视为错误= ConvertDocumentToOrder(d,o)
''自己进行调试打印
''这是另一份文件
d.culture =" en-US"
d.deliverydate =" aug 25 2004"
d.orderdate =" Julyly 2004 2004"
d。 price ="
Public Class test
Shared Sub main()
Dim d As New document
Dim o As New order
''The next is the setting from an XML file or whatever document
d.culture = "nl-NL"
d.deliverydate = "25 aug 2004"
d.orderdate = "10 juli 2004"
d.price = "? 111,10"
d.quantity = "10.000"
Dim thisErrors As Errors = ConvertDocumentToOrder(d, o)
''Do the debugprinting yourself
''This is another document
d.culture = "en-US"
d.deliverydate = "aug 25 2004"
d.orderdate = "july 10 2004"
d.price = "


111.10"
d.quantity =" 10,000"
thisErrors = ConvertDocumentToOrder(d,o)
''做自己调试打印
End Sub
公共共享函数ConvertDocumentToOrder(ByVal d作为文档,_
ByVal o作为顺序)作为错误
将myError视为新错误
尝试
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo(d.culture)
o.orderdate = CDate(d.orderdate)
o.deliveryda te = CDate(d.deliverydate)
o.price = CDec(d.price)
o.quantity = CInt(d.quantity)
o.culture = d.culture 返回myError
Catch ex As Exception''无论错误集如何评估(myerror,ex)
返回myError
最后
Threading.Thread.CurrentThread。 CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture
111.10"
d.quantity = "10,000"
thisErrors = ConvertDocumentToOrder(d, o)
''Do the debugprinting yourself
End Sub
Public Shared Function ConvertDocumentToOrder(ByVal d As document, _
ByVal o As order) As Errors
Dim myError As New Errors
Try
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo(d.culture)
o.orderdate = CDate(d.orderdate)
o.deliverydate = CDate(d.deliverydate)
o.price = CDec(d.price)
o.quantity = CInt(d.quantity)
o.culture = d.culture
Return myError
Catch ex As Exception ''However whatever error set
''evaluate(myerror, ex)
Return myError
Finally
Threading.Thread.CurrentThread.CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture




同样的错误。这不一定会将文化重置为之前使用的文化




我会使用<而不是使用''CDate'并改变文化br />
''DateTime.Parse''(='''Date.Parse'')并指定''DateTimeInfo'':


\\\\ \\ b
.... = Date.Parse(d.DiliveryDate,ci.DateTimeFormat)

///


。 ...'''ci''是您自定义日期/时间格式的自定义文化信息。


-

Herfried K. Wagner [MVP ]

< URL:http://dotnet.mvps.org/>



Same bug. This will not necessarily reset the culture to the culture used
before.

Instead of using ''CDate'' and changing the culture, I would use
''DateTime.Parse'' (= ''Date.Parse'') and specify the ''DateTimeInfo'':

\\\
.... = Date.Parse(d.DiliveryDate, ci.DateTimeFormat)
///

.... ''ci'' is your custom culture info with the custom date/time format set.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


这篇关于全球化与DateTime.ParseExact的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆