<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    從心開始

     

    系統(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 飄雪 閱讀(1962) 評論(1)  編輯  收藏 所屬分類: JAVA技術

    評論

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

    good
      回復  更多評論   

    導航

    統(tǒng)計

    常用鏈接

    留言簿(1)

    隨筆分類(11)

    隨筆檔案(13)

    收藏夾

    firends

    搜索

    最新評論

    • 1.?re: udp及tcp穿越NAT
    • 您上述提到的是互聯(lián)網之間的公網與私網之間的NAT穿越,3g終端可以通過這種方式實現(xiàn)嗎?還有3g移動設備的IP是動態(tài)分配的,我怎么才能在公網服務器找到這個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: 系統(tǒng)時間修改方法
    • good
    • --jone
    • 5.?re: udp及tcp穿越NAT
    • 評論內容較長,點擊標題查看
    • --...

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 成人福利在线观看免费视频| 亚洲日本va一区二区三区| 乱爱性全过程免费视频| 国产成人啪精品视频免费网| 久久久久亚洲国产| 午夜精品在线免费观看| 亚洲人成网站在线在线观看| 国产精品美女午夜爽爽爽免费| 亚洲ts人妖网站| 最近免费中文字幕大全视频| 亚洲日本一线产区和二线| 国产免费卡一卡三卡乱码| 小说专区亚洲春色校园| 免费一级毛片在播放视频| 青青久久精品国产免费看| 日产国产精品亚洲系列| 国产精品极品美女自在线观看免费| 最新精品亚洲成a人在线观看| 三级黄色片免费看| 久久亚洲精精品中文字幕| 在线视频精品免费| 欧美日韩亚洲精品| 相泽亚洲一区中文字幕| 国产情侣久久久久aⅴ免费| 亚洲国产精品张柏芝在线观看| 午夜dj在线观看免费视频| 国产国产人免费人成成免视频| 亚洲αv久久久噜噜噜噜噜| 91高清免费国产自产| 亚洲av无码一区二区三区四区| 亚洲一区二区三区国产精品| 久久青青草原国产精品免费| 亚洲免费福利视频| 免费国产在线观看不卡| 日本免费电影一区二区| 亚洲中文字幕一二三四区| 亚洲色偷偷综合亚洲AV伊人| 91老湿机福利免费体验| 国产精品日本亚洲777| 久久亚洲AV无码精品色午夜麻| 成年女人免费v片|