Java TimeZone Get getMillis(TimeZone tz, int year, int month, int day, int hour, int minute, int second, int millis)

Here you can find the source of getMillis(TimeZone tz, int year, int month, int day, int hour, int minute, int second, int millis)

Description

Calculate the milliseconds since 1970-01-01 (UTC) for the given date and time (in the specified timezone).

License

Open Source License

Parameter

Parameter Description
tz the timezone of the parameters
year the absolute year (positive or negative)
month the month (1-12)
day the day (1-31)
hour the hour (0-23)
minute the minutes (0-59)
second the number of seconds (0-59)
millis the number of milliseconds

Return

the number of milliseconds (UTC)

Declaration

public static long getMillis(TimeZone tz, int year, int month, int day, int hour, int minute, int second,
        int millis) 

Method Source Code

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

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

public class Main {
    private static int zoneOffset;
    private static Calendar cachedCalendar;

    /**/*from   w ww  .j a v  a  2  s.  c  om*/
     * Calculate the milliseconds since 1970-01-01 (UTC) for the given date and
     * time (in the specified timezone).
     *
     * @param tz the timezone of the parameters
     * @param year the absolute year (positive or negative)
     * @param month the month (1-12)
     * @param day the day (1-31)
     * @param hour the hour (0-23)
     * @param minute the minutes (0-59)
     * @param second the number of seconds (0-59)
     * @param millis the number of milliseconds
     * @return the number of milliseconds (UTC)
     */
    public static long getMillis(TimeZone tz, int year, int month, int day, int hour, int minute, int second,
            int millis) {
        try {
            return getTimeTry(false, tz, year, month, day, hour, minute, second, millis);
        } catch (IllegalArgumentException e) {
            // special case: if the time simply doesn't exist because of
            // daylight saving time changes, use the lenient version
            String message = e.toString();
            if (message.indexOf("HOUR_OF_DAY") > 0) {
                if (hour < 0 || hour > 23) {
                    throw e;
                }
                return getTimeTry(true, tz, year, month, day, hour, minute, second, millis);
            } else if (message.indexOf("DAY_OF_MONTH") > 0) {
                int maxDay;
                if (month == 2) {
                    maxDay = new GregorianCalendar().isLeapYear(year) ? 29 : 28;
                } else {
                    maxDay = 30 + ((month + (month > 7 ? 1 : 0)) & 1);
                }
                if (day < 1 || day > maxDay) {
                    throw e;
                }
                // DAY_OF_MONTH is thrown for years > 2037
                // using the timezone Brasilia and others,
                // for example for 2042-10-12 00:00:00.
                hour += 6;
                return getTimeTry(true, tz, year, month, day, hour, minute, second, millis);
            } else {
                return getTimeTry(true, tz, year, month, day, hour, minute, second, millis);
            }
        }
    }

    private static long getTimeTry(boolean lenient, TimeZone tz, int year, int month, int day, int hour, int minute,
            int second, int millis) {
        Calendar c;
        if (tz == null) {
            c = getCalendar();
        } else {
            c = Calendar.getInstance(tz);
        }
        synchronized (c) {
            c.clear();
            c.setLenient(lenient);
            setCalendarFields(c, year, month, day, hour, minute, second, millis);
            return c.getTime().getTime();
        }
    }

    private static Calendar getCalendar() {
        if (cachedCalendar == null) {
            cachedCalendar = Calendar.getInstance();
            zoneOffset = cachedCalendar.get(Calendar.ZONE_OFFSET);
        }
        return cachedCalendar;
    }

    private static void setCalendarFields(Calendar cal, int year, int month, int day, int hour, int minute,
            int second, int millis) {
        if (year <= 0) {
            cal.set(Calendar.ERA, GregorianCalendar.BC);
            cal.set(Calendar.YEAR, 1 - year);
        } else {
            cal.set(Calendar.ERA, GregorianCalendar.AD);
            cal.set(Calendar.YEAR, year);
        }
        // january is 0
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, second);
        cal.set(Calendar.MILLISECOND, millis);
    }
}

Related

  1. getHourPart(TimeZone timeZone)
  2. getJapanTimeZone()
  3. getMediumDisplayDate(Date moment, TimeZone tz, Locale inLocale)
  4. getMicrosoftTimeZoneName(TimeZone timeZone)
  5. getMilitaryTimeZoneCharacter(TimeZone tz)
  6. getMoscowCalendar()
  7. getMoscowTimeZone()
  8. getNextHour(final String timezone)
  9. getPossibleUserTimezoneList(int userOffsetInHours)