1
import java.util.TimeZone;
2
import java.util.Date;
3
import java.text.DateFormat;
4
5
public class ForTest {
6
7
public static void main(String[] args) {
8
9
DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化輸出
10
TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//獲取時(shí)區(qū)
11
dateFormatterChina.setTimeZone(timeZoneChina);//設(shè)置系統(tǒng)時(shí)區(qū)
12
Date curDate = new Date();//獲取系統(tǒng)時(shí)間
13
14
System.out.println(dateFormatterChina.format(curDate));
15
}
16
}
正常情況下會(huì)與本機(jī)時(shí)間相差8小時(shí),原因是注冊(cè)表里面的時(shí)區(qū)設(shè)置問題。
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

解決:
找到原有的注冊(cè)表并修改對(duì)應(yīng)的地方
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\China Standard Time]
“Display“=“(GMT+08:00) 北京,重慶,香港特別行政區(qū),烏魯木齊“
“Dlt“=“中國夏季時(shí)間“
“Std“=“中國標(biāo)準(zhǔn)時(shí)間“
“MapID“=“-1,75“
“Index“=dword:000000d2
“TZI“=hex:20,fe,ff,ff,00,00,00,00,c4,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00