Converting local time to GMT is useful when an application needs to work with users or systems in different time zones. Java provides several classes for handling time-zone conversion and representing a specific moment in time.
- The modern java.time API provides a clear way to convert between time zones.
- SimpleDateFormat can also be used when working with the older Date API.
Example: Program to converts a local time from the Asia/Kolkata time zone to GMT using ZonedDateTime and ZoneOffset.UTC.
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.ZoneOffset;
public class GFG {
public static void main(String[] args) {
LocalDateTime localTime =
LocalDateTime.of(2026, 9, 10, 10, 30);
ZonedDateTime kolkataTime =
localTime.atZone(ZoneId.of("Asia/Kolkata"));
ZonedDateTime gmtTime =
kolkataTime.withZoneSameInstant(ZoneOffset.UTC);
System.out.println("Local Time: " + kolkataTime);
System.out.println("GMT: " + gmtTime);
}
}
Output
Local Time: 2026-09-10T10:30+05:30[Asia/Kolkata] GMT: 2026-09-10T05:00Z
Explanation: The program creates a local time in the Asia/Kolkata time zone and uses withZoneSameInstant() to convert the same instant to GMT. Since Kolkata is 5 hours 30 minutes ahead of GMT, 10:30 AM becomes 5:00 AM GMT.
Ways to Convert Local Time to GMT
Java provides several ways to convert local time to GMT (Greenwich Mean Time), depending on the Java API and the required time zone handling.
Method 1: Using SimpleDateFormat
SimpleDateFormat can format a Date object according to a specified time zone. By setting the formatter's time zone to GMT, the same instant can be displayed in GMT.
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class LocalToGMT {
public static void main(String[] args)
{
// Get the current local date and time
Date localTime = new Date();
// Create a formatter
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");
// Set the time zone to GMT
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
// Display local time
System.out.println("Local Time: " + localTime);
// Display GMT time
System.out.println("GMT Time: " + formatter.format(localTime));
}
}
Output
Local Time: Thu Aug 27 05:08:16 UTC 2026 GMT Time: 27/08/2026 05:08:16 GMT
Explanation: The Date object represents the current instant, while setTimeZone() specifies the time zone used for formatting. The format() method then displays that instant according to GMT.
Note: SimpleDateFormat belongs to Java's legacy date and time API. The java.time API is recommended for new applications.
Method 2: Using ZonedDateTime
The modern ZonedDateTime class stores a date and time together with its time-zone information. The withZoneSameInstant() method can be used to convert the local time to GMT while preserving the same instant.
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class LocalToGMT {
public static void main(String[] args)
{
// Specify the local time zone
ZoneId localZone = ZoneId.of("Asia/Kolkata");
// Get the current local time
ZonedDateTime localTime = ZonedDateTime.now(localZone);
// Convert local time to GMT
ZonedDateTime gmtTime= localTime.withZoneSameInstant(ZoneId.of("GMT"));
// Specify the output format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss z");
// Display both times
System.out.println("Local Time: " + localTime.format(formatter));
System.out.println("GMT Time: " + gmtTime.format(formatter));
}
}
Output
Local Time: 27/08/2026 10:41:53 IST GMT Time: 27/08/2026 05:11:53 GMT
Explanation: ZonedDateTime keeps the time-zone information along with the date and time. withZoneSameInstant() changes the time zone from India to GMT without changing the actual moment represented.
Method 3: Using Instant
Instant represents a specific moment on the UTC timeline. It is useful when the application needs to work with a time independently of a particular local time zone.
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class InstantGMT {
public static void main(String[] args)
{
// Get the current instant
Instant instant = Instant.now();
// Convert the instant to GMT
ZonedDateTime gmtTime = instant.atZone(ZoneId.of("GMT"));
// Specify the output format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss z");
System.out.println("GMT Time: " + gmtTime.format(formatter));
System.out.println("UTC Instant: " + instant);
}
}
Output
GMT Time: 27/08/2026 05:17:23 GMT UTC Instant: 2026-08-27T05:17:23.945471908Z
Explanation: Instant.now() obtains the current moment in UTC. atZone() associates that instant with the GMT time zone so that it can be displayed as a date and time.