zoukankan      html  css  js  c++  java
  • 修改Windows和linux系统时间

    1、修改本机Windows的系统时间,Java代码实现:

    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class ChangeWindowsDate {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) {
            try {
                Runtime.getRuntime().exec("cmd /c date 2013-11-08") ;
                Runtime.getRuntime().exec("cmd /c time 18:10:00") ;
            } catch (IOException e) {
                e.printStackTrace() ;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
            Date date = new Date() ;
            System.out.println(sdf.format(date));
            
        }
    
    }

    2、修改远程Linux Server的系统时间,Java代码实现:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import ch.ethz.ssh2.Connection;
    import ch.ethz.ssh2.Session;
    import ch.ethz.ssh2.StreamGobbler;
    
    
    
    public class ChangeLinuxDate {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            String host_ip1 = "192.168.1.118" ;
            int port = 22 ;
            String username = "root" ;
            String password = "123456" ;        
            
            String cmd = "date -s '2013-08-04 23:00:00'" ;
            
            Connection conn1 = new Connection(host_ip1, port) ;
            Session session1 = null ;
            
            try {
                conn1.connect() ;
                boolean isconn = conn1.authenticateWithPassword(username, password) ;
                if(!isconn){
                    System.out.println("用户名称或者是密码不正确");
                }else{
                    System.out.println(host_ip1 + ":" + "已经连接OK");
                    session1 = conn1.openSession() ;
                    session1.execCommand(cmd) ;
    
                    InputStream is = new StreamGobbler(session1.getStdout());
                    BufferedReader brs = new BufferedReader(new InputStreamReader(is));
                    while(true){  
                        String line = brs.readLine();  
                        if(line==null){  
                            break;  
                        }  
                        System.out.println(line);  
                    }
                    is.close() ;
                    brs.close() ;
                    session1.close() ;
                    conn1.close() ;
                }
                
            } catch (IOException e) {
                e.printStackTrace() ;
            }
            
        }
    
    }
  • 相关阅读:
    从yum源下载软件包
    本地yum源建立
    Redis慢查询,redis-cli,redis-benchmark,info
    Centos6.6安装mysql记录
    Nginx常用命令(加入系统服务)
    Nginx+keepalived双机热备(主从模式)
    Nginx反向代理+负载均衡简单实现
    Centos7安装Python3.5
    CentOS 6.4下OpenSSH升级到6.7操作
    Redis详解
  • 原文地址:https://www.cnblogs.com/candle806/p/3257044.html
Copyright © 2011-2022 走看看