本文檔主要為了解決實際開發當中,服務器和客戶端電腦時間不能相等的問題,純干貨,實際項目這種時間不同步的情況很多很多,時間不相等,到時候把本地的數據提交給服務器,服務器看實際上傳時間和我寫入數據庫時間相差好大,影響實際業務操作和判斷業務准確性,所以需要設置設備或者電腦的時間來為上傳提供准確的時間節點。
歡迎傳播分享,必須保持原作者的信息,但禁止將該文檔直接用於商業盈利。
本人自從幾年前走上編程之路,一直致力於收集和總結出好用的框架和通用類庫,不管是微軟自己的還是第三方的只要實際項目中好用且可以解決實際問題那都會收集好,編寫好文章和別人一起分享,這樣自己學到了,別人也能學到知識,當今社會很需要知識的搬運工。
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 設置日期
有時候只需要設置電腦的年月日
public static void SetLocalDate(int year, int month, int day)
3.2 設置時間
有時候只需要設置電腦的時分秒
public static void SetLocalTime(int hour, int min, int sec)
3.3 設置年月日時分秒
有時候只需要設置電腦的時分秒
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 }