convert To Gmt Timezone - Java java.util

Java examples for java.util:Time

Description

convert To Gmt Timezone

Demo Code


//package com.java2s;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar cal = Calendar.getInstance();
        String timeZone = "java2s.com";
        System.out.println(convertToGmt(cal, timeZone));
    }//from   w  w  w. j a v a  2s. c om

    public static Calendar convertToGmt(Calendar cal, String timeZone) {

        Date date = cal.getTime();
        TimeZone tz = cal.getTimeZone();

        System.out.println("Input calendar has date [" + date
                + "] for Time Zone : '" + tz.getDisplayName() + "'");

        // Create a new calendar in GMT timezone, set to this date and add the offset
        Calendar cl = new java.util.GregorianCalendar(
                TimeZone.getTimeZone(timeZone));
        cl.setTime(date);

        System.out.println("Created '" + timeZone + "' cal with date ["
                + cl.getTime() + "]");

        return cal;
    }
}

Related Tutorials