If you want to display a Java Date in GMT (UTC) timezone then you need follow the below steps,
- Create a Date object: Date currentDate = new Date();
- Create a SimpleDate object: DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
- Now set timezone to DateFormat object: dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
- Now when you apply the dataFormat to the currentDate object, you will get the time printed in GMT: System.out.println(dateFormat.format(currentDate));
package org.code2care;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
Date currentDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("Local Time: " + dateFormat.format(currentDate));
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Time in GMT: " + dateFormat.format(currentDate));
}
}
Local Time: 2021/03/27 04:25:50
Time in GMT: 2021/03/27 09:25:50

Java Display Time in GMT Example
By default when you create a Date object instance in Java you will get the time in the TimeZone that you have set on your device. Say you write a piece of code on your computer and you live in Chicago, your default system time zone will be America/Chicago (CST/CDT)
public static void main(String[] args) {
Date currentDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(dateFormat.format(currentDate));
}
Output:
2021/03/26 15:00:00.000-05:00
If your server is hosted in New York, then when the same code runs the output will be of America/New_York timezone,
2021/03/26 15:00:00.000-04:00
Have Questions? Post them here!
More Posts related to Java,
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
More Posts:
- incorrect line ending: found carriage return (\r) without corresponding newline (\n) - Android
- Upload Pdf file using PHP Script - PHP
- Fix zsh: permission denied: script.sh - zsh
- 3 Commands to stop Nginx Server - Linux
- Steps to Compare Two files in Sublime Text Side-by-Side - Sublime-Text
- Android : IOException: Unable to open sync connection! - Android
- Send Email using SharePoint PowerShell command, SMTP server - SharePoint
- How to install pip on macOS using terminal command [Python] - Python