tl;dr

Instant.now()   // Capture the current moment in UTC. 

Generate a String to represent that value:

Instant.now().toString()  

2016-09-13T23:30:52.123Z

Details

As the correct answer by Jon Skeet stated, a java.util.Date object has no time zone. But its toString implementation applies the JVM’s default time zone when generating the String representation of that date-time value. Confusingly to the naïve programmer, a Date seems to have a time zone but does not.

The java.util.Date, j.u.Calendar, and java.text.SimpleDateFormat classes bundled with Java are notoriously troublesome. Avoid them. Instead, use either of these competent date-time libraries:

  • java.time.* package in Java 8
  • Joda-Time

java.time (Java 8)

Java 8 brings an excellent new java.time.* package to supplant the old java.util.Date/Calendar classes.

Getting current time in UTC/GMT is a simple one-liner…

Instant instant = Instant.now();

That Instant class is the basic building block in java.time, representing a moment on the timeline in UTC with a resolution of nanoseconds.

In Java 8, the current moment is captured with only up to milliseconds resolution. Java 9 brings a fresh implementation of Clock captures the current moment in up to the full nanosecond capability of this class, depending on the ability of your host computer’s clock hardware.

toString method of Instant generates a String representation of its value using one specific ISO 8601 format. That format outputs zero, three, six or nine digits digits (milliseconds, microseconds, or nanoseconds) as necessary to represent the fraction-of-second.

If you want more flexible formatting, or other additional features, then apply an offset-from-UTC of zero, for UTC itself (ZoneOffset.UTC constant) to get a OffsetDateTime.

OffsetDateTime now = OffsetDateTime.now( ZoneOffset.UTC );

Dump to console…

System.out.println( "now.toString(): " + now );

When run…

now.toString(): 2014-01-21T23:42:03.522Z

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
  • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
  • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
  • Later versions of Android bundle implementations of the java.time classes.
  • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


Joda-Time

UPDATE: The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

Using the Joda-Time 3rd-party open-source free-of-cost library, you can get the current date-time in just one line of code.

Joda-Time inspired the new java.time.* classes in Java 8, but has a different architecture. You may use Joda-Time in older versions of Java. Joda-Time continues to work in Java 8 and continues to be actively maintained (as of 2014). However, the Joda-Time team does advise migration to java.time.

System.out.println( "UTC/GMT date-time in ISO 8601 format: " + new org.joda.time.DateTime( org.joda.time.DateTimeZone.UTC ) );

More detailed example code (Joda-Time 2.3)…

org.joda.time.DateTime now = new org.joda.time.DateTime(); // Default time zone.
org.joda.time.DateTime zulu = now.toDateTime( org.joda.time.DateTimeZone.UTC );

Dump to console…

System.out.println( "Local time in ISO 8601 format: " + now );
System.out.println( "Same moment in UTC (Zulu): " + zulu );

When run…

Local time in ISO 8601 format: 2014-01-21T15:34:29.933-08:00
Same moment in UTC (Zulu): 2014-01-21T23:34:29.933Z

For more example code doing time zone work, see my answer to a similar question.

Time Zone

I recommend you always specify a time zone rather than relying implicitly on the JVM’s current default time zone (which can change at any moment!). Such reliance seems to be a common cause of confusion and bugs in date-time work.

When calling now() pass the desired/expected time zone to be assigned. Use the DateTimeZone class.

DateTimeZone zoneMontréal = DateTimeZone.forID( "America/Montreal" );
DateTime now = DateTime.now( zoneMontréal );

That class holds a constant for UTC time zone.

DateTime now = DateTime.now( DateTimeZone.UTC );

If you truly want to use the JVM’s current default time zone, make an explicit call so your code is self-documenting.

DateTimeZone zoneDefault = DateTimeZone.getDefault();

ISO 8601

Read about ISO 8601 formats. Both java.time and Joda-Time use that standard’s sensible formats as their defaults for both parsing and generating strings.


Actually, java.util.Date does have a time zone, buried deep under layers of source code. For most practical purposes, that time zone is ignored. So, as shorthand, we say java.util.Date has no time zone. Furthermore, that buried time zone is not the one used by Date’s toString method; that method uses the JVM’s current default time zone. All the more reason to avoid this confusing class and stick with Joda-Time and java.time.

Answer from Basil Bourque on Stack Overflow
Top answer
1 of 16
535

tl;dr

Instant.now()   // Capture the current moment in UTC. 

Generate a String to represent that value:

Instant.now().toString()  

2016-09-13T23:30:52.123Z

