包括将世界标准时间转换为本地时间和将世界标准时间转换为目标时区的本地时间,以及将本地时间转换为世界标准时间。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * @author j.tommy
 * @version 1.0
 * @date 2017/12/10
 */
public final class DateUtil {
    /**
     * 将世界标准时间转换为本地时间
     * @param gmtDate
     * @return
     */
    public static Date convertGMT2Local(Date gmtDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(gmtDate);
        int zoneOffset = c.get(Calendar.ZONE_OFFSET);
        int dstOffset = c.get(Calendar.DST_OFFSET);
        c.add(Calendar.MILLISECOND,zoneOffset+dstOffset);
        return c.getTime();
    }
    /**
     * 将世界标准时间转换为目标时区的本地时间
     * @param gmtDate
     * @param id
     * @return
     */
    public static Date convertGMTToLocal(Date gmtDate, String id) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        calendar.setTime(gmtDate);
        calendar.setTimeZone(TimeZone.getTimeZone(id));
        int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
        int dstOffset = calendar.get(Calendar.DST_OFFSET);
        calendar.add(Calendar.MILLISECOND, dstOffset+zoneOffset);
        return calendar.getTime();
    }
    /**
     * 将本地时间转换为世界标准时间
     * @param date
     * @return
     */
    public static Date convertToGMT(Date date) {
        //Local Time Zone Calendar Instance
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
        int dstOffset = calendar.get(Calendar.DST_OFFSET);
        calendar.add(Calendar.MILLISECOND, -(dstOffset+zoneOffset));
        return calendar.getTime();
    }
    public static void main(String[] args) {
        String gmtDateString = "2017-12-10T04:10:01.794Z";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try {
            Date gmtDate = sdf.parse(gmtDateString);
            Date localDate = convertGMT2Local(gmtDate);
            System.out.println(sdf2.format(localDate));
//            for (String id : TimeZone.getAvailableIDs()) {
//                System.out.println(id);
//            }
            localDate = convertGMTToLocal(gmtDate,"Asia/Hong_Kong");
            System.out.println(sdf2.format(localDate));
            gmtDate = convertToGMT(localDate);
            System.out.println(sdf.format(gmtDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

参考:http://blog.csdn.net/calkee/article/details/50879383
http://blog.csdn.net/tjgykhulj/article/details/68953636