
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Convert the local Time to GMT
In this article, we will learn how to convert the local time to GMT (Greenwich Mean Time) in Java. We need this conversion when we want to standardize time across different time zones or when working with systems that require GMT.
We will cover the following methods to convert local time to GMT:
Using ZonedDateTime Class
ZonedDateTime class was introduced in Java 8, which is used for converting local time to GMT. We can use the ZonedDateTime class to represent a date-time with a time zone in the ISO-8601 calendar system.
Example
In the following example, we will create a ZonedDateTime object that shows the current local time and then convert it to GMT using the withZoneSameInstant method.
import java.time.ZonedDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; public class LocalToGMT { public static void main(String[] args){ ZonedDateTime local = ZonedDateTime.now(); ZonedDateTime gmt = local.withZoneSameInstant(ZoneId.of("GMT")); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); System.out.println("Local Time: " + local.format(formatter)); System.out.println("GMT Time: " + gmt.format(formatter)); } }
Following is the output of the above code:
Local Time: 2025-10-06 15:30:00 Asia/Kolkata GMT Time: 2025-10-06 10:00:00 GMT
Using Calendar Class
Now, we will use the Calendar class to convert local time to GMT. The Calendar class provides methods to manipulate date and time fields.
We will use methods like getTimeInMillis() and getInstance() to get the current local time and then convert it to GMT.
Example
In the following example, we will create a Calendar object to get the current local time and then convert it to GMT.
import java.util.Calendar; import java.util.TimeZone; public class LocalToGMT { public static void main(String[] args) { Calendar localCalendar = Calendar.getInstance(); System.out.println("Local Time: " + localCalendar.getTime()); Calendar gmtCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); gmtCalendar.setTimeInMillis(localCalendar.getTimeInMillis()); System.out.println("GMT Time: " + gmtCalendar.getTime()); } }
Following is the output of the above code:
Local Time: Mon Oct 06 15:30:00 IST 2025 GMT Time: Mon Oct 06 10:00:00 GMT 2025
Using Date Class
The Date class is outdated, but it is still in use in many apps. We will use the Date class and its methods to convert local time to GMT.
We will use the getTimeZone method from the TimeZone class to get the GMT time zone. Then, the methods getTime() and getOffset() will be used to convert the local time to GMT.
Example
In the following example, we will create a Date object to get the current local time and then convert it to GMT.
import java.util.Date; import java.util.TimeZone; public class LocalToGMT { public static void main(String[] args) { Date localDate = new Date(); System.out.println("Local Time: " + localDate); TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT"); Date gmtDate = new Date(localDate.getTime() + gmtTimeZone.getOffset(localDate.getTime())); System.out.println("GMT Time: " + gmtDate); } }
Following is the output of the above code:
Local Time: Mon Oct 06 15:30:00 IST 2025 GMT Time: Mon Oct 06 10:00:00 GMT 2025