Java Unix time unixTimestamp(int year, int month, int day, int hour, int minute, int second)

Here you can find the source of unixTimestamp(int year, int month, int day, int hour, int minute, int second)

Description

unix Timestamp

License

Apache License

Declaration

public static long unixTimestamp(int year, int month, int day,
            int hour, int minute, int second) 

Method Source Code

//package com.java2s;
/*//www .  ja  va  2 s  . co m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you 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 julian date of the epoch, 1970-01-01. */
    public static final int EPOCH_JULIAN = 2440588;
    /**
     * The number of milliseconds in a second.
     */
    public static final long MILLIS_PER_SECOND = 1000L;
    /**
     * The number of milliseconds in a minute.
     */
    public static final long MILLIS_PER_MINUTE = 60000L;
    /**
     * The number of milliseconds in an hour.
     */
    public static final long MILLIS_PER_HOUR = 3600000L;
    /**
     * The number of milliseconds in a day.
     *
     * <p>This is the modulo 'mask' used when converting
     * TIMESTAMP values to DATE and TIME values.
     */
    public static final long MILLIS_PER_DAY = 86400000;

    public static long unixTimestamp(int year, int month, int day,
            int hour, int minute, int second) {
        final int date = ymdToUnixDate(year, month, day);
        return (long) date * MILLIS_PER_DAY + (long) hour * MILLIS_PER_HOUR
                + (long) minute * MILLIS_PER_MINUTE + (long) second
                * MILLIS_PER_SECOND;
    }

    public static int ymdToUnixDate(int year, int month, int day) {
        final int julian = ymdToJulian(year, month, day);
        return julian - EPOCH_JULIAN;
    }

    public static int ymdToJulian(int year, int month, int day) {
        int a = (14 - month) / 12;
        int y = year + 4800 - a;
        int m = month + 12 * a - 3;
        int j = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y
                / 400 - 32045;
        if (j < 2299161) {
            j = day + (153 * m + 2) / 5 + 365 * y + y / 4 - 32083;
        }
        return j;
    }
}

Related

  1. getUnixTimeByDate(Date date)
  2. unix2afpTime(long ut)
  3. unixDateToString(int date)
  4. unixTime()
  5. unixtime()
  6. unixTimestampToString(long timestamp)
  7. unixtimeToDate(long time)
  8. unixTimeToString(int time)
  9. unixTimeToString(int time, int precision)