为什么更改Thread.CurrentThread.CurrentCulture会抛出一个日期而不是其他有效日期的错误

时间:2016-02-16 20:37:25

标签: vb.net

如果我使用02/16/16,我会收到错误消息。但是,如果我使用02/07/16它可以正常工作。

错误

C:\temp\.Net Exam Guide 2003\Exercises\Ch11\CDContent\Exercise 11-1>ChangeCultur
e.exe
Tuesday, February 16, 2016

Unhandled Exception: System.InvalidCastException: Conversion from string "02/16/
16" to type 'Date' is not valid.
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value)
   at Module1.Main()

代码
    进口系统     进口System.Globalization     进口System.Threading

Module Module1
    Sub Main()
        Dim DateString As String = "02/07/16"
        Dim TestDate As DateTime = DateString
        Console.WriteLine(TestDate.ToLongDateString)
        Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB")
        TestDate = DateString
        Console.WriteLine(TestDate.ToLongDateString)
    End Sub
End Module

预期结果

C:\temp\.Net Exam Guide 2003\Exercises\Ch11\CDContent\Exercise 11-1>ChangeCultur
e.exe
Sunday, February 07, 2016
02 July 2016

1 个答案:

答案 0 :(得分:0)

必须删除将文化更改为en-GB后出现的以下行

TestDate = TestString