using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace ZB.QueueSys.Common { public class SetTimeHelper { //定义一个用于保存静态变量的实例 private static SetTimeHelper instance = null; //定义一个保证线程同步的标识 private static readonly object locker = new object(); //构造函数为私有,使外界不能创建该类的实例 private SetTimeHelper() { } public static SetTimeHelper Instance { get { if (instance == null) { lock (locker) { if (instance == null) instance = new SetTimeHelper(); } } return instance; } } [DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")] public static extern int GetSystemDefaultLCID(); [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")] public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData); public const int LOCALE_SLONGDATE = 0x20; public const int LOCALE_SSHORTDATE = 0x1F; public const int LOCALE_STIME = 0x1003; //[DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")] //public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData); [DllImport("user32.dll", EntryPoint = "SendMessageTimeout")] public static extern long SendMessageTimeout(int hWnd, int Msg, int wParam, int lParam, int fuFlags, int uTimeout, ref int lpdwResult); [DllImport("User32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage( int hWnd, // handle to destination window int Msg, // message int wParam, // first message parameter int lParam // second message parameter ); public const int LOCALE_USER_DEFAULT = 0x0400; public const int LOCALE_SYSTEM_DEFAULT = 0x0800; //public const int LOCALE_SSHORTDATE = 0x1F; public const int LOCALE_STIMEFORMAT = 0x1003; public const int HWND_BROADCAST = 0xFFFF; public const int WM_SETTINGCHANGE = 0x001A; public const int SMTO_ABORTIFHUNG = 2; public void SetDateTimeFormat() { try { int x = GetSystemDefaultLCID(); SetLocaleInfo(x, LOCALE_STIME, "HH:mm:ss"); //时间格式 SetLocaleInfo(x, LOCALE_SSHORTDATE, "yyyy-MM-dd"); //短日期格式 SetLocaleInfo(x, LOCALE_SLONGDATE, "yyyy-MM-dd"); //长日期格式 } catch (Exception ex) { //Console.WriteLine(ex); } } public void SetDateTimeFormat2() { int p = 0; //设置短日期格式 SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, "yyyy-MM-dd"); //设置时间格式,24小时制 SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, "HH:mm:ss"); //设置完成后必须调用,通知其他程序格式已经更改,否则即使是程序自身也不能使用新设置的格式 SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0, SMTO_ABORTIFHUNG, 10, ref p); } public void ConvertDateTimeFormat() { SetDateTimeFormat(); SetDateTimeFormat2(); } } }