java修改操作系统时间(linux和windows)
2015-08-06 17:38
197 查看
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.junit.Test;
public class TestModifySysDate {
@Test
public void getSysdate1(){
DateFormat chinaFormatter=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,Locale.CHINA);
DateFormat defaultFormatter=DateFormat.getDateTimeInstance();
Date now=new Date();
DateFormat gmt08Formatter=DateFormat.getDateTimeInstance();
TimeZone timezone=TimeZone.getTimeZone( “GMT 08:00 “);
gmt08Formatter.setTimeZone(timezone); //格式日期/时间
String defaultDateTime=defaultFormatter.format(now);
String chinaDateTime=chinaFormatter.format(now);
String gmt08DateTime=gmt08Formatter.format(now);
System.out.print( defaultDateTime+” ” +chinaDateTime+” “+ gmt08DateTime);
}
public static void main(String args[]) throws Exception{
FileWriter excutefw = new FileWriter(“/usr/updateSysTime.sh”);
BufferedWriter excutebw=new BufferedWriter(excutefw);
excutebw.write(“date -s \”2015-12-01 00:00:00\”\r\n”);
excutebw.write(“clock -w”);
excutebw.close();
excutefw.close();
String cmd_date =”sh /usr/updateSysTime.sh”;
Runtime.getRuntime().exec(cmd_date);
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.junit.Test;
public class TestModifySysDate {
@Test
public void getSysdate1(){
DateFormat chinaFormatter=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,Locale.CHINA);
DateFormat defaultFormatter=DateFormat.getDateTimeInstance();
Date now=new Date();
DateFormat gmt08Formatter=DateFormat.getDateTimeInstance();
TimeZone timezone=TimeZone.getTimeZone( “GMT 08:00 “);
gmt08Formatter.setTimeZone(timezone); //格式日期/时间
String defaultDateTime=defaultFormatter.format(now);
String chinaDateTime=chinaFormatter.format(now);
String gmt08DateTime=gmt08Formatter.format(now);
System.out.print( defaultDateTime+” ” +chinaDateTime+” “+ gmt08DateTime);
}
@Test //修改window7时间 public void modifyTime(){ try { Process p = Runtime.getRuntime().exec("cmd /c time 08:55:00"); // p.waitFor(); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); if (!br.ready()) { Thread.sleep(100); } while (true) { String s = br.readLine(); if (s == null) break; System.out.println(s); } br.close(); } catch (Exception e) { e.printStackTrace(); } } @Test //修改windows7日期 public void modifyDate(){ String date="2015-06-15"; try{ Runtime run=Runtime.getRuntime(); String command="cmd.exe /c date"+" "+(date); run.exec(command); } catch(Exception e) { e.printStackTrace(); } } //linux下修改时间和日期
public static void main(String args[]) throws Exception{
FileWriter excutefw = new FileWriter(“/usr/updateSysTime.sh”);
BufferedWriter excutebw=new BufferedWriter(excutefw);
excutebw.write(“date -s \”2015-12-01 00:00:00\”\r\n”);
excutebw.write(“clock -w”);
excutebw.close();
excutefw.close();
String cmd_date =”sh /usr/updateSysTime.sh”;
Runtime.getRuntime().exec(cmd_date);
}
}
}
相关文章推荐
- Linux crontab 命令格式与详细例子
- Android 中怎样查找SELinux导致的权限受限问题
- linux批量替换多个文件的字符串
- Centos 6.4 搭建SVN服务
- linux中断--中断下半部机制的使用&中断编程
- linux下iptables配置详解
- Linux启动(续)
- CentOS 6.4下Percona Xtrabackup安装部署错误处理
- linux虚拟机安装vmtools
- 每天一个linux命令【转】
- 另一个ISIS配置-来自leaf(linux embedded a f)
- nodejs环境搭建(linux版)
- linux查看RAID信息
- 总结Linux下查看流量工具
- Linux学习(三) 展讯Android 4.0编译 -- Makefile
- linux内核日志 dmesg 出现的提示及含意
- 为Python配置caffe(基于linux)
- Linux 磁盘挂载共享
- 开发笔记-Linux-VI-MySQL
- Linux时间子系统之八:动态时钟框架(CONFIG_NO_HZ、tickless)