從心開始

           

          系統時間修改方法

          java 修改系統時間方法

          第一種方法:

          需下載 jna.jar???????????????????????????

          a) 創建 Kernel32 接口

          package ?time.test;

          import ?com.sun.jna.Native;
          import ?com.sun.jna.Structure;
          import ?com.sun.jna.win32.StdCallLibrary;

          public ? interface ?Kernel32? extends ?StdCallLibrary
          {
          ????Kernel32?INSTANCE?
          = ?(Kernel32)Native.loadLibrary( " kernel32 " ,?Kernel32. class );
          ????
          public ?SYSTEMTIME?GetSystemTime();

          ????
          public ? void ?SetLocalTime(SYSTEMTIME?localTime);

          ????
          public ? static ? class ?SYSTEMTIME? extends ?Structure
          ????
          {
          ????
          // 必須有這么多個字段,按這個順序定義屬性
          ?
          ???????? public ? short ?wYear;
          ????????
          public ? short ?wMonth;
          ?????????????public?short?wDayOfWeek;
          ????????
          public ? short ?wDay;
          ????????
          public ? short ?wHour;
          ????????
          public ? short ?wMinute;
          ????????
          public ? short ?wSecond;
          ????????
          public ? short ?wMilliseconds;
          ???????
          ????}

          }

          b) 修改時間

          ?

          import ?time.test.Kernel32.SYSTEMTIME;


          public ? class ?SysTimeSettingDaoImp
          {
          ????
          protected ? void ?setLocalTime(String?time)
          ????
          {
          ??????
          // time時間格式是14位的字符串,如"20080108152130"
          ????????Short?year? = ?Short.parseShort(time.substring( 0 ,? 4 ));
          ????????Short?month?
          = ?Short.parseShort(time.substring( 4 ,? 6 ));
          ????????Short?day?
          = ?Short.parseShort(time.substring( 6 ,? 8 ));
          ????????Short?hour?
          = ?Short.parseShort(time.substring( 8 ,? 10 ));
          ????????Short?minute?
          = ?Short.parseShort(time.substring( 10 ,? 12 ));
          ????????Short?second?
          = ?Short.parseShort(time.substring( 12 ,? 14 ));

          ????????SYSTEMTIME?ss?
          = ? new ?SYSTEMTIME();
          ????????ss.setWYear(year);
          ????????ss.setWMonth(month);
          ????????ss.setWDay(day);
          ????????ss.setWHour(hour);
          ????????ss.setWMinute(minute);
          ????????ss.setWSecond(second);

          ????????Kernel32?lib?
          = ?Kernel32.INSTANCE;
          ????????lib.SetLocalTime(ss);
          ????}

          }

          第二種方法

          ?

          public ? class ?MyTimeClass
          {
          ????????????
          // timeAndDate格式位14位的字符串表示形式。
          ???????????? public ? void ?setLocalTime(String?timeAndDate)
          ????????????
          {
          ????????????????????????????String?date?
          = ?getDate(timeAndDate);
          ????????????????????????????String?time?
          = ?getTime(timeAndDate);
          ????????????
          ????????????????????????????
          // ?修改系統日期和時間
          ????????????????????????????Runtime.getRuntime().exec( " cmd?/c?date? " ? + ?date);
          ????????????????????????????Runtime.getRuntime().exec(
          " cmd?/c?time? " ? + ?time);
          ????????????}

          ????????????
          public ?String?getDate(String?timeAndDate)
          ????????????
          {
          ????????????????????String?year?
          = ?timeAndDate.substring( 0 ,? 4 );
          ????????????????????String?month?
          = ?timeAndDate.substring( 4 ,? 6 );
          ????????????????????String?day?
          = ?timeAndDate.substring( 6 ,? 8 );
          ????????????????????
          return ?year? + ? " - " ? + ?month? + ? " - " ? + ?day;
          ????????????}

          ????????????
          public ?String?getTime(String?timeAndDate)
          ????????????
          {
          ????????????????????String?hour?
          = ?timeAndDate.substring( 8 ,? 10 );
          ????????????????????String?minute?
          = ?timeAndDate.substring( 10 ,? 12 );
          ????????????????????String?second?
          = ?timeAndDate.substring( 12 ,? 14 );
          ????????????????????
          return ?hour? + ? " : " ? + ?minute? + ? " : " ? + ?second;
          ????????????}

          }

          Linux系統修改時間

          ??String?os? = ?System.getProperty( " os.name " ).toLowerCase(); // 獲取操作系統名稱

          if (os.indexOf( " windows " )? != ? - 1 )
          {
          ????cmd?
          = ? " cmd?/c?time? " ? + ?timeStr;
          ????ProcessUtil.printErr(Runtime.getRuntime().exec(cmd));
          ????cmd?
          = ? " cmd?/c?date? " ? + ?timeStr;
          ????ProcessUtil.printErr(Runtime.getRuntime().exec(cmd));
          ???}

          ???
          else
          ???
          {??
          ????cmd?
          = ? " date? " ? + ?timeStr; //timeStr時間到分,先寫時間再寫日期
          ????ProcessUtil.printErr(Runtime.getRuntime().exec(cmd));
          ???}


          public class ProcessUtil {
          ???
          ??? public static void printErr(Process p) {
          ??????? BufferedReader br = null;
          ??????? try {
          ??????????? br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
          ??????????? String line = null;
          ??????????? while ((line = br.readLine()) != null) {
          ??????????????? System.out.println(line);
          ??????????? }
          ??????? } catch (Exception e) {
          ??????????? e.printStackTrace();
          ??????? } finally {
          ??????????? try {
          ??????????????? if (br != null)
          ??????????????????? br.close();
          ??????????? } catch (Exception e) {
          ??????????????? e.printStackTrace();
          ??????????? }
          ??????????? p.destroy();
          ??????? }
          ??? }
          ???
          ??? public static void printConsole(Process p) {
          ??????? BufferedReader br = null;
          ??????? try {
          ??????????? br = new BufferedReader(new InputStreamReader(p.getInputStream()));
          ??????????? String line = null;
          ??????????? while ((line = br.readLine()) != null) {
          ??????????????? System.out.println(line);
          ??????????? }
          ??????? } catch (Exception e) {
          ??????????? e.printStackTrace();
          ??????? } finally {
          ??????????? try {
          ??????????????? if (br != null)
          ??????????????????? br.close();
          ??????????? } catch (Exception e) {
          ??????????????? e.printStackTrace();
          ??????????? }
          ??????? }
          ??? }
          ???
          ??? public static String getErrInfo(Process p) {
          ??????? StringBuffer sb = new StringBuffer();
          ??????? BufferedReader br = null;
          ??????? try {
          ??????????? br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
          ??????????? String line = null;
          ??????????? while ((line = br.readLine()) != null) {
          ??????????????? sb.append(line).append("\n");
          ??????????? }
          ??????????? return sb.toString();
          ??????? } catch (Exception e) {
          ??????????? e.printStackTrace();
          ??????? } finally {
          ??????????? try {
          ??????????????? if (br != null)
          ??????????????????? br.close();
          ??????????? } catch (Exception e) {
          ??????????????? e.printStackTrace();
          ??????????? }
          ??????? }
          ??????? return null;
          ??? }

          }


          ?

          posted on 2008-01-08 23:47 飄雪 閱讀(1955) 評論(1)  編輯  收藏 所屬分類: JAVA技術

          評論

          # re: 系統時間修改方法 2008-09-19 09:33 jone

          good
            回復  更多評論   

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類(11)

          隨筆檔案(13)

          收藏夾

          firends

          搜索

          最新評論

          • 1.?re: udp及tcp穿越NAT
          • 您上述提到的是互聯網之間的公網與私網之間的NAT穿越,3g終端可以通過這種方式實現嗎?還有3g移動設備的IP是動態分配的,我怎么才能在公網服務器找到這個3G終端?
          • --svurm
          • 2.?re: udp及tcp穿越NAT
          • TCP穿越針對的是公網IP,而這個公網ip進過幾個NAT,多少層映射到局域網客戶端上對大洞無影響,因為這些映射是nat完成的,一層,二層,三層,最終都映射到公網ip上,所以幾層NAT對打洞并無影響。
          • --lch
          • 3.?re: udp及tcp穿越NAT
          • 您好,感謝您提供的好介紹。請問:如果P2P的兩點之間,存在3-4個NAT,P2P也可以通起來嗎?從您對NAT的理解,如果通信兩端之間存在4個NAT,對那些應用有影響?
          • --xujf
          • 4.?re: 系統時間修改方法
          • good
          • --jone
          • 5.?re: udp及tcp穿越NAT
          • 評論內容較長,點擊標題查看
          • --...

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 元朗区| 黄平县| 宜宾县| 东兰县| 清徐县| 侯马市| 怀安县| 襄垣县| 青海省| 西盟| 工布江达县| 武乡县| 贵阳市| 双辽市| 遵化市| 瑞丽市| 岑溪市| 汤原县| 凌云县| 常熟市| 南宁市| 将乐县| 彭山县| 基隆市| 嘉义县| 游戏| 无锡市| 永兴县| 拉萨市| 贵南县| 灵武市| 北京市| 彰化市| 曲麻莱县| 麦盖提县| 基隆市| 汉寿县| 枞阳县| 张北县| 都江堰市| 临泉县|