Java TimeZone Add adjustTimeToDefaultTimezone(long time)

Here you can find the source of adjustTimeToDefaultTimezone(long time)

Description

Shift the time from UTC to the local timezone

License

Open Source License

Parameter

Parameter Description
time a parameter

Declaration

public static long adjustTimeToDefaultTimezone(long time) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static final long SECOND_FACTOR = 1000;
    public static final long MINUTE_FACTOR = 60 * SECOND_FACTOR;
    public static final long HOUR_FACTOR = 60 * MINUTE_FACTOR;

    /**/*ww w  .jav  a  2s.  c o m*/
     * Shift the time from UTC to the local timezone
     * @param time
     * @return
     */
    public static long adjustTimeToDefaultTimezone(long time) {
        return time + getDefaultTimeZoneOffset();
    }

    /**
     * Get the offset between GMT and the local timezone
     * @return the offset
     */
    public static long getDefaultTimeZoneOffset() {
        long offset = 0;
        TimeZone zn = TimeZone.getDefault();
        Calendar local = Calendar.getInstance();
        local.setTime(new Date(System.currentTimeMillis()));

        // the offset to add to GMT to get local time, modified in case of
        // daylight savings
        int time = (int) (local.get(Calendar.HOUR_OF_DAY) * HOUR_FACTOR + local.get(Calendar.MINUTE) * MINUTE_FACTOR
                + local.get(Calendar.SECOND) * SECOND_FACTOR);
        offset = zn.getOffset(1, // era AD
                local.get(Calendar.YEAR), local.get(Calendar.MONTH), local.get(Calendar.DAY_OF_MONTH),
                local.get(Calendar.DAY_OF_WEEK), time);
        return offset;
    }
}

Related

  1. addTimeZone(String id, double off)
  2. baseFor(TimeZone tz)
  3. cal(TimeZone tz)
  4. isAfter(int month1, int day1, int year1, int hour1, int minute1, int amPm1, int month2, int day2, int year2, int hour2, int minute2, int amPm2, TimeZone timeZone, Locale locale)
  5. isFuture(int month, int year, TimeZone timeZone, Locale locale)