zoukankan      html  css  js  c++  java
  • C# 设置本机日期格式

    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();
            }
    
    
    
        }
    }
    

      

    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    富士康前端一面
    中孚前端面筋
    至真科技校招前端面筋
    SysFader:iexplore.exe错误
    设置文件属性
    SQL2000数据库定期自动备份与修改
    如何配置jdk1.5的环境变量及运行java程序
    ResultSet的getDate()、getTime()和getTimestamp()比较
    Java BigDecimal详解
    MVVM教程(一):MVVM简介与准备工作
  • 原文地址:https://www.cnblogs.com/YYkun/p/14788839.html
Copyright © 2011-2022 走看看