Open In App
Related Articles

How to Convert Local Time to GMT in Java?

Improve Article
Save Article
Like Article

Time conversion from IST or any standard time to GMT is necessary for locals to understand and reciprocate their international clients if they are connected overseas in terms of work or any purpose. Today we will have a look at a code where we convert the standard time of any country to GMT. 

Here we would use the SimpleDateFormat to convert the local Time in GMT. It is available in class as mentioned:


Methods: One can use different methods like SimpleDateFormat or maybe even Instance() method. They are very easy and helpful methods. We can also use calendar and time methods to do so.

  1. Using format() method of SimpleDateFormat class
  2. Using instance() method of SimpleDateFormat class

Method 1: Using format() method of SimpleDateFormat class

The format() method of DateFormat class in Java is used to format a given date into a Date/Time string. Basically, the method is used to convert this date and time into a particular format i.e., mm/dd/yyyy.


public final String format(Date date)

Parameters: The method takes one parameter date of Date object type and refers to the date whose string output is to be produced.

Return Value: The method returns Date or time in string format of mm/dd/yyyy.


  • Here we would simply first print our local time
  • Then convert it to GMT using SimpleDateFormat
  • Then print both the time zones.



// Java Program to convert local time to GMT
// Importing libraries
// 1. input output libraries
// 3. Text class
import java.text.DateFormat;
import java.text.SimpleDateFormat;
// 2. Utility libraries for
// Date and TimeZone class
import java.util.Date;
import java.util.TimeZone;
// Class
class GFG {
    // Main driver method
    public static void main(String[] args)
        // Creating a Date class object
        // to take local time from the user
        Date localTime = new Date();
        // Creating a DateFormat class object to
        // convert the localtime to GMT
        DateFormat s = new SimpleDateFormat("dd/MM/yyyy"
                                            + " "
                                            + " HH:mm:ss");
        //  function will helps to get the GMT Timezone
        // using the getTimeZOne() method
        // One can get any other time zone also
        // by passing some other argument to it
        // Printing the local time
        System.out.println("local Time:" + localTime);
        // Printing the GMT time to
        // illustrate changes in GMT time
        System.out.println("Time IN Gmt : "
                           + s.format(localTime));



local Time:Thu Feb 04 11:34:15 UTC 2021
Time IN Gmt : 04/02/2021  11:34:15


Method 2: Using instance() method of SimpleDateFormat class


As in the above method we used SimpleDateFormat, we will now use the instant method to get the time. The SimpleDateFormat can be used in different ways, now the instant method can be used to get the UTC or GMT.




  • We will use the Instant method to get the time proper time
  • It can be importing the complete time class
 java.time.* ;





// Java Program to convert Local time to
// GMT time
// Importing all input output classes
// Importing all time classes
import java.time.*;
// Class
class GFG {
    // Main driver method
    public static void main(String[] args)
        // Instant operator helps to note
        // the time and the location of it
        // Creating an object of Instant type
        // using the now() method
        Instant now =;
        // Now with the help of Instant operator
        // zoned operator is called
        // Creating an object of ZonedDateTime
        ZonedDateTime zdt = ZonedDateTime.ofInstant(
            now, ZoneId.systemDefault());
        // Printing the local time
        System.out.println(" Local : " + zdt);
        // Creating an object of  Instant type
        // taking any other instant method
        Instant instant =;
        // Printing the GMT/UTC time by parsing with string
        // using the toString() method
        System.out.println(" GMT : "+instant.toString());



 Local : 2021-02-04T10:40:34.436700Z[Etc/UTC]
 GMT : 2021-02-04T10:40:34.547680Z


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 04 Jul, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials