아래 예제 코드를 SystemDateTimeSyncTest.java 파일로 작성한다.
실행시 반드시 루트 권한으로 실행하여야
public class SystemDateTimeSyncTest { public static void main(String[] args) { this.updateSystemDateTime("20100217161550"); // 2010-02-17 16:15:50 } private boolean updateSystemDateTime(String recvMsg) { String strDate = null; String[] cmds = null; BufferedReader br = null; Process proc = null; boolean result = false; try { strDate = recvMsg.substring(0, 4) + "-" + recvMsg.substring(4, 6) + "-" + recvMsg.substring(6, 8) + " " + recvMsg.substring(8, 10) + ":" + recvMsg.substring(10, 12) + ":" + recvMsg.substring(12, 14); cmds = new String[] { "/bin/date", "-s", strDate }; System.out.println("user.name: " + System.getProperty("user.name")); Runtime runtime = Runtime.getRuntime(); runtime.traceInstructions(true); runtime.traceMethodCalls(true); proc = runtime.exec(cmds); br = new BufferedReader(new InputStreamReader(proc.getInputStream())); String execLine = null; while( (execLine = br.readLine()) != null) { System.out.println("----------> " + execLine); } br.close(); br = null; br = new BufferedReader(new InputStreamReader(proc.getErrorStream())); while( (execLine = br.readLine()) != null) { System.out.println("----------> " + execLine); } proc.waitFor(); System.out.println("Exit value: " + proc.exitValue()); result = true; } catch(Exception e) { e.printStackTrace(); result = false; } finally { try { br.close(); } catch(Exception e2) {} br = null; cmds = null; strDate = null; proc = null; } return result; } }