Android Timestamp Compare compare(Timestamp t1, Timestamp t2, int what)

Here you can find the source of compare(Timestamp t1, Timestamp t2, int what)

Description

compare

Declaration

public static int compare(Timestamp t1, Timestamp t2, int what) 

Method Source Code

//package com.java2s;

import java.sql.Timestamp;

import java.util.Calendar;

public class Main {
    public static int compare(Calendar c1, Calendar c2, int what) {
        int number = 0;
        switch (what) {
        case Calendar.YEAR:
            number = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
            break;
        case Calendar.MONTH:
            int years = compare(c1, c2, Calendar.YEAR);
            number = 12 * years//from w  w  w  . j  av  a 2  s.  c  o m
                    + (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH));
            break;
        case Calendar.DATE:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        default:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        }
        return number;
    }

    public static int compare(Timestamp t1, Timestamp t2, int what) {

        Calendar c1 = Calendar.getInstance();
        c1.setTime(t1);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(t2);

        int number = 0;
        switch (what) {
        case Calendar.YEAR:
            number = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
            break;
        case Calendar.MONTH:
            int years = compare(c1, c2, Calendar.YEAR);
            number = 12 * years
                    + (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH));
            break;
        case Calendar.DATE:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        default:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        }
        return number;
    }
}

Related

  1. after(Timestamp t1, Timestamp t2)
  2. before(Timestamp t1, Timestamp t2)
  3. equals(Timestamp t1, Timestamp t2)