获取北京时间

 public static DateTime GetBeijingTime()
{
DateTime dt; // 返回国际标准时间
// 只使用 timeServers 的 IP 地址,未使用域名
try
{
string[,] timeServers = new string[, ];
int[] searchOrder = { , , , , , , , , , , , , };
timeServers[, ] = "time-a.nist.gov";
timeServers[, ] = "129.6.15.28";
timeServers[, ] = "time-b.nist.gov";
timeServers[, ] = "129.6.15.29";
timeServers[, ] = "time-a.timefreq.bldrdoc.gov";
timeServers[, ] = "132.163.4.101";
timeServers[, ] = "time-b.timefreq.bldrdoc.gov";
timeServers[, ] = "132.163.4.102";
timeServers[, ] = "time-c.timefreq.bldrdoc.gov";
timeServers[, ] = "132.163.4.103";
timeServers[, ] = "utcnist.colorado.edu";
timeServers[, ] = "128.138.140.44";
timeServers[, ] = "time.nist.gov";
timeServers[, ] = "192.43.244.18";
timeServers[, ] = "time-nw.nist.gov";
timeServers[, ] = "131.107.1.10";
timeServers[, ] = "nist1.symmetricom.com";
timeServers[, ] = "69.25.96.13";
timeServers[, ] = "nist1-dc.glassey.com";
timeServers[, ] = "216.200.93.8";
timeServers[, ] = "nist1-ny.glassey.com";
timeServers[, ] = "208.184.49.9";
timeServers[, ] = "nist1-sj.glassey.com";
timeServers[, ] = "207.126.98.204";
timeServers[, ] = "nist1.aol-ca.truetime.com";
timeServers[, ] = "207.200.81.113";
timeServers[, ] = "nist1.aol-va.truetime.com";
timeServers[, ] = "64.236.96.53";
int portNum = ;
byte[] bytes = new byte[];
int bytesRead = ;
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
for (int i = ; i < ; i++)
{
string hostName = timeServers[searchOrder[i], ];
try
{
client.Connect(hostName, portNum);
System.Net.Sockets.NetworkStream ns = client.GetStream();
bytesRead = ns.Read(bytes, , bytes.Length);
client.Close();
break;
}
catch (Exception)
{
// ignored
}
}
char[] sp = new char[];
sp[] = ' ';
dt = new DateTime();
string str1 = System.Text.Encoding.ASCII.GetString(bytes, , bytesRead); string[] s = str1.Split(sp);
if (s.Length >= )
{
dt = DateTime.Parse(s[] + " " + s[]); // 得到标准时间
dt = dt.AddHours(); // 得到北京时间
}
else
{
dt = DateTime.Parse("2016-1-1");
}
}
catch (Exception)
{
dt = DateTime.Parse("2016-1-1");
}
return dt;
}

设置本地系统时间

 [DllImport("kernel32.dll")]
private static extern bool SetLocalTime(ref Systemtime time); [StructLayout(LayoutKind.Sequential)]
private struct Systemtime
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
} public static bool SetDate(DateTime dt)
{
Systemtime st; st.year = (short)dt.Year;
st.month = (short)dt.Month;
st.dayOfWeek = (short)dt.DayOfWeek;
st.day = (short)dt.Day;
st.hour = (short)dt.Hour;
st.minute = (short)dt.Minute;
st.second = (short)dt.Second;
st.milliseconds = (short)dt.Millisecond; bool rt = SetLocalTime(ref st);
return rt;
}

C#获取北京时间与设置系统时间的更多相关文章

  1. Qt设置系统时间(使用SetSystemTime API函数)

    大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime();  ...

  2. ubuntu设置系统时间与网络时间同步和时区

    Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...

  3. centos7设置系统时间与网络时间同步

    Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...

  4. 使用AIDL调用远程服务设置系统时间

    在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的.于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间 ...

  5. date 显示或设置系统时间和日期

    显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...

  6. Linux 设置系统时间和日期 API

    嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码. Linux设置时间和日期的步骤: 1. 设置系统时间和日期: 2. 该系统的时间和日期,同步到硬件. #include ...

  7. ubuntu设置系统时间与网络时间同步

    ubuntu设置系统时间与网络时间同步   Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC).   系统时间:指当前Linux Ker ...

  8. delphi中设置系统时间方法

    procedure TMainFrm.Timer1Timer(Sender: TObject); var   systemtime:Tsystemtime;   dt:TDateTime; begin ...

  9. CentOS设置系统时间、硬件时间、以及定时校对时间

    CentOS设置系统时间和时区 一.设置时区 方法一:使用setup工具 setup 选择Timezone configuration 选择Asia/Shanghai 空格键勾选上System clo ...

随机推荐

  1. spring.net 配置文件需要注意换行问题

    今天在做Spring.NET Demo时写配置文件写ObjectNames 节点的Value成这样 <object id="ProxyCreator" type=" ...

  2. C# 扩展类

    C# 中提供一个非常实用的供能,扩展方法(Extension method) 扩展方法是通过额外的静态方法扩展现有的类型.通过扩展方法,可以对已有类型做自己想做的相关扩展.方法:定义静态类,扩展方法也 ...

  3. 总结js中数据类型的bool值及其比较

    首先需要知道的是,js中有6个值为false,分别是: 0, '', null, undefined, NaN 和 false, 其他(包括{}, [], Infinity)为true. 可以使用Bo ...

  4. 红帽RHEL7版本RHCE认证学习及考试经历

    RHCE是红帽公司推出的Linux系统的中级认证,考试全部采取上机形式,在考察考生的基础理论知识的同时还能考察实践动手操作能力,在Linux领域的价值是不可否认的,所以对于期望从事相关行业的同学们在上 ...

  5. [转载]再来重新认识JavaEE完整体系架构

    移步: http://www.jizhuomi.com/software/644.html

  6. java筛选法求素数

    这本身没什么,代码一堆 发来纪念下而已 本来刚学习java,编写输出100以内的素数 对于我这个有代码运行性能洁癖的人(但是本身又不懂算法)来说,不能忍 于是看了些资料 参考: http://blog ...

  7. c# 中int.ToString()的格式化的示例

    格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式说明符,指定格式化类型,xx 为精度说明符, ...

  8. Ubuntu14.04使用apt-fast来加快apt-get下载的教程

    代码如下: $ sudo add-apt-repository ppa:saiarcot895/myppa $ sudo apt-get update $ sudo apt-get install a ...

  9. Newtonsoft.Json 序列化和反序列化 时间格式

    From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...

  10. Binding ConvererParameter

    WPF中ConverterParameter不可以绑定,可以通过如下扩展来实现ConverterParameter的Binding: 1.自定义ConverterBindableBinding和Mul ...