Java Data Type How to - Get the current date and time in UTC or GMT


We would like to know how to get the current date and time in UTC or GMT.


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
//w ww  . j av  a  2s. com
public class Main {
    public static void main (String[] args){
        System.out.println(my_time_in("GMT-5:00", "MM/dd/yyyy HH:mm:ss") );
        System.out.println(my_time_in("GMT+5:30", "HH:mm a z 'on' MM/dd/yyyy"));

        System.out.println(my_time_in("America/Los_Angeles", "HH:mm a z 'on' MM/dd/yyyy") );
        System.out.println(my_time_in("America/Buenos_Aires", "HH:mm a z 'on' MM/dd/yyyy") );

    public static String my_time_in(String target_time_zone, String format){
        TimeZone tz = TimeZone.getTimeZone(target_time_zone);
        Date date = Calendar.getInstance().getTime();
        SimpleDateFormat date_format_gmt = new SimpleDateFormat(format);
        return date_format_gmt.format(date);

The code above generates the following result.