從心開始

           

          系統(tǒng)時間修改方法

          java 修改系統(tǒng)時間方法

          第一種方法:

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

          a) 創(chuàng)建 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);
          ????????????
          ????????????????????????????
          // ?修改系統(tǒng)日期和時間
          ????????????????????????????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系統(tǒng)修改時間

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

          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技術(shù)

          評論

          # re: 系統(tǒng)時間修改方法 2008-09-19 09:33 jone

          good
            回復(fù)  更多評論   

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類(11)

          隨筆檔案(13)

          收藏夾

          firends

          搜索

          最新評論

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

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 佛坪县| 万年县| 东阿县| 常山县| 安乡县| 石泉县| 天镇县| 永春县| 阳朔县| 章丘市| 宁晋县| 安西县| 永泰县| 郸城县| 师宗县| 福建省| 甘谷县| 淅川县| 太白县| 土默特右旗| 富阳市| 大竹县| 阜宁县| 海南省| 商南县| 旅游| 固阳县| 临湘市| 广元市| 信宜市| 江孜县| 普格县| 东莞市| 仁化县| 乐昌市| 常熟市| 焉耆| 新疆| 开化县| 特克斯县| 长宁县|