Java Date Convert convertSistemDependentDateToDesiredTimeZone(final Date sistemDependentDate, TimeZone desiredTimeZone)

Here you can find the source of convertSistemDependentDateToDesiredTimeZone(final Date sistemDependentDate, TimeZone desiredTimeZone)

Description

Convert sistem dependent date to desired time zone.

License

Open Source License

Parameter

Parameter Description
sistemDependentDate the sistem dependent date
desiredTimeZone the desired time zone

Return

the date

Declaration

public static Date convertSistemDependentDateToDesiredTimeZone(final Date sistemDependentDate,
        TimeZone desiredTimeZone) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

import java.util.TimeZone;

public class Main {
    /**//from w  ww  .  j  a v a 2s .  co m
     * Convert sistem dependent date to desired time zone. Mongo siempre guarda
     * las fechas en GMT0/UTC0. Al recuperar una fecha, mongo automaticamente la
     * devuelve expresada en la zona horaria del sistema desde donde se pidio la
     * fecha. Este metodo transforma una fecha a una zona horaria deseada,
     * teniendo en cuenta el local desde donde se ejecuta el codigo.
     * 
     * @param sistemDependentDate
     *            the sistem dependent date
     * @param desiredTimeZone
     *            the desired time zone
     * @return the date
     */
    public static Date convertSistemDependentDateToDesiredTimeZone(final Date sistemDependentDate,
            TimeZone desiredTimeZone) {

        Long msDate = sistemDependentDate.getTime();
        int systemTimeZoneOffset = TimeZone.getDefault().getOffset(Calendar.getInstance().getTimeInMillis());
        if (desiredTimeZone == null) {
            desiredTimeZone = TimeZone.getDefault();
        }
        int desiredTimeZoneOffset = desiredTimeZone.getOffset(Calendar.getInstance().getTimeInMillis());
        int totalMsOffset = -systemTimeZoneOffset + desiredTimeZoneOffset;
        msDate = msDate + totalMsOffset;
        return new Date(msDate);
    }
}

Related

  1. convertIntToDate(final int iDate, final StringBuilder verbatimDate)
  2. convertLocalDateToUTCDate(Date localDate)
  3. convertLocalToGMT(int[] dateTime)
  4. convertLocalToTimezone(Date original, String targetTimezoneId)
  5. convertsDateToGMTTimezone(Date date)
  6. convertStringToDate(String date)
  7. convertStringToDate(String date)
  8. convertStringToDate(String string)
  9. convertToClientTime(Date serverDate, Integer clientTimezoneOffset)