Details

As the correct answer by Jon Skeet stated, a java.util.Date object has no time zone. But its toString implementation applies the JVM’s default time zone when generating the String representation of that date-time value. Confusingly to the naïve programmer, a Date seems to have a time zone but does not.

The java.util.Date, j.u.Calendar, and java.text.SimpleDateFormat classes bundled with Java are notoriously troublesome. Avoid them. Instead, use either of these competent date-time libraries:

  • java.time.* package in Java 8
  • Joda-Time

java.time (Java 8)

Java 8 brings an excellent new java.time.* package to supplant the old java.util.Date/Calendar classes.

Getting current time in UTC/GMT is a simple one-liner…

Instant instant = Instant.now();

That Instant class is the basic building block in java.time, representing a moment on the timeline in UTC with a resolution of nanoseconds.

In Java 8, the current moment is captured with only up to milliseconds resolution. Java 9 brings a fresh implementation of Clock captures the current moment in up to the full nanosecond capability of this class, depending on the ability of your host computer’s clock hardware.

toString method of Instant generates a String representation of its value using one specific ISO 8601 format. That format outputs zero, three, six or nine digits digits (milliseconds, microseconds, or nanoseconds) as necessary to represent the fraction-of-second.

If you want more flexible formatting, or other additional features, then apply an offset-from-UTC of zero, for UTC itself (ZoneOffset.UTC constant) to get a OffsetDateTime.

OffsetDateTime now = OffsetDateTime.now( ZoneOffset.UTC );

Dump to console…

System.out.println( "now.toString(): " + now );

When run…

now.toString(): 2014-01-21T23:42:03.522Z

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
  • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
  • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
  • Later versions of Android bundle implementations of the java.time classes.
  • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


Joda-Time

UPDATE: The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

Using the Joda-Time 3rd-party open-source free-of-cost library, you can get the current date-time in just one line of code.

Joda-Time inspired the new java.time.* classes in Java 8, but has a different architecture. You may use Joda-Time in older versions of Java. Joda-Time continues to work in Java 8 and continues to be actively maintained (as of 2014). However, the Joda-Time team does advise migration to java.time.

System.out.println( "UTC/GMT date-time in ISO 8601 format: " + new org.joda.time.DateTime( org.joda.time.DateTimeZone.UTC ) );

More detailed example code (Joda-Time 2.3)…

org.joda.time.DateTime now = new org.joda.time.DateTime(); // Default time zone.
org.joda.time.DateTime zulu = now.toDateTime( org.joda.time.DateTimeZone.UTC );

Dump to console…

System.out.println( "Local time in ISO 8601 format: " + now );
System.out.println( "Same moment in UTC (Zulu): " + zulu );

When run…

Local time in ISO 8601 format: 2014-01-21T15:34:29.933-08:00
Same moment in UTC (Zulu): 2014-01-21T23:34:29.933Z

For more example code doing time zone work, see my answer to a similar question.

Time Zone

I recommend you always specify a time zone rather than relying implicitly on the JVM’s current default time zone (which can change at any moment!). Such reliance seems to be a common cause of confusion and bugs in date-time work.

When calling now() pass the desired/expected time zone to be assigned. Use the DateTimeZone class.

DateTimeZone zoneMontréal = DateTimeZone.forID( "America/Montreal" );
DateTime now = DateTime.now( zoneMontréal );

That class holds a constant for UTC time zone.

DateTime now = DateTime.now( DateTimeZone.UTC );

If you truly want to use the JVM’s current default time zone, make an explicit call so your code is self-documenting.

DateTimeZone zoneDefault = DateTimeZone.getDefault();

ISO 8601

Read about ISO 8601 formats. Both java.time and Joda-Time use that standard’s sensible formats as their defaults for both parsing and generating strings.


Actually, java.util.Date does have a time zone, buried deep under layers of source code. For most practical purposes, that time zone is ignored. So, as shorthand, we say java.util.Date has no time zone. Furthermore, that buried time zone is not the one used by Date’s toString method; that method uses the JVM’s current default time zone. All the more reason to avoid this confusing class and stick with Joda-Time and java.time.

2 of 16
444

java.util.Date has no specific time zone, although its value is most commonly thought of in relation to UTC. What makes you think it's in local time?

To be precise: the value within a java.util.Date is the number of milliseconds since the Unix epoch, which occurred at midnight January 1st 1970, UTC. The same epoch could also be described in other time zones, but the traditional description is in terms of UTC. As it's a number of milliseconds since a fixed epoch, the value within java.util.Date is the same around the world at any particular instant, regardless of local time zone.

I suspect the problem is that you're displaying it via an instance of Calendar which uses the local timezone, or possibly using Date.toString() which also uses the local timezone, or a SimpleDateFormat instance, which, by default, also uses local timezone.

If this isn't the problem, please post some sample code.

I would, however, recommend that you use Joda-Time anyway, which offers a much clearer API.

🌐
Phrase
phrase.com › home › resources › blog › how to get the current utc date and time in java?
Solved: How to Get the Current UTC Date and Time in Java?
September 23, 2022 - Current Date in milliseconds is :1583954404789 Wed Mar 11 19:20:04 GMT 2020 Wed Mar 11 19:20:04 GMT 2020 Wed Mar 11 19:20:04 UTC 2020 · In general, this isn’t ideal as it displays the current time based on the time zone of the specified region, which may be different than GMT; therefore, you should ideally avoid it. A better and more modern option bundled within the core Java library (and not using any third-party offerings) is to use the java.time package.
🌐
Vultr Docs
docs.vultr.com › java › examples › get-current-datetime
Java Program to Get Current Date/TIme | Vultr Docs
December 16, 2024 - Import the Instant class. Use Instant.now() to get the GMT/UTC time. ... import java.time.Instant; Instant timestamp = Instant.now(); System.out.println("Current GMT/UTC Time: " + timestamp); Explain Code
🌐
W3Docs
w3docs.com › java
How can I get the current date and time in UTC or GMT in Java?
import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; public class CurrentDateTimeExample { public static void main(String[] args) { // Get the current date and time in UTC Instant instant = Instant.now(); // ...
🌐
Quora
quora.com › How-do-I-get-the-current-UTC-date-using-Java
How to get the current UTC date using Java - Quora
Answer (1 of 6): A2A. How do I get the current UTC date using Java? Following code will helps you [code]package com.ashok.sample; import java.util.Date; import java.util.TimeZone; import java.text.SimpleDateFormat; public class CurrentUtcDate { public static void main(String[] args) { Dat...
Top answer
1 of 7
18

Your code:

Date.from(Instant.now())

You are mixing the terrible legacy classes with their replacement, the modern java.time classes.

Don’t.

Never use Date. Certainly no need to mix with java.time.Instant.

To explain your particular example, understand that among the Date class’ many poor design choices is the anti-feature of its Date#toString method implicitly applying the JVM’s current default time zone while generating its text.

You ran your code on two different JVMs that had different current default time zones. So you got different outputs.

Sun, Oracle, and the JCP gave up on the legacy date-time classes. So should we all. I recommend you not spend time trying understand Date, Calendar, SimpleDateFormat, and such.

You asked:

Question: How do we display the UTC timestamp in a Java program?

Instant.now().toString()

See that code run live at IdeOne.com.

2021-01-22T21:50:18.887335Z

You said:

On Windows: …

On Linux: …

You’ll get the same consistent results from Instant.now().toString() across Windows, Linux, BSD, macOS, iOS, Android, AIX, and so on.


Here is a table I made to guide you in transitioning from the legacy classes.

2 of 7
7

The java.util.Date object is not a real date-time object like the modern date-time types; rather, it represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT (or UTC). When you print an object of java.util.Date, its toString method returns the date-time in the JVM's timezone, calculated from this milliseconds value. If you need to print the date-time in a different timezone, you will need to set the timezone to SimpleDateFormat and obtain the formatted string from it.

I would suggest you simply use Instant.now() which you can convert to other java.time type.

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.

  • For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.
  • If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

However, if you still want to use java.util.Date, use SimpleDateFormat as mentioned above.

Demo:

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        Date currentUtcTime = Date.from(Instant.now());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        System.out.println("Current UTC time is " + sdf.format(currentUtcTime));
    }
}

Output:

Current UTC time is 2021-01-22 21:53:07 UTC
🌐
Medium
medium.com › @AlexanderObregon › javas-instant-now-method-explained-5403bac7ec1e
Java’s Instant.now() Method Explained | Medium
September 13, 2024 - Here’s how the basic usage looks in code: import java.time.Instant; public class TimestampExample { public static void main(String[] args) { Instant timestamp = Instant.now(); System.out.println("Current Timestamp: " + timestamp); } }
🌐
Tutorjoes
tutorjoes.in › Java_example_programs › get_current_timestamp_in_java
Write a Java program to get current timestamp
Instant.now() method is used to obtain the current timestamp in UTC timezone. The timestamp is then printed to the console using ... import java.time.*; class Current_TimeStamp { public static void main(String[] args) { Instant ts = Instant.now(); System.out.println("Current Timestamp : " + ts); } }
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › java date time › get current timestamp in java
Get Current Timestamp in Java
April 4, 2023 - Timestamp timestamp1 = new Timestamp(System.currentTimeMillis()); Date date = new Date(); Timestamp timestamp2 = new Timestamp(date.getTime()); System.out.println(timestamp1); //2022-02-15 13:55:56.18 System.out.println(timestamp2); //2022-02-15 ...
🌐
Attacomsian
attacomsian.com › blog › java-get-unix-timestamp
How to get the Unix timestamp in Java
October 6, 2022 - To convert a Unix timestamp back ... using a universal timezone (UTC). In Java 7 and below, you can use the System.currentTimeMillis() method to get the current time in milliseconds....
🌐
Blogger
javarevisited.blogspot.com › 2012 › 01 › get-current-date-timestamps-java.html
How to get current Date Timestamps in Java on GMT or Local Timezone? Example Tutorial
From Java 8 onwards, you can also use LocalDate, LocalTime, and ZonedDateTime class to get the current date and timestamp in any timezone like GMT or UTC
🌐
Baeldung
baeldung.com › home › java › java dates › get the current date and time in java
Get the Current Date and Time in Java | Baeldung
January 8, 2024 - With java.time.LocalTime, let’s ... Use java.time.Instant to get a time stamp from the Java epoch. According to the JavaDoc, “epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z, where instants after the epoch have positive values: Instant instant = Instant.now(); long timeStampMillis = instant.toEpochMilli();...
🌐
Edureka Community
edureka.co › home › community › categories › java › how can i get the current date and time in utc or...
How can I get the current date and time in UTC or GMT in Java | Edureka Community
June 7, 2018 - When I create a new Date object, it is initialized to the current time but in the local timezone. How can I get the current date and time in GMT?
🌐
Baeldung
baeldung.com › home › java › java dates › retrieving unix time in java
Retrieving Unix Time in Java | Baeldung
January 8, 2024 - We can get the current Unix time by invoking getEpochSecond() on the Instant object: @Test void givenTimeUsingLocalDate_whenConvertedToUnixTime_thenMatch() { LocalDate date = LocalDate.of(2023, Month.FEBRUARY, 15); Instant instant = ...
🌐
Mkyong
mkyong.com › home › java › how to get current timestamps in java
How to Get Current Timestamps in Java - Mkyong.com
March 7, 2025 - ... package com.mkyong; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeExample { public static void main(String[] args) { LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter ...
🌐
Apache Commons
commons.apache.org › proper › commons-net › jacoco › org.apache.commons.net.ntp › TimeStamp.java.html
TimeStamp.java
* @see System#currentTimeMillis() */ public static TimeStamp getCurrentTime() { return getNtpTime(System.currentTimeMillis()); } // initialization of static time bases /* * static { TimeZone utcZone = TimeZone.getTimeZone("UTC"); Calendar calendar = Calendar.getInstance(utcZone); calendar.set(1900, Calendar.JANUARY, 1, 0, 0, * 0); calendar.set(Calendar.MILLISECOND, 0); msb1baseTime = calendar.getTime().getTime(); calendar.set(2036, Calendar.FEBRUARY, 7, 6, 28, 16); * calendar.set(Calendar.MILLISECOND, 0); msb0baseTime = calendar.getTime().getTime(); } */ /** * Gets an NTP timestamp object from a Java time.
🌐
GitHub
github.com › pgjdbc › pgjdbc › issues › 1108
Always use UTC for SQL TIMESTAMP to java.sql.Timestamp conversion · Issue #1108 · pgjdbc/pgjdbc
January 3, 2018 - @Override public Timestamp getTimestamp(int i, java.util.Calendar cal) throws SQLException { checkResultSet(i); if (wasNullFlag) { return null; } if (cal == null) { cal = getDefaultCalendar(); // This boils down to the default calendar used in Postgres all around which is the system calendar } int col = i - 1; int oid = fields[col].getOID(); if (isBinary(i)) { if (oid == Oid.TIMESTAMPTZ || oid == Oid.TIMESTAMP) { boolean hasTimeZone = oid == Oid.TIMESTAMPTZ; TimeZone tz = cal.getTimeZone(); return connection.getTimestampUtils().toTimestampBin(tz, this_row[col], hasTimeZone); } else { // JDBC s
Author   Davio