How to get Current UTC Time in Java - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here we will see how to get current UTC time in java in different ways.
In older versions of java like before Java8, we can get current UTC time using SimpleDateFormate object like below.
private static Date getCurrentUtcTime() throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
    return localDateFormat.parse( simpleDateFormat.format(new Date()) );
}
Output:
Sat Jun 08 04:54:47 IST 2019
Java 8 brought an excellent new java.time.*package to supplement the old Java Date/Calendar classes. From Java 8 getting the UTC time as simple as creating Instant object like the following.
An Instant class represents a moment of the timeline in UTC as default timezone.
Instant instant = Instant.now();
System.out.println(instant.toString());
Output:
2019-06-08T04:54:47.543Z
UTC time with OffsetDateTime class.
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);
System.out.println(now);
Add the Joda dependency in your classpath.
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
 <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.10.2</version>
 </dependency>
Using Joda Time API, you can get current UTC time in a single line below.
private static void getCurrentUtcTimeJoda() throws ParseException {
    DateTime now = new DateTime(); // Gives the default time zone.
    DateTime dateTime = now.toDateTime(DateTimeZone.UTC ); // Converting default zone to UTC
    System.out.println(dateTime);
}
Output:
2019-06-08T04:54:47.731Z
Done!
Java 8 Time API
Joda Time API
Happy Learning 🙂

DateTime API in Java 8 Example
How to convert Timestamp to Date in Java
How to set Spring Boot SetTimeZone
Spring Boot Scheduler Job @EnableScheduling example
Ubuntu – How to set default Java version on Ubuntu
Java 8 forEach with index Example
Java 8 Stream Filter Example with Objects
How to Filter a Map in Java 8
Java 8 groupingBy Example
Java 8 – Convert java.util.Date to java.time.LocalDate
How to convert List to Map in Java 8
User defined sorting with Java 8 Comparator
5 Ways to Iterate ArrayList in Java
Java 8 How to Convert List to String comma separated values
Java – How to Convert InputStream to String

DateTime API in Java 8 Example
How to convert Timestamp to Date in Java
How to set Spring Boot SetTimeZone
Spring Boot Scheduler Job @EnableScheduling example
Ubuntu – How to set default Java version on Ubuntu
Java 8 forEach with index Example
Java 8 Stream Filter Example with Objects
How to Filter a Map in Java 8
Java 8 groupingBy Example
Java 8 – Convert java.util.Date to java.time.LocalDate
How to convert List to Map in Java 8
User defined sorting with Java 8 Comparator
5 Ways to Iterate ArrayList in Java
Java 8 How to Convert List to String comma separated values
Java – How to Convert InputStream to String
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String