从心开始

 

系统时间修改方法

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 飘雪 阅读(1913) 评论(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
  • 评论内容较长,点击标题查看
  • --...

阅读排行榜

评论排行榜