Here you can find the source of compareSecond(java.util.Date date1, java.util.Date date2)
public static int compareSecond(java.util.Date date1, java.util.Date date2)
//package com.java2s; import java.util.Calendar; public class Main { public static int compareSecond(String date1str, String date2str) { if (date1str == null || date2str == null) return 0; java.util.Date date1 = toDate(date1str); java.util.Date date2 = toDate(date2str); return compareSecond(date1, date2); }/*from w w w. j a v a2s . co m*/ public static int compareSecond(java.util.Date date1, java.util.Date date2) { if (date1 == null || date2 == null) return 0; long time1 = date1.getTime(); long time2 = date2.getTime(); long margin = time1 - time2; Long longValue = new Long(margin / (1000)); return longValue.intValue(); } public static java.util.Date toDate(String dateTime) { int index = dateTime.indexOf(" "); String date = dateTime.substring(0, index); String time = dateTime.substring(index + 1); return toDate(date, time); } public static java.util.Date toDate(String date, String time) { if (date == null || time == null) return null; int dateSlash1 = date.indexOf("-"); int dateSlash2 = date.lastIndexOf("-"); if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return null; int timeColon1 = time.indexOf(":"); int timeColon2 = time.lastIndexOf(":"); if (timeColon1 <= 0 || timeColon1 == timeColon2) return null; String year = date.substring(0, dateSlash1); String month = date.substring(dateSlash1 + 1, dateSlash2); String day = date.substring(dateSlash2 + 1); String hour = time.substring(0, timeColon1); String minute = time.substring(timeColon1 + 1, timeColon2); String second = time.substring(timeColon2 + 1); ; return toDate(year, month, day, hour, minute, second); } public static java.util.Date toDate(String yearStr, String monthStr, String dayStr, String hourStr, String minuteStr, String secondStr) { int year, month, day, hour, minute, second; try { year = Integer.parseInt(yearStr); month = Integer.parseInt(monthStr); day = Integer.parseInt(dayStr); hour = Integer.parseInt(hourStr); minute = Integer.parseInt(minuteStr); second = Integer.parseInt(secondStr); } catch (Exception e) { return null; } return toDate(year, month, day, hour, minute, second); } public static java.util.Date toDate(int year, int month, int day, int hour, int minute, int second) { Calendar calendar = Calendar.getInstance(); try { calendar.set(year, month - 1, day, hour, minute, second); } catch (Exception e) { return null; } return calendar.getTime(); } public static java.util.Date toDate(int[] dates) { if (dates == null || dates.length < 6) return null; return toCalendar(dates).getTime(); } public static Calendar toCalendar(String yearStr, String monthStr, String dayStr, String hourStr, String minuteStr, String secondStr) { int year, month, day, hour, minute, second; try { year = Integer.parseInt(yearStr); month = Integer.parseInt(monthStr); day = Integer.parseInt(dayStr); hour = Integer.parseInt(hourStr); minute = Integer.parseInt(minuteStr); second = Integer.parseInt(secondStr); } catch (Exception e) { return null; } return toCalendar(year, month, day, hour, minute, second); } public static Calendar toCalendar(String datetime) { int index = datetime.indexOf(" "); String date = datetime.substring(0, index); String time = datetime.substring(index + 1); int dateSlash1 = date.indexOf("-"); int dateSlash2 = date.lastIndexOf("-"); if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return null; int timeColon1 = time.indexOf(":"); int timeColon2 = time.lastIndexOf(":"); if (timeColon1 <= 0 || timeColon1 == timeColon2) return null; String yearStr = date.substring(0, dateSlash1); String monthStr = date.substring(dateSlash1 + 1, dateSlash2); String dayStr = date.substring(dateSlash2 + 1); String hourStr = time.substring(0, timeColon1); String minuteStr = time.substring(timeColon1 + 1, timeColon2); String secondStr = time.substring(timeColon2 + 1); ; int year, month, day, hour, minute, second; try { year = Integer.parseInt(yearStr); month = Integer.parseInt(monthStr); day = Integer.parseInt(dayStr); hour = Integer.parseInt(hourStr); minute = Integer.parseInt(minuteStr); second = Integer.parseInt(secondStr); Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, day, hour, minute, second); return calendar; } catch (Exception e) { return null; } } public static Calendar toCalendar(int[] dates) { if (dates == null || dates.length < 6) return null; return toCalendar(dates[0], dates[1], dates[2], dates[3], dates[4], dates[5]); } public static Calendar toCalendar(int year, int month, int day, int hour, int minute, int second) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DATE, day); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); return c; } }