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

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

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

    從心開始

     

    系統時間修改方法

    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 飄雪 閱讀(1951) 評論(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
    • 評論內容較長,點擊標題查看
    • --...

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: a级特黄毛片免费观看| 亚洲日本一区二区一本一道| 亚洲日韩小电影在线观看| 精品无码免费专区毛片| 亚洲视频网站在线观看| 亚洲精品动漫免费二区| 成人免费看黄20分钟| 亚洲国产成a人v在线| 日韩吃奶摸下AA片免费观看| 男女啪啪免费体验区| 亚洲邪恶天堂影院在线观看| 高清国语自产拍免费视频国产 | 亚洲精品无码专区在线| 中文字幕无码精品亚洲资源网| 99精品视频免费观看| 美女被免费网站视频在线| 91亚洲自偷手机在线观看| 又黄又爽一线毛片免费观看 | 四虎成人免费大片在线| 日本一区午夜艳熟免费| 亚洲老熟女五十路老熟女bbw| 亚洲色成人网站WWW永久| 午夜影视在线免费观看| 3d动漫精品啪啪一区二区免费| 337P日本欧洲亚洲大胆艺术图| 亚洲视频在线观看不卡| 亚洲综合色成在线播放| 成人免费a级毛片| 亚在线观看免费视频入口| 免费国产草莓视频在线观看黄| 亚洲性69影院在线观看| 国产A在亚洲线播放| 日批日出水久久亚洲精品tv| 永久免费AV无码国产网站| 中文无码成人免费视频在线观看| 久久久久久亚洲精品无码| 亚洲成人福利在线观看| 亚洲Av无码精品色午夜| 黑人大战亚洲人精品一区| 免费国产真实迷j在线观看| 最近中文字幕无吗高清免费视频|