|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "How To Get GMT Time In Java?" |
| 4 | +author: gaurav |
| 5 | +categories: [Java, Java Time] |
| 6 | +description: "In this article, we will see how we can get GMT time in Java." |
| 7 | +--- |
| 8 | + |
| 9 | +In Java Programming, sometimes we need date in GMT format. In this quick tutorial, we will see how we can get GMT time in Java. |
| 10 | + |
| 11 | +We will be using the `ZonedDateTime` class to get the GMT. We can get any zone time using the `ZonedDateTime` class. You can <a target="_blank" href="https://coderolls.com/get-current-date-time-in-java/">see this article</a> to learn more about it. |
| 12 | + |
| 13 | +I have given a java program below, it has the `getGMTTime()` method. This method receives the Date object (java.util.Date) and returns a GMT string. |
| 14 | + |
| 15 | +```java |
| 16 | +package com.coderolls.examples; |
| 17 | + |
| 18 | +import java.time.Instant; |
| 19 | +import java.time.ZoneOffset; |
| 20 | +import java.time.ZonedDateTime; |
| 21 | +import java.time.format.DateTimeFormatter; |
| 22 | +import java.util.Date; |
| 23 | +/** |
| 24 | + * A Java Program to get time in GMT |
| 25 | + * |
| 26 | + * @author Gaurav Kukade at coderolls.com |
| 27 | + * |
| 28 | + */ |
| 29 | +public class GMTTimeExample { |
| 30 | + |
| 31 | + public static void main(String[] args) { |
| 32 | + |
| 33 | + Date date = new Date(); |
| 34 | + String gmtTime = getGMTTime(date); |
| 35 | + System.out.println("Print time in GMT: "+gmtTime); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * A method to return GMT time string |
| 41 | + * @param date |
| 42 | + * @return |
| 43 | + */ |
| 44 | + public static String getGMTTime(Date date) { |
| 45 | + |
| 46 | + DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss O"); |
| 47 | + Instant instant = date.toInstant(); |
| 48 | + ZonedDateTime zonedDateTime = instant.atZone(ZoneOffset.UTC); |
| 49 | + return customFormatter.format(zonedDateTime); |
| 50 | + |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +In the above program, we have used the <a target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html">DateTimeFormatter</a>. You can check its various patterns in the <a target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html">documentation</a>. |
| 56 | + |
| 57 | +If you are looking to get the current timestamp in java, you can visit this article <a target="_blank" href="https://coderolls.com/how-to-get-current-timestamps-in-java/">How To Get Current Timestamp In Java?</a>. |
| 58 | + |
| 59 | +You can check my YouTube channel [here](https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w). |
| 60 | + |
| 61 | +### Related Articles |
| 62 | + |
| 63 | +- [How To Get Current Timestamp In Java?](https://coderolls.com/how-to-get-current-timestamps-in-java/) |
| 64 | + |
| 65 | +- [How To Convert An Integer To String In Java](https://coderolls.com/convert-int-to-string/) |
| 66 | + |
| 67 | +- [How to convert String to Integer in Java](https://coderolls.com/convert-string-to-int/) |
| 68 | + |
| 69 | +- [How To Convert StringBuilder To String In Java?](https://coderolls.com/convert-stringbuilder-to-string-in-java/) |
| 70 | + |
| 71 | +- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) |
| 72 | + |
| 73 | +- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) |
| 74 | + |
0 commit comments