Pages

Converting local time into GMT or any other timezone in Java is easy as Java has support for time zones. JDK has a class called java.util.Timezone which represents timezone and Java also has classes like SimpleDateFormat which can use Time zone while parsing or formatting dates. By using java.uti.TimeZone and java.text.SimpleDateFormat we can write simple Java program to convert local time to GMT or any other time zone in Java. We have already seen example of How to get current date and time in GMT and this Java program is extension of that in order to convert time form one timezone to other timezone.

Java program to convert local time to GMT or other Timezone

Here is complete code example of converting local time into GMT timezone. As I said you can convert local time to any other timezone by just setting current timezone for SimpleDateFormat as shown in this example. By the way beware while using SimpleDateFormat in multi-threading environment as SimpleDateFormat in Java is not thread safe.

/**
* Simple Java program to convert local time into GMT or any other TimeZone in Java
* SimpleDateFormat in Java can be used to convert Date from one timezone to other
* @author Javin
*/publicclass TimeZoneConverter {

publicstaticvoid main(String args[]){

//Date will return local time in Java Date localTime = newDate();

//creating DateFormat for converting time from local timezone to GMTDateFormat converter = newSimpleDateFormat("dd/MM/yyyy:HH:mm:ss");

//getting GMT timezone, you can get any timezone e.g. UTC
converter.setTimeZone(TimeZone.getTimeZone("GMT"));

That's all on how to convert local time into GMT or any other timezone in Java like UTC or EDT etc. while converting time form one timezone to another just keep an eye on day light saving as some Timezone falls under day light time saving zone.