Java Timestamp Compare compareTimestamp(final Timestamp d1, final Timestamp d2)

Here you can find the source of compareTimestamp(final Timestamp d1, final Timestamp d2)

Description

compare Timestamp

License

Apache License

Declaration

@SuppressWarnings("deprecation")
    public static int compareTimestamp(final Timestamp d1, final Timestamp d2) 

Method Source Code


//package com.java2s;
/*//ww w  .  ja  v a 2 s  . c  o  m
 * Copyright 2017 stanislawbartkowski@gmail.com  
 * 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.
 */

import java.sql.Timestamp;
import java.util.Date;

public class Main {
    @SuppressWarnings("deprecation")
    public static int compareTimestamp(final Timestamp d1, final Timestamp d2) {
        int res = compareDate(d1, d2);
        if (res != 0) {
            return res;
        }
        return dCompare(d1.getHours(), d2.getHours(), d1.getMinutes(), d2.getMinutes(), d1.getSeconds(),
                d2.getSeconds());
    }

    /**
     * Compare two dates (day)
     * 
     * @param d1
     *            First day
     * @param d2
     *            Second day
     * @return -1 : first earlier then second 0 : equals 1 : first greater then
     *         second
     */

    @SuppressWarnings("deprecation")
    public static int compareDate(final Date d1, final Date d2) {
        return dCompare(d1.getYear(), d2.getYear(), d1.getMonth(), d2.getMonth(), d1.getDate(), d2.getDate());
    }

    private static int dCompare(int y1, int y2, int m1, int m2, int dd1, int dd2) {
        if (y1 != y2) {
            if (y1 < y2) {
                return -1;
            }
            return 1;
        }
        if (m1 != m2) {
            if (m1 < m2) {
                return -1;
            }
            return 1;
        }
        if (dd1 != dd2) {
            if (dd1 < dd2) {
                return -1;
            }
            return 1;
        }
        return 0;
    }
}

Related

  1. compareTimestamps(Timestamp minTimestamp, Timestamp maxTimestamp)
  2. compareTimestamps(Timestamp timestamp1, Timestamp timestamp2)
  3. dateDiff(Timestamp t1, Timestamp t2, int type)
  4. dateDifference(Timestamp one, Timestamp two)