Java Timestamp Create timestampStringToUnixDate(String s)

Here you can find the source of timestampStringToUnixDate(String s)

Description

timestamp String To Unix Date

License

Apache License

Declaration

public static long timestampStringToUnixDate(String s) 

Method Source Code

//package com.java2s;
/*/*ww w .  ja va  2s . 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 timestampStringToUnixDate(String s) {
        final long d;
        final long t;
        s = s.trim();
        int space = s.indexOf(' ');
        if (space >= 0) {
            d = dateStringToUnixDate(s.substring(0, space));
            t = timeStringToUnixDate(s, space + 1);
        } else {
            d = dateStringToUnixDate(s);
            t = 0;
        }
        return d * MILLIS_PER_DAY + t;
    }

    public static int dateStringToUnixDate(String s) {
        int hyphen1 = s.indexOf('-');
        int y;
        int m;
        int d;
        if (hyphen1 < 0) {
            y = Integer.parseInt(s.trim());
            m = 1;
            d = 1;
        } else {
            y = Integer.parseInt(s.substring(0, hyphen1).trim());
            final int hyphen2 = s.indexOf('-', hyphen1 + 1);
            if (hyphen2 < 0) {
                m = Integer.parseInt(s.substring(hyphen1 + 1).trim());
                d = 1;
            } else {
                m = Integer.parseInt(s.substring(hyphen1 + 1, hyphen2).trim());
                d = Integer.parseInt(s.substring(hyphen2 + 1).trim());
            }
        }
        return ymdToUnixDate(y, m, d);
    }

    public static int timeStringToUnixDate(String v) {
        return timeStringToUnixDate(v, 0);
    }

    public static int timeStringToUnixDate(String v, int start) {
        final int colon1 = v.indexOf(':', start);
        int hour;
        int minute;
        int second;
        int milli;
        if (colon1 < 0) {
            hour = Integer.parseInt(v.trim());
            minute = 1;
            second = 1;
            milli = 0;
        } else {
            hour = Integer.parseInt(v.substring(start, colon1).trim());
            final int colon2 = v.indexOf(':', colon1 + 1);
            if (colon2 < 0) {
                minute = Integer.parseInt(v.substring(colon1 + 1).trim());
                second = 1;
                milli = 0;
            } else {
                minute = Integer.parseInt(v.substring(colon1 + 1, colon2).trim());
                int dot = v.indexOf('.', colon2);
                if (dot < 0) {
                    second = Integer.parseInt(v.substring(colon2 + 1).trim());
                    milli = 0;
                } else {
                    second = Integer.parseInt(v.substring(colon2 + 1, dot).trim());
                    milli = Integer.parseInt(v.substring(dot + 1).trim());
                }
            }
        }
        return hour * (int) MILLIS_PER_HOUR + minute * (int) MILLIS_PER_MINUTE + second * (int) MILLIS_PER_SECOND
                + milli;
    }

    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. timestampDifference(long ts1, long ts2)
  2. timestampMicros()
  3. timestampNonce(final int length)
  4. timestampNow()
  5. timestampStartOfDay(long time)
  6. toOracleTimestamp(String stringDate, int intFlag)
  7. toSQLTime(Instant timestamp)
  8. toSqlTime(Timestamp timestamp)
  9. toSqlTimestamp(Date d)