How do I convert string to Date in GMT timezone?

The following code snippet convert a string representation of a date into a java.util.Date object and the timezone is set to GMT. To parse the string so that the result is in GMT you must set the TimeZone of the DateFormat object into GMT.

package org.kodejava.example.text;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class WithTimezoneStringToDate {
    public static void main(String[] args) {
        // Create a DateFormat and set the timezone to GMT.
        DateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
        df.setTimeZone(TimeZone.getTimeZone("GMT"));

        try {
            // Convert string into Date
            Date today = df.parse("Mon, 16 Apr 2018 00:00:00 GMT+08:00");
            System.out.println("Today = " + df.format(today));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

The code snippet above print the following output:

Today = Sun, 15 Apr 2018 16:00:00 GMT

How do I get timezone ids by their milliseconds offset?

package org.kodejava.example.util;

import java.util.TimeZone;

public class TimeZoneByOffset {
    public static void main(String[] args) {
        // Gets the available IDs according to the given time zone
        // offset in milliseconds.
        int offset = 8 * 60 * 60 * 1000;
        String[] timezones = TimeZone.getAvailableIDs(offset);

        System.out.println("List of available IDs for GMT:+08:00");
        System.out.println("====================================");
        for (String id : timezones) {
            System.out.println(id);
        }
    }
}

Here are the timezone ids in the GMT+8 offset:

List of available IDs for GMT:+08:00
====================================
Asia/Brunei
Asia/Choibalsan
Asia/Chongqing
Asia/Chungking
Asia/Harbin
Asia/Hong_Kong
Asia/Irkutsk
Asia/Kuala_Lumpur
Asia/Kuching
Asia/Macao
Asia/Macau
Asia/Makassar
Asia/Manila
Asia/Shanghai
Asia/Singapore
Asia/Taipei
Asia/Ujung_Pandang
Asia/Ulaanbaatar
Asia/Ulan_Bator
Australia/Perth
Australia/West
CTT
Etc/GMT-8
Hongkong
PRC
Singapore

How do I get all available timezones?

package org.kodejava.example.util;

import java.util.TimeZone;

public class TimezonesExample {
    public static void main(String[] args) {
        String[] availableTimezones = TimeZone.getAvailableIDs();

        for (String timezone : availableTimezones) {
            System.out.println("Timezone ID = " + timezone);                        
        }
    }
}

Some examples of the returned timezones ids are:

Timezone ID = Etc/GMT+12
Timezone ID = Etc/GMT+11
Timezone ID = MIT
Timezone ID = Pacific/Apia
Timezone ID = Pacific/Midway
Timezone ID = Pacific/Niue
Timezone ID = Pacific/Pago_Pago
Timezone ID = Pacific/Samoa
Timezone ID = US/Samoa
...

How do I convert time between timezone?

package org.kodejava.example.util;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class TimeZoneExample {
    public static void main(String[] args) {
        // Create a calendar object and set it time based on the local
        // time zone
        Calendar localTime = Calendar.getInstance();
        localTime.set(Calendar.HOUR, 17);
        localTime.set(Calendar.MINUTE, 15);
        localTime.set(Calendar.SECOND, 20);

        int hour = localTime.get(Calendar.HOUR);
        int minute = localTime.get(Calendar.MINUTE);
        int second = localTime.get(Calendar.SECOND);

        // Print the local time
        System.out.printf("Local time  : %02d:%02d:%02dn", hour, minute, second);

        // Create a calendar object for representing a Germany time zone. Then we
        // wet the time of the calendar with the value of the local time
        Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
        germanyTime.setTimeInMillis(localTime.getTimeInMillis());
        hour = germanyTime.get(Calendar.HOUR);
        minute = germanyTime.get(Calendar.MINUTE);
        second = germanyTime.get(Calendar.SECOND);

        // Print the local time in Germany time zone
        System.out.printf("Germany time: %02d:%02d:%02dn", hour, minute, second);
    }
}