How do I convert time between timezone?

Category: java.util, viewed: 15737 time(s).
package org.kodejava.example.util;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class TimeZoneExample {
    public static void main(String[] args) {
        //
        // Create a calendar object and set it time based on the local
        // time zone
        //
        Calendar localTime = Calendar.getInstance();
        localTime.set(Calendar.HOUR, 17);
        localTime.set(Calendar.MINUTE, 15);
        localTime.set(Calendar.SECOND, 20);

        int hour = localTime.get(Calendar.HOUR);
        int minute = localTime.get(Calendar.MINUTE);
        int second = localTime.get(Calendar.SECOND);

        //
        // Print the local time
        //
        System.out.printf("Local time  : %02d:%02d:%02d\n", hour, minute, second);

        //
        // Create a calendar object for representing a Germany time zone. Then we
        // wet the time of the calendar with the value of the local time
        //
        Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
        germanyTime.setTimeInMillis(localTime.getTimeInMillis());
        hour = germanyTime.get(Calendar.HOUR);
        minute = germanyTime.get(Calendar.MINUTE);
        second = germanyTime.get(Calendar.SECOND);

        //
        // Print the local time in Germany time zone
        //
        System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats