Java Date Convert convertDateValueToMillis(TimeZone tz, long dateValue)

Here you can find the source of convertDateValueToMillis(TimeZone tz, long dateValue)

Description

Convert an encoded date value to millis, using the supplied timezone.

License

Mozilla Public License

Parameter

Parameter Description
tz the timezone
dateValue the date value

Return

the date

Declaration

public static long convertDateValueToMillis(TimeZone tz, long dateValue) 

Method Source Code

//package com.java2s;
/*/*from w  ww  .j a  v  a  2 s . com*/
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, and the
 * EPL 1.0 (http://h2database.com/html/license.html). Initial Developer: H2
 * Group Iso8601: Initial Developer: Robert Rathsack (firstName dot lastName at
 * gmx dot de)
 */

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

import java.util.TimeZone;

public class Main {
    private static final int SHIFT_YEAR = 9;
    private static final int SHIFT_MONTH = 5;
    /**
     * The thread local. Can not override initialValue because this would result
     * in an inner class, which would not be garbage collected in a web
     * container, and prevent the class loader of H2 from being garbage
     * collected. Using a ThreadLocal on a system class like Calendar does not
     * have that problem, and while it is still a small memory leak, it is not a
     * class loader memory leak.
     */
    private static final ThreadLocal<Calendar> CACHED_CALENDAR = new ThreadLocal<Calendar>();
    /**
     * A cached instance of Calendar used when a timezone is specified.
     */
    private static final ThreadLocal<Calendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE = new ThreadLocal<Calendar>();

    /**
     * Convert an encoded date value to millis, using the supplied timezone.
     *
     * @param tz the timezone
     * @param dateValue the date value
     * @return the date
     */
    public static long convertDateValueToMillis(TimeZone tz, long dateValue) {
        return getMillis(tz, yearFromDateValue(dateValue), monthFromDateValue(dateValue),
                dayFromDateValue(dateValue), 0, 0, 0, 0);
    }

    /**
     * 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, or null for the default
     *            timezone
     * @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);
            }
        }
    }

    /**
     * Get the year from a date value.
     *
     * @param x the date value
     * @return the year
     */
    public static int yearFromDateValue(long x) {
        return (int) (x >>> SHIFT_YEAR);
    }

    /**
     * Get the month from a date value.
     *
     * @param x the date value
     * @return the month (1..12)
     */
    public static int monthFromDateValue(long x) {
        return (int) (x >>> SHIFT_MONTH) & 15;
    }

    /**
     * Get the day of month from a date value.
     *
     * @param x the date value
     * @return the day (1..31)
     */
    public static int dayFromDateValue(long x) {
        return (int) (x & 31);
    }

    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 = getCalendar(tz);
        }
        c.setLenient(lenient);
        setCalendarFields(c, year, month, day, hour, minute, second, millis);
        return c.getTime().getTime();
    }

    /**
     * Get a calendar for the default timezone.
     *
     * @return a calendar instance. A cached instance is returned where possible
     */
    private static Calendar getCalendar() {
        Calendar c = CACHED_CALENDAR.get();
        if (c == null) {
            c = Calendar.getInstance();
            CACHED_CALENDAR.set(c);
        }
        c.clear();
        return c;
    }

    /**
     * Get a calendar for the given timezone.
     *
     * @param tz timezone for the calendar, is never null
     * @return a calendar instance. A cached instance is returned where possible
     */
    private static Calendar getCalendar(TimeZone tz) {
        Calendar c = CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.get();
        if (c == null || !c.getTimeZone().equals(tz)) {
            c = Calendar.getInstance(tz);
            CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.set(c);
        }
        c.clear();
        return c;
    }

    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. convertDateToLong(int year, int month, int day, int hour, int minute, int second)
  2. convertDateToServerTimeZoneDateInMilliSec(String timeZoneName, long milliSecond)
  3. convertDateToTimeStamp(Date date)
  4. convertDateToUTC(Date date)
  5. convertDateToWindowsTime(Date javaDate)
  6. convertDoubleTimeToDate(Number d)
  7. convertFromFileman(String date)
  8. convertInputDateToString(Date inputDate)
  9. convertInt2Date(int date)