Java Timestamp Convert To timestampToTicks(int year, int month, int day, int hours, int minutes, int seconds, int milliseconds)

Here you can find the source of timestampToTicks(int year, int month, int day, int hours, int minutes, int seconds, int milliseconds)

Description

Converts a field by field timestamp into millisecond ticks.

License

Apache License

Parameter

Parameter Description
year The year.
month The month.
day The day.
hours The hours.
minutes The minutes.
seconds The seconds.
milliseconds The milliseconds.

Return

Millisecond ticks since midnight 01/01/1970.

Declaration

public static long timestampToTicks(int year, int month, int day, int hours, int minutes, int seconds,
        int milliseconds) 

Method Source Code

//package com.java2s;
/*//  w w w . j a va2 s.  c  o  m
 * Copyright The Sett Ltd, 2005 to 2014.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /** The number of milliseconds in a second. */
    private static final int MILLIS_PER_SECOND = 1000;
    /** The number of milliseconds in a minute. */
    public static final int MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
    /** The number of milliseconds in an hour. */
    public static final int MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
    /** The number of milliseconds in a day. */
    private static final int MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;
    /** The number of days from 0000 to 1970. */
    private static final int DAYS_TO_1970 = 719527;
    /** Tables the number of days in the year prior to the start of each month, for leap years. */
    private static final int[] LEAP_DAYS_IN_YEAR_PRIOR_TO_MONTH = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274,
            305, 335 };
    /** Tables the number of days in the year prior to the start of each month. */
    private static final int[] USUAL_DAYS_IN_YEAR_PRIOR_TO_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273,
            304, 334 };

    /**
     * Converts a field by field timestamp into millisecond ticks.
     *
     * @param  year         The year.
     * @param  month        The month.
     * @param  day          The day.
     * @param  hours        The hours.
     * @param  minutes      The minutes.
     * @param  seconds      The seconds.
     * @param  milliseconds The milliseconds.
     *
     * @return Millisecond ticks since midnight 01/01/1970.
     */
    public static long timestampToTicks(int year, int month, int day, int hours, int minutes, int seconds,
            int milliseconds) {
        boolean isLeapYear = isLeapYear(year);

        long dayComponent = (long) (day - 1) * MILLIS_PER_DAY;
        long monthComponent = millisToStartOfMonth(month, isLeapYear);
        long yearComponent = millisToYearStart(year);
        long hoursComponent = (long) hours * MILLIS_PER_HOUR;
        long minutesComponent = (long) minutes * MILLIS_PER_MINUTE;
        long secondsComponent = (long) seconds * MILLIS_PER_SECOND;

        return dayComponent + monthComponent + yearComponent + hoursComponent + minutesComponent + secondsComponent
                + milliseconds;
    }

    /**
     * Converts a field by field timestamp into millisecond ticks.
     *
     * @param  year    The year.
     * @param  month   The month.
     * @param  day     The day.
     * @param  hours   The hours.
     * @param  minutes The minutes.
     * @param  seconds The seconds.
     *
     * @return Millisecond ticks since midnight 01/01/1970.
     */
    public static long timestampToTicks(int year, int month, int day, int hours, int minutes, int seconds) {
        boolean isLeapYear = isLeapYear(year);

        long dayComponent = (long) (day - 1) * MILLIS_PER_DAY;
        long monthComponent = millisToStartOfMonth(month, isLeapYear);
        long yearComponent = millisToYearStart(year);
        long hoursComponent = (long) hours * MILLIS_PER_HOUR;
        long minutesComponent = (long) minutes * MILLIS_PER_MINUTE;
        long secondsComponent = (long) seconds * MILLIS_PER_SECOND;

        return dayComponent + monthComponent + yearComponent + hoursComponent + minutesComponent + secondsComponent;
    }

    /**
     * Converts a field by field timestamp into millisecond ticks.
     *
     * @param  year  The year.
     * @param  month The month.
     * @param  day   The day.
     *
     * @return Millisecond ticks since midnight 01/01/1970.
     */
    public static long timestampToTicks(int year, int month, int day) {
        boolean isLeapYear = isLeapYear(year);

        long dayComponent = (long) (day - 1) * MILLIS_PER_DAY;
        long monthComponent = millisToStartOfMonth(month, isLeapYear);
        long yearComponent = millisToYearStart(year);

        return dayComponent + monthComponent + yearComponent;
    }

    /**
     * Determines whether or not a year is a leap year. This takes no account of when leap years were first introduced,
     * so will not be accurate for ancient dates.
     *
     * @param  year The year.
     *
     * @return <tt>true</tt> if the year is a leap year.
     */
    private static boolean isLeapYear(int year) {
        if ((year & 3) != 0) {
            return false;
        }

        if ((year % 100) != 0) {
            return true;
        }

        return (year % 400) == 0;
    }

    /**
     * Given a month, numbered from 1 to 12, calculates the number of milliseconds to the start of that month in a year.
     *
     * @param  month    The month.
     * @param  leapYear <tt>true</tt> if the year is a leap year.
     *
     * @return The number of milliseconds to the start of the month.
     */
    private static long millisToStartOfMonth(int month, boolean leapYear) {
        return (long) (leapYear ? LEAP_DAYS_IN_YEAR_PRIOR_TO_MONTH[month - 1]
                : USUAL_DAYS_IN_YEAR_PRIOR_TO_MONTH[month - 1]) * MILLIS_PER_DAY;
    }

    /**
     * Calculates the number of milliseconds to the start of the specified year, taking 1970 as zero.
     *
     * @param  year The year.
     *
     * @return The number of milliseconds to the start of the specified year, taking 1970 as zero.
     */
    public static long millisToYearStart(int year) {
        // Calculate how many leap years elapsed prior to the year in question.
        int leapYears = year / 100;

        if (year < 0) {
            leapYears = ((year + 3) >> 2) - leapYears + ((leapYears + 3) >> 2) - 1;
        } else {
            leapYears = (year >> 2) - leapYears + (leapYears >> 2);

            if (isLeapYear(year)) {
                leapYears--;
            }
        }

        return ((year * 365L) + leapYears - DAYS_TO_1970) * MILLIS_PER_DAY;
    }
}

Related

  1. timestampToMicros(Timestamp timestamp)
  2. timestampToParts(long time)
  3. timestampToPrettyString(long ts)
  4. timestampToSearchString(final Timestamp timestamp)
  5. timestampToString(long time)
  6. to_timestamp(String date)
  7. toCalendar(final Timestamp ts)
  8. toCalendar(java.sql.Timestamp stamp)
  9. toCalendar(Timestamp stamp)