Java – How to Convert Local Time To GMT In Java


  • 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:-

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *