Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Convert Local Time to GMT in Java?

How to Convert Local Time to GMT in Java?

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:

java.util.SimpleDateFormat

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.

Syntax:

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.

Procedure:

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

Example:

Java




// Java Program to convert local time to GMT
 
// Importing libraries
// 1. input output libraries
import java.io.*;
// 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
        s.setTimeZone(TimeZone.getTimeZone("GMT"));
 
        // 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));
    }
}


 
 

Output

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.

 

Procedure:

 

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

 

Example:

 

Java




// Java Program to convert Local time to
// GMT time
 
// Importing all input output classes
import java.io.*;
// 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 = 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 = Instant.now();
 
        // Printing the GMT/UTC time by parsing with string
        // using the toString() method
        System.out.println(" GMT : "+instant.toString());
    }
}


 
 

Output

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

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments