【C#公共帮助类】DateTimeHelper设置电脑本地时间,实际开发很需要

  本文档主要为了解决实际开发当中,服务器和客户端电脑时间不能相等的问题,纯干货,实际项目这种时间不同步的情况很多很多,时间不相等,到时候把本地的数据提交给服务器,服务器看实际上传时间和我写入数据库时间相差好大,影响实际业务操作和判断业务准确性,所以需要设置设备或者电脑的时间来为上传提供准确的时间节点。

  欢迎传播分享,必须保持原作者的信息,但禁止将该文档直接用于商业盈利。

  本人自从几年前走上编程之路,一直致力于收集和总结出好用的框架和通用类库,不管是微软自己的还是第三方的只要实际项目中好用且可以解决实际问题那都会收集好,编写好文章和别人一起分享,这样自己学到了,别人也能学到知识,当今社会很需要知识的搬运工。

     Email:707055073@qq.com

  本文章地址:http://www.cnblogs.com/wohexiaocai/p/5721906.html

1.基本介绍

      实际开发中,我们需要测试电脑时间不是准确时间情况下对业务的影响就需要用到这个类,帮我们设置电脑正确的时间点。

     一般我们是设置用户的电脑时间,不是服务器的时间哦,服务器的时间可以用NTP来进行统一设置的,科普:http://baike.baidu.com/link?url=Bq6U-qi2UFJSqMXaBJXNLemE69CEfpfrJmAx4HflvHgRIjP1v6hI2YZPyfabcgnjGNXP-yykPprAPtgRU3czna

2.如何获取正确的时间

要想获取到正确的时间,一般将我们服务器的时间获取到就可以,服务器的时间一般都是联网的,时间比较准确,实际项目中我们就是通过调用服务器的接口获取到当前时间的,这个并不难
 
复制代码
 1 #region public static string GetResponse(string url)
 2         /// <summary>
 3         /// 获取一个网页
 4         /// </summary>
 5         /// <param name="url">地址</param>
 6         /// <returns>字符串返回值</returns>
 7         public static string GetResponse(string url)
 8         {
 9             string result = null;
10             WebResponse webResponse = null;
11             StreamReader streamReader = null;
12             try
13             {
14                 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
15                 httpWebRequest.Method = "GET";
16                 webResponse = httpWebRequest.GetResponse();
17                 streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
18                 result = streamReader.ReadToEnd();
19             }
20             catch
21             {
22                 // handle error
23             }
24             finally
25             {
26                 if (streamReader != null)
27                 {
28                     streamReader.Close();
29                 }
30                 if (webResponse != null)
31                 {
32                     webResponse.Close();
33                 }
34             }
35             return result;
36         }
37         #endregion
获取时间
复制代码

3.设置电脑时间

得到时间就需要进行设置了,一般Windows系统都是通过DOS命令来设置电脑的时间所以这里也用了C#的DOS来设置

3.1 设置日期

 有时候只需要设置电脑的年月日

1
public static void SetLocalDate(int year, int month, int day)

3.2 设置时间

 有时候只需要设置电脑的时分秒

1
public static void SetLocalTime(int hour, int min, int sec)

3.3 设置年月日时分秒

 有时候只需要设置电脑的时分秒

1
public static void SetLocalDateTime(DateTime time)

4.实战效果

经过win7、XP、windows server等测试后可以正常设置电脑时间,请大家可以正常使用,不实战怎么敢拿出来直接用呢?

5.源码下载

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace ConsoleApplication1
 9 {
10     public class DateTimeHelper
11     {
12         /// <summary>
13         /// 设置本地电脑的年月日
14         /// </summary>
15         /// <param name="year"></param>
16         /// <param name="month"></param>
17         /// <param name="day"></param>
18         public static void SetLocalDate(int year, int month, int day)
19         {
20             //实例一个Process类,启动一个独立进程 
21             Process p = new Process();
22             //Process类有一个StartInfo属性 
23             //设定程序名 
24             p.StartInfo.FileName = "cmd.exe";
25             //设定程式执行参数 “/C”表示执行完命令后马上退出
26             p.StartInfo.Arguments = string.Format("/c date {0}-{1}-{2}", year, month, day);
27             //关闭Shell的使用 
28             p.StartInfo.UseShellExecute = false;
29             //重定向标准输入 
30             p.StartInfo.RedirectStandardInput = true;
31             p.StartInfo.RedirectStandardOutput = true;
32             //重定向错误输出 
33             p.StartInfo.RedirectStandardError = true;
34             //设置不显示doc窗口 
35             p.StartInfo.CreateNoWindow = true;
36             //启动 
37             p.Start();
38             //从输出流取得命令执行结果 
39             p.StandardOutput.ReadToEnd();
40         }
41 
42         /// <summary>
43         /// 设置本机电脑的时分秒
44         /// </summary>
45         /// <param name="hour"></param>
46         /// <param name="min"></param>
47         /// <param name="sec"></param>
48         public static void SetLocalTime(int hour, int min, int sec)
49         {
50             //实例一个Process类,启动一个独立进程 
51             Process p = new Process();
52             //Process类有一个StartInfo属性 
53             //设定程序名 
54             p.StartInfo.FileName = "cmd.exe";
55             //设定程式执行参数 “/C”表示执行完命令后马上退出
56             p.StartInfo.Arguments = string.Format("/c time {0}:{1}:{2}", hour, min, sec);
57             //关闭Shell的使用 
58             p.StartInfo.UseShellExecute = false;
59             //重定向标准输入 
60             p.StartInfo.RedirectStandardInput = true;
61             p.StartInfo.RedirectStandardOutput = true;
62             //重定向错误输出 
63             p.StartInfo.RedirectStandardError = true;
64             //设置不显示doc窗口 
65             p.StartInfo.CreateNoWindow = true;
66             //启动 
67             p.Start();
68             //从输出流取得命令执行结果 
69             p.StandardOutput.ReadToEnd();
70         }
71 
72         /// <summary>
73         /// 设置本机电脑的年月日和时分秒
74         /// </summary>
75         /// <param name="time"></param>
76         public static void SetLocalDateTime(DateTime time)
77         {
78             SetLocalDate(time.Year, time.Month, time.Day);
79             SetLocalTime(time.Hour, time.Minute, time.Second);
80         }
81     }
82 }
复制代码

 

posted @   小菜鸟叽叽喳喳  阅读(3697)  评论(7编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2013-08-10 老毛桃安装Win8(哪里不会点哪里,so easy)
点击右上角即可分享
微信分享提示