- Converting Local Time to GMT timezone or any other timezone in Java is easy because java JDK provide class java.util.Timezone & SimpleDateFormat Class.
- java.util.Timezone class represents timezone and SimpleDateFormat class support to formatting dates.
Java program to convert local time zone into GMT zone or any other TimeZone in Java
import java.util.Date; import java.util.TimeZone; import java.text.DateFormat; import java.text.SimpleDateFormat; public class TimeZoneConverter { public static void main(String args[]) { Date localTime = new Date(); //create DateFormat to perform conversion from local timezone to GMT DateFormat convert = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss"); //get GMT timezone and you can get any timezone like below e.g. UTC convert.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println("local time : " + localTime); System.out.println("time in GMT : " + convert.format(localTime)); } }
Output:-

java.util.Date and SimpleDateFormatter aren’t thread-safe. As of java 8, java.time classes should be used.
ZonedDateTime or OffsetDateTime should be able to do the conversion to GMT.
https://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html talks about not using java.util.Date and SimpleDateFormatter