
在一个精简的Linux安装上jre,发现所有的java程序获取的时间都不是系统时间。后来发现原来是时区不对。
java程序获取的时间都是GMT时间,而系统是北京时间,应该是GMT+8,刚好相差8小时。网上搜了好多资料,只有两种方法:1、在程序中使用
java的函数设定时区。2、在启动java程序时加参数-Dusertimezone=GMT+8
import javaioBufferedInputStream;
import javaioBufferedOutputStream;
import javaioFile;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
public class Test {
public static void main(String[] args) {
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File("d:/infotxt")));
String line = "第一行文本\n第二行文本";
outwrite(linegetBytes());
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
} finally {
if (out != null) {
try {
outclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream("d:/infotxt"));
StringBuffer buffer = new StringBuffer();
byte[] buff = new byte[inavailable()];
while (inread(buff) != -1) {
bufferappend(new String(buff));
}
Systemoutprintln(buffer);
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
} finally {
if (in != null) {
try {
inclose();
in = null;
} catch (IOException e) {
eprintStackTrace();
}
}
}
}
}
以上就是关于怎么实时同步java虚拟机与 *** 作系统时区 及JVM启动后再更改 *** 作系统时区或时间也能保持其同步全部的内容,包括:怎么实时同步java虚拟机与 *** 作系统时区 及JVM启动后再更改 *** 作系统时区或时间也能保持其同步、java程序设计题、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)