Java Date Compare compareDate(String sFirstDate, String sSecondDate)

Here you can find the source of compareDate(String sFirstDate, String sSecondDate)

Description

compare Date

License

Apache License

Declaration

public static long compareDate(String sFirstDate, String sSecondDate) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

public class Main {

    public static long compareDate(String sFirstDate, String sSecondDate) {
        long time1 = convertDateTimeToSysTime(sFirstDate);
        long time2 = convertDateTimeToSysTime(sSecondDate);
        long nTime = (time1 - time2);
        final long nConst = (24 * 60 * 60 * 1000);
        if (nTime % nConst == 0) {
            return nTime / nConst;
        }//from w w  w . j a  v a  2  s .c o m

        return (nTime / nConst) + 1;
    }

    public static long convertDateTimeToSysTime(String sDateTime) {
        int nYear = Integer.parseInt(sDateTime.substring(0, 4));
        int nMonth = Integer.parseInt(sDateTime.substring(4, 6));
        int nDay = Integer.parseInt(sDateTime.substring(6, 8));
        int nHour = 0, nMinute = 0, nSecond = 0;
        if (sDateTime.length() == 14) {
            nHour = Integer.parseInt(sDateTime.substring(8, 10));
            nMinute = Integer.parseInt(sDateTime.substring(10, 12));
            nSecond = Integer.parseInt(sDateTime.substring(12, 14));
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(nYear, nMonth - 1, nDay, nHour, nMinute, nSecond);
        return calendar.getTime().getTime();
    }
}

Related

  1. compareDate(String date1, String date2)
  2. compareDate(String DATE1, String DATE2)
  3. compareDate(String s, String e)
  4. compareDate(String s, String e)
  5. compareDate(String sDate, String eDate, String formatStr)
  6. compareDate(String startdate, String enddate)
  7. compareDate(String startDate, String endDate, String strFormat)
  8. compareDate(String strDate1, String strDate2, String format)
  9. compareDate2(String begingDate, String endDate, String format)