
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Convert the local Time to GMT
In this article, we will understand how to convert the local Time to GMT. Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes.
Below is a demonstration of the same −
Suppose our input is −
The local time is: Fri Mar 18 00:01:54 IST 2022
The desired output would be −
The time in Gmt is: 17/03/2022 18:31:54
Algorithm
Step 1 - START Step 2 - Declare an object of LocalDateTime namely date. Step 3 - Define the values. Step 4 - Define different date time formats using DateTimeFormatter objects. Step 5 - Display the GMT date time formats Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Demo { public static void main(String[] args){ System.out.println("The required packages have been imported"); Date localTime = new Date(); System.out.println("A Date object is defined"); DateFormat GMT_format = new SimpleDateFormat("dd/MM/yyyy" + " " + " HH:mm:ss"); GMT_format.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println("\nThe local time is: " + localTime); System.out.println("The time in Gmt is: " + GMT_format.format(localTime)); } }
Output
The required packages have been imported A Date object is defined The local time is: Tue Mar 29 08:59:11 UTC 2022 The time in Gmt is: 29/03/2022 08:59:11
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Demo { static void GMT_time(Date localTime){ DateFormat GMT_format = new SimpleDateFormat("dd/MM/yyyy" + " " + " HH:mm:ss"); GMT_format.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println("\nThe local time is: " + localTime); System.out.println("The time in Gmt is: " + GMT_format.format(localTime)); } public static void main(String[] args){ System.out.println("The required packages have been imported"); Date localTime = new Date(); System.out.println("A Date object is defined"); GMT_time(localTime); } }
Output
The required packages have been imported A Date object is defined The local time is: Tue Mar 29 08:59:38 UTC 2022 The time in Gmt is: 29/03/2022 08:59:38
- Related Articles
- Set the base time zone offset to GMT in Java
- Select current time with MySQL now() and convert it to GMT 0?
- Convert the specified Windows file time to an equivalent local time in C#
- Java Program to convert java.util.Date to any local date in certain timezone
- Python Pandas - Convert naive Timestamp to local time zone
- GMT Time in Perl
- How to convert UTC date time into local date time using JavaScript?
- Convert string of time to time object in Java
- How to calculate local time in Node.js?
- How to use time() with local time in Android sqlite?
- How to print local time in Android sqlite?
- Java Program to Convert Character to String
- Java Program to Convert String to Date
- Java Program to Convert InputStream to String
- Java Program to Convert OutputStream to String

Advertisements