获取北京时间

 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. Python 对不均衡数据进行Over sample(重抽样)

    需要重采样的数据文件(Libsvm format),如heart_scale +1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.4 ...

  2. 错误: java.lang.reflect.InvocationTargetException

    错误: java.lang.reflect.InvocationTargetException    at sun.reflect.NativeMethodAccessorImpl.invoke0(N ...

  3. a 标签中调用js的几种方法 文章摘自他人

    我们常用的在a标签中有点击事件:1. a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题 ...

  4. Android6.0获取权限

    照着<第一行代码>打代码,然并卵,感叹技术进步的神速.最后提醒一点:IT类的书籍一定要注意出版时间!出版时间!出版时间!重要的事情说三遍 问题出在android6.0的权限获取问题上,以前 ...

  5. POJ1635 树的最小表示

    /*zoj1990Subway Tree Systems题目大意:初始时站在树的根节点,若朝着远离根的方向走,记录“”,接近根的方向走记录“”.并且树的每一条边只能来回走一次(即向下和返回).一个合法 ...

  6. 用 QGIS 画矢量交通路线图

    一.准备工作 1.安装插件 为了方便画图,我们安装了OpenLayers,QuickOSM两个插件. 如何安装插件,度娘上都有答案.下图中打勾的部分为安装好的插件: OpenLayers提供了一些开放 ...

  7. 解析txt文本,dom4j工具输出为xml文档

    有如下一个ttl.txt文本文档,每一行用空格隔开的三段分别代表主谓宾, 要将它们输出为xml格式文档 工具:dom4j,jar包导入MyEclipse的Java Project工程 代码如下: pa ...

  8. Fragment的startActivityForResult和Activity的startActivityForResult的区别

    2016-08-30 18:22:33 前提:我们的APP要兼容Api level 11以前的,所以必须用FragmentActivity 1.对于Fragment的,我们很多时候都会在Activit ...

  9. Redis集群部署

    1.1.1redis简介 Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志 型. Key-Value数据库 1.1.2redis常见使用场景 1.会话缓存(S ...

  10. C++小项目:directx11图形程序(二):systemclass

    先上代码: systemclass.h #pragma once #include"graphicsclass.h" const bool FULLSCREEN = true; c ...