java 修改系统时间_java修改系统时间

package test.dagong.testModifyDate;

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

}

}

@Test

//linux下修改时间和日期

public void testModifyLinux() throws Exception{

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

FileWriter excutefw = new FileWriter(""+"/updateSysTime.sh");

excutefw.flush();

BufferedWriter excutebw=new BufferedWriter(excutefw);

excutebw.write("date -s \"2010-12-13 14:26\";\r\n");

excutebw.write("clock -w;\r\n");

excutebw.close();

excutefw.close();

String cmd_date =""+"/updateSysTime.sh";

Runtime.getRuntime().exec(cmd_date);

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java获取本地当前时间并将其插入到Oracle数据库,可以按照以下步骤进行操作: 1. 首先,我们需要建立一个与Oracle数据库的连接。可以使用JDBC连接数据库。示例代码如下: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class OracleConnectionTest { public static void main(String[] args) { // JDBC连接的URL,根据实际情况进行修改 String url = "jdbc:oracle:thin:@localhost:1521:xe"; String username = "your_username"; String password = "your_password"; try { // 加载Oracle JDBC驱动程序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 建立连接 Connection connection = DriverManager.getConnection(url, username, password); System.out.println("Successfully connected to Oracle database!"); // 在此处插入当前时间到数据库 // ... // 关闭连接 connection.close(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } } ``` 2. 在以上代码的注释部分插入当前时间到Oracle数据库。可以使用PreparedStatement来执行插入操作。示例代码如下: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; public class OracleConnectionTest { public static void main(String[] args) { // JDBC连接的URL,根据实际情况进行修改 String url = "jdbc:oracle:thin:@localhost:1521:xe"; String username = "your_username"; String password = "your_password"; try { // 加载Oracle JDBC驱动程序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 建立连接 Connection connection = DriverManager.getConnection(url, username, password); System.out.println("Successfully connected to Oracle database!"); // 插入当前时间到数据库 String sql = "INSERT INTO your_table_name (timestamp_column) VALUES (?)"; PreparedStatement statement = connection.prepareStatement(sql); statement.setTimestamp(1, new Timestamp(System.currentTimeMillis())); statement.executeUpdate(); System.out.println("Successfully inserted current time to Oracle database!"); // 关闭连接 statement.close(); connection.close(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } } ``` 3. 在以上代码中,需要根据实际情况修改连接Oracle数据库的URL、数据库用户名、密码、要插入的表名和时间戳列名。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值