LoginSignup
2
4

More than 5 years have passed since last update.

OSのカルチャ設定を自スレッドに対して上書きする

Posted at

アプリケーションの動作をOSのUIカルチャ(西暦・和暦など)に依存させたくない。

カレンダーの設定の変更から、カレンダーの種類を「和暦」にすると、タスクバーの日付が和暦表示になってとても便利。
2016-01-12.png

調べたところ、現在のスレッドのカルチャを上書き適用することができるらしい。
→ Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
(スコープは現在のスレッドなのでOSの設定を書き換えることはない。)

慌ててすべてのToStringやフォーマットに対してカルチャ設定をする必要はない。

hello.vb
Imports System.Globalization
Imports System.Threading
    Sub Main()
        Dim d As New Date(2016, 1, 23, 4, 56, 0)

        Console.WriteLine(CultureInfo.CurrentCulture.ToString)
        Console.WriteLine(d.ToString("yyyy-MMdd-hhmm"))'OS設定に依存
        Console.WriteLine(d.ToString("ggyy.MM.dd"))'OS設定に依存

        Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
        Console.WriteLine(CultureInfo.CurrentCulture.ToString)
        Console.WriteLine(d.ToString("yyyy-MMdd-hhmm"))’2016-0123-0456
        Console.WriteLine(d.ToString("ggyy.MM.dd"))'A.D.16.01.23

        Thread.CurrentThread.CurrentCulture = New CultureInfo("ja-JP")
        Console.WriteLine(CultureInfo.CurrentCulture.ToString)
        Console.WriteLine(d.ToString("yyyy-MMdd-hhmm"))'28-0123-0456
        Console.WriteLine(d.ToString("ggyy.MM.dd"))'平成28.01.23

        Console.ReadLine()

    End Sub

2016-01-12 (1).png

設定かくにん!よかった。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4