Java Date Compare Compare(Date dtA, Date dtB, boolean bUnkIsPast)

Here you can find the source of Compare(Date dtA, Date dtB, boolean bUnkIsPast)

Description

Compare

License

Open Source License

Declaration

public static int Compare(Date dtA, Date dtB, boolean bUnkIsPast) 

Method Source Code

//package com.java2s;
/*//from w  ww.jav  a 2 s . c om
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
    
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
    
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

import java.util.*;

public class Main {
    private static final Date m_dtInvMin = new Date(2999, 12, 27, 23, 59, 59);
    private static final Date m_dtInvMax = new Date(2999, 12, 29, 23, 59, 59);

    public static int Compare(Date dtA, Date dtB, boolean bUnkIsPast) {
        if (bUnkIsPast) {
            // 2999-12-28 23:59:59 in KeePass 1.x means 'unknown';
            // expect time zone corruption (twice)
            // boolean bInvA = ((dtA.Year == 2999) && (dtA.Month == 12) &&
            //   (dtA.Day >= 27) && (dtA.Day <= 29) && (dtA.Minute == 59) &&
            //   (dtA.Second == 59));
            // boolean bInvB = ((dtB.Year == 2999) && (dtB.Month == 12) &&
            //   (dtB.Day >= 27) && (dtB.Day <= 29) && (dtB.Minute == 59) &&
            //   (dtB.Second == 59));
            // Faster due to internal implementation of Date:
            boolean bInvA = ((dtA.getTime() >= m_dtInvMin.getTime()) && (dtA.getTime() <= m_dtInvMax.getTime())
                    && (dtA.getMinutes() == 59) && (dtA.getSeconds() == 59));
            boolean bInvB = ((dtB.getTime() >= m_dtInvMin.getTime()) && (dtB.getTime() <= m_dtInvMax.getTime())
                    && (dtB.getMinutes() == 59) && (dtB.getSeconds() == 59));

            if (bInvA)
                return (bInvB ? 0 : -1);
            if (bInvB)
                return 1;
        }

        return dtA.compareTo(dtB);
    }
}

Related

  1. compare(Date d1, Date d2)
  2. compare(Date d1, Date d2)
  3. compare(Date d1, Date d2)
  4. compare(Date date, Date fromDate, Date toDate)
  5. compare(Date date1, Date date2, String format)
  6. compare(Date start, Date end)
  7. compare(Date start, String end)
  8. compare(Date startDate, Date endDate, Date targetDate)
  9. compare(final Date lhs, final Date rhs)