In Java when you create a new Date OR Calendar object, it is initialized to the current time but in the local timezone where the program is running. For example, for a program running in India, creates a TimeZone object based on Indian Standard Time.
In your java carrier you may need to get an UTC time (Coordinated Universal Time) or GMT time (Greenwich Mean Time). For this java provides a simple class known as TimeZone, which help us to get date/time from any geographical region accross the globe. All you need to do is pass correct time zone abbreviation.
To know more about TimeZone, see API.
Below are the examples of few time zone abbreviations
Get list of all time zone abbreviations, click here.
/* UTCDates.java */
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
public class UTCDates {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
String strDate;
sdFormat.setTimeZone(TimeZone.getDefault());
strDate = sdFormat.format(cal.getTime());
System.out.println("Now : "+strDate);
sdFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
strDate = sdFormat.format(cal.getTime());
System.out.println("UTC : "+strDate);
}
}
Output :
Also Apache provides us an API to do this. To know more about it, click here.
You need to download commons-lang-x.x.jar and set it in classpath for example commons-lang-2.1.jar file.
To download commons-lang-x.x.jar, click here.
/* UTCDates.java */
import java.util.Calendar;
import org.apache.commons.lang.time.DateFormatUtils;
public class UTCDates {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
String strDate;
strDate = DateFormatUtils.ISO_DATETIME_FORMAT.format(cal.getTime());
System.out.println("Now : "+strDate);
strDate = DateFormatUtils.formatUTC(cal.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern());
System.out.println("UTC : "+strDate);
}
}
More formats
In your java carrier you may need to get an UTC time (Coordinated Universal Time) or GMT time (Greenwich Mean Time). For this java provides a simple class known as TimeZone, which help us to get date/time from any geographical region accross the globe. All you need to do is pass correct time zone abbreviation.
To know more about TimeZone, see API.
Below are the examples of few time zone abbreviations
Abbr | Name | UTC offset |
---|---|---|
AKST | Alaska Standard Time | UTC−09 |
AST | Atlantic Standard Time | UTC−04 |
CET | Central European Time | UTC+01 |
CT | China Time | UTC+08 |
IST | Indian Standard Time | UTC+05:30 |
SGT | Singapore Time | UTC+08 |
UTC | Coordinated Universal Time | UTC |
Get list of all time zone abbreviations, click here.
/* UTCDates.java */
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
public class UTCDates {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
String strDate;
sdFormat.setTimeZone(TimeZone.getDefault());
strDate = sdFormat.format(cal.getTime());
System.out.println("Now : "+strDate);
sdFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
strDate = sdFormat.format(cal.getTime());
System.out.println("UTC : "+strDate);
}
}
Output :
Now : 2011-12-15T05:25:22
UTC : 2011-12-15T11:55:22
Also Apache provides us an API to do this. To know more about it, click here.
You need to download commons-lang-x.x.jar and set it in classpath for example commons-lang-2.1.jar file.
To download commons-lang-x.x.jar, click here.
import java.util.Calendar;
import org.apache.commons.lang.time.DateFormatUtils;
public class UTCDates {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
String strDate;
strDate = DateFormatUtils.ISO_DATETIME_FORMAT.format(cal.getTime());
System.out.println("Now : "+strDate);
strDate = DateFormatUtils.formatUTC(cal.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern());
System.out.println("UTC : "+strDate);
}
}
More formats
FastDateFormat | Pattern |
---|---|
ISO_DATETIME_FORMAT | yyyy-MM-dd'T'HH:mm:ss |
ISO_DATETIME_TIME_ZONE_FORMAT | yyyy-MM-dd'T'HH:mm:ssZZ |
ISO_DATE_FORMAT | yyyy-MM-dd |
ISO_DATE_TIME_ZONE_FORMAT | yyyy-MM-ddZZ |
ISO_TIME_FORMAT | 'T'HH:mm:ss |
ISO_TIME_TIME_ZONE_FORMAT | 'T'HH:mm:ssZZ |
ISO_TIME_NO_T_FORMAT | HH:mm:ss |
ISO_TIME_NO_T_TIME_ZONE_FORMAT | HH:mm:ssZZ |
SMTP_DATETIME_FORMAT | EEE, dd MMM yyyy HH:mm:ss Z |