2017-04-25 25 views
1

我正在使用代理Java應用程序,它安裝在世界不同地方的幾臺Windows機器上。我想定期同步窗口時鐘(日期和時間)。我已經發現了本機命令通過Java代碼來設置窗口時間:由java應用程序在windows機器上設置日期和時間的最佳方式

Runtime.getRuntime().exec("cmd /C date " + strDateToSet); // dd-MM-yy 
Runtime.getRuntime().exec("cmd /C time " + strTimeToSet); // hh:mm:ss 

或執行

Runtime.getRuntime().exec("cmd /C date " + strDateToSet + "& time " + strTimeToSet); 

但主要的問題是focalizied上設定的日期,因爲有可能是日期格式在Windows機器上的所有機器都不一樣。例如,對於意大利機器,我可以有dd-MM-yy,對於美國機器,我可以有yy-MM-dd。所以如果我的應用程序設置日期格式dd-MM-yy對美國機器來說是錯誤的。

知道我cant't使用NTP(機器到局域網防火牆規則出唯一協議HTTPS端口443) 我怎麼能爲所有Windows機器設置正確日期由java應用程序? 哪一個是最好的解決方案semplicity和可維護性?

注意代理的Java應用程序已經時間戳將Windows機器通過網絡服務響應傳遞設置,因此只做setDateAndTime

與格式日期YYYY TEST EXEC date命令必要-MM-dd在Windows上(設置錯誤的日期): enter image description here

+3

爲什麼你不能使用NTP?因爲這是(IMO)簡單和可維護性最好的**解決方案。 –

+3

舊學校但:http://stackoverflow.com/a/17035731/180100 – 2017-04-25 15:58:35

+0

可能:https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#getDateInstance (int )它使用默認的語言環境。 – assylias

回答

2

我試圖用JNA導入kernel32.dll執行解決方案在Windows 7機器上用時區UTC + 1(意大利國家)執行測試。

我描述的步驟:

1)我進口我的Maven項目的遵循依賴:

<dependency> 
     <groupId>net.java.dev.jna</groupId> 
     <artifactId>jna-platform</artifactId> 
     <version>4.4.0</version> 
</dependency> 
<dependency> 
     <groupId>net.java.dev.jna</groupId> 
     <artifactId>jna</artifactId> 
     <version>4.3.0</version> 
</dependency> 

2)我實現被跟隨類:

import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.stereotype.Component; 

import com.sun.jna.Native; 
import com.sun.jna.platform.win32.WinBase.SYSTEMTIME; 
import com.sun.jna.win32.StdCallLibrary; 


@Component 
@Qualifier("windowsSetSystemTime") 
public class WindowsSetSystemTime { 

    /** 
    * Kernel32 DLL Interface. kernel32.dll uses the __stdcall calling 
    * convention (check the function declaration for "WINAPI" or "PASCAL"), so 
    * extend StdCallLibrary Most C libraries will just extend 
    * com.sun.jna.Library, 
    */ 
    public interface Kernel32 extends StdCallLibrary { 

     boolean SetLocalTime(SYSTEMTIME st); 

     Kernel32 instance = (Kernel32) Native.loadLibrary("kernel32.dll", Kernel32.class); 

    } 

    public boolean SetLocalTime(SYSTEMTIME st) { 
     return Kernel32.instance.SetLocalTime(st); 
    } 

    public boolean SetLocalTime(short wYear, short wMonth, short wDay, short wHour, short wMinute, short wSecond) { 
     SYSTEMTIME st = new SYSTEMTIME(); 
     st.wYear = wYear; 
     st.wMonth = wMonth; 
     st.wDay = wDay; 
     st.wHour = wHour; 
     st.wMinute = wMinute; 
     st.wSecond = wSecond; 
     return SetLocalTime(st); 
    } 
} 

三)測試班我試着設置相關日期和時間

public void setTime(){ 
     System.out.println("START SYNC " + windowsSetSystemTime); 

     windowsSetSystemTime.SetLocalTime((short)2017, (short)10,(short) 29,(short) 11,(short) 35,(short) 0); 
    } 

測試結果: 因爲在這種情況下我獲得了正確的日期和時間,因爲該功能考慮了在2017年10月29日3:00進入的日光節約時間。

測試之前,時鐘設定:

enter image description here

試驗後時鐘組:

enter image description here

我發現了邏輯SetLocalTime方法引入的Kernel32。由Windows開發中心文檔在鏈接DLL: SetLocalTime documentation

Windows開發人員中心備註 SetLocalTime:

系統使用UTC內部。因此,當您調用SetLocalTime時,系統將使用當前時區信息執行轉換,包括夏令時設置。請注意,系統使用當前時間的夏令時設置,而不是您設置的新時間。因此,爲確保正確的結果,現在第一次調用已更新夏令時設置,再次調用SetLocalTime。

相關問題