Java Get Time in UTC
UTC is the abbreviation for Universal Time Coordinated. Before the beginning of UTC, it is mentioned as the Greenwich Mean Time (GMT) but Now it is mentioned as the universal time coordinated or coordinated universal time. Sometimes UTC time zone is denoted by the letter Z where Z stands for “Zulu”. It is used to change time from one time zone to another time zone
- The GMT and UTC are not similar. Because UTC is not a time zone whereas GMT is the time zone. Mainly in Europe and Africa, and also in some parts of the world the GMT Time zone is not used. For displaying it uses 24 hrs or 12 hrs format.
- UTC is a time standard used for displaying the time zone. Based on UTC we can calculate the time in different regions. For example, by adding a certain number of hours from UTC we can calculate the time in India i.e,(+5:30).For calculation, like addition or subtraction value is Depended upon how many time zones you are away from Greenwich (England) we can calculate. UTC does not affect due to the switch to daylight saving time and is used to store dates and times in databases.
- java.util.TimeZone is used. If we use java.util.date package there will be some limitations due to several reasons.
- No time zone.
- The date is not represented but an instance in time in milliseconds since Nov1, 1970 (UNIX epoch).
- Different variations are present in the UTC format. Some formats are UTC has been replaced with Z without a space, in ISO-8601 is xx:xx:xx ZUTC. 12 hrs (AM/PM) xx:xx:xx is another UTC format.
Program
Below is the program to display the number of time zones in a system.
import java.text.ParseException;
import java.util.TimeZone;
public class Timezones{
public static void main(String[] args) throws ParseException {
String[] availableIDs = TimeZone.getAvailableIDs();
for(String id : availableIDs) {
System.out.println("Timezone = " + id);
}
}
}
Output:
Timezone = Africa/Abidjan
Timezone = Africa/Accra
Timezone = Africa/Addis_Ababa
Timezone = Africa/Algiers……Timezone = SST
Timezone = VST
To get the time in UTC there are many ways available such as using below are:
- SimpleDateFormat
- Java 8 Instant
- Java8 OffsetDateTime
SimpleDateFormat
In getting the UTC time this class plays a major role. It is mostly used in java8(older versions).
Program:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.text.ParseException;
SimpleDateFormat
public class UtcExample{
public static void main(String args[]){
try {
System.out.println("IST Time is:"+new Date());
System.out.println("UTC Time is: "+getCurrentUtcTime());
}
catch (ParseException e) {
e.printStackTrace();
}
}
public static Date getCurrentUtcTime() throws ParseException { // handling ParseException
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
// set UTC time zone by using SimpleDateFormat class
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat ldf = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
Date d1 = null;
try {
d1 = ldf.parse( sdf.format(new Date()) );
}
catch (java.text.ParseException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return d1;
}}
Output:
IST Time is:Fri Aug 19 21:44:18 IST 2022
UTC Time is: Fri Aug 19 16:14:18 IST 2022
Java 8 Instant
By creating an instance of the Instant object we can get the UTC time. An excellent new Java.time.* package was brought by the jav8 for using the old java Date/Calendar classes. Timeline moment in UTC as the default time zone is defined by the instant class.
Program:
import java.time.Instant;
import java.util.Date;
public class UtcExample{
public static void main(String[] args)
{
System.out.println("IST Time is:"+new Date());
// print current UTC time
System.out.println("UTC Time is: "+getCurrentUtcTime());
}
public static String getCurrentUtcTime() {
String d1;
Instant instant = Instant.now();
d1 = instant.toString();
return d1;
}
}
Output:
IST Time is:Fri Aug 19 22:07:56 IST 2022
UTC Time is: 2022-08-19T16:37:56.270855500Z
Java 8 OffsetDateTime
ForGetting the current UTC time in java it is one of the important class.unchange0 representation of a date-time with an offset is done by this class mainly for storing the date-time field into the precision of nanoseconds.
Program:
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Date;
public class UtcExample {
public static void main(String args[]) {
// print current time
System.out.println("IST Time is:"+new Date());
// print current UTC time
System.out.println("UTC Time is: "+getCurrentUtcTime());
}
public static OffsetDateTime getCurrentUtcTime() {
OffsetDateTime d1;
d1 = OffsetDateTime.now(ZoneOffset.UTC);
// pass UTC date to the main method.
return d1;
}}
Output:
IST Time is:Fri Aug 19 22:28:48 IST 2022
UTC Time is: 2022-08-19T16:58:48.397321Z
But among all the above-mentioned ways we mostly use the SimpleDateFormat class to get the time in UTC.