Java TimeZone Get getAdjustedDateTime(Date dt, TimeZone tz)

Here you can find the source of getAdjustedDateTime(Date dt, TimeZone tz)

Description

Returns a Date object adjusted for the given timezone.

License

Open Source License

Parameter

Parameter Description
dt The date to be adjusted.
tz The desired timezone.

Declaration

public static Date getAdjustedDateTime(Date dt, TimeZone tz) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) {2009,2011} {Software Design and Collaboration Laboratory (SDCL)
 *            , University of California, Irvine}.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*ww  w .j ava 2  s. c o m*/
 *    {Software Design and Collaboration Laboratory (SDCL)
 *   , University of California, Irvine} 
 *         - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import java.util.TimeZone;

public class Main {
    /**
     * Returns a <code>Date</code> object adjusted for the given timezone.
     * 
     * @param dt The date to be adjusted.
     * @param tz The desired timezone.
     * @return
     */
    public static Date getAdjustedDateTime(Date dt, TimeZone tz) {
        Date result = null;
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
        df.setTimeZone(tz);
        try {
            result = df.parse(df.format(dt));
        } catch (ParseException e) {
            // The exception will never be thrown, since we are parsing the same
            // pattern set in the DateFormat object.
        }
        return result;
    }
}

Related

  1. getAllTimezonesSorted()
  2. getAsTimeZone(Map params, String propName)
  3. getAvailableTimezones()
  4. getCal(int dayOfMonth, int month, int year, int hours, int minutes, int seconds, String timezone)