gpt4 book ai didi

java - 将系统时间更改为提前三个小时,然后恢复为默认值

转载 作者:行者123 更新时间:2023-12-01 13:35:10 25 4
gpt4 key购买 nike

我一直在尝试将系统时间更改为提前三个小时,然后恢复为默认值,但我似乎没有得到我想要的时间。不是 cal.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));是否要将时间设置为 America/Los_Angeles 时区?

public static void changeSystemTime() throws Exception {

DateFormat dateFormat = new SimpleDateFormat("HH:mm");

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));

System.out.println("Current time is " + dateFormat.format(cal.getTime());

cal.add(Calendar.HOUR_OF_DAY, 3);
System.out.println("3 Hours from now is " + dateFormat.format(cal.getTime());

Runtime runtime = Runtime.getRuntime();

runtime.exec("cmd /c Time " + dateFormat.format(cal.getTime()));
}

public static void changeSystemTimeToDefault() throws Exception {

DateFormat dateFormat = new SimpleDateFormat("HH:mm");

Date date = new Date();

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));

System.out.println("Timezone for LA is " + dateFormat.format(cal.getTime());

Runtime runtime = Runtime.getRuntime();

runtime.exec("cmd /c Time " + dateFormat.format(cal.getTime()));
}

最佳答案

你实际上没有在方法changeSystemTime()中添加3个小时;

原因:

String time = dateFormat.format(cal.getTime());
System.out.println("Current time is " + time);

cal.add(Calendar.HOUR_OF_DAY, 3);
// here you again work with the initial time, the 3 hours are not added to it
System.out.println("3 Hours from now is " + time);

这样做:

cal.add(Calendar.HOUR_OF_DAY, 3);
// get new time
System.out.println("3 Hours from now is " + cal.getTime());

关于java - 将系统时间更改为提前三个小时,然后恢复为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21338763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com