java linux 修改系统时间格式,java修改操作系统时间(linux和windows)

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);

}

@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);

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值