Java Date Compare compareDate(Date date1, Date date2)

Here you can find the source of compareDate(Date date1, Date date2)

Description

compare Date

License

Open Source License

Declaration

public static String compareDate(Date date1, Date date2) 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {

    public static String compareDate(String yyyyMMddHHmmss1, String yyyyMMddHHmmss2) {
        long d1 = getMillins(yyyyMMddHHmmss1);
        long d2 = getMillins(yyyyMMddHHmmss2);
        long tmp = (d1 > d2) ? (d1 - d2) : (d2 - d1);
        int day = 0, hour = 0, min = 0, sec = 0;
        for (; tmp >= 24 * 60 * 60 * 1000; tmp -= 24 * 60 * 60 * 1000, day++)
            ;/*from ww  w .  j a  v a2s. c  om*/
        for (; tmp >= 60 * 60 * 1000; tmp -= 60 * 60 * 1000, hour++)
            ;
        for (; tmp >= 60 * 1000; tmp -= 60 * 1000, min++)
            ;
        for (; tmp >= 60; tmp -= 60, sec++)
            ;
        return String.valueOf(day) + "," + String.valueOf(hour) + "," + String.valueOf(min) + ","
                + String.valueOf(sec) + "," + String.valueOf(tmp);
    }

    public static String compareDate(Date date1, Date date2) {
        if (date1 == null || date2 == null)
            return null;
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date1);
        long d1 = c.getTimeInMillis();
        c.setTime(date2);
        long d2 = c.getTimeInMillis();
        long tmp = (d1 > d2) ? (d1 - d2) : (d2 - d1);
        int day = 0, hour = 0, min = 0, sec = 0;
        for (; tmp >= 24 * 60 * 60 * 1000; tmp -= 24 * 60 * 60 * 1000, day++)
            ;
        for (; tmp >= 60 * 60 * 1000; tmp -= 60 * 60 * 1000, hour++)
            ;
        for (; tmp >= 60 * 1000; tmp -= 60 * 1000, min++)
            ;
        for (; tmp >= 60; tmp -= 60, sec++)
            ;
        return String.valueOf(day) + "," + String.valueOf(hour) + "," + String.valueOf(min) + ","
                + String.valueOf(sec) + "," + String.valueOf(tmp);
    }

    public static long getMillins(String yyyyMMddHHmmss) {
        long d = 0;
        try {
            java.util.Date date = new java.util.Date();
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyyMMddhhmmss");
            date = formatter.parse(yyyyMMddHHmmss);
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(date);
            d = c.getTimeInMillis();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }

    public static String getMillins(long yyyyMMddHHmmss) {
        String d = new String();
        try {
            java.util.Date date = new java.util.Date();
            date.setTime(yyyyMMddHHmmss);
            d = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }
}

Related

  1. compareDate(Date d1, Date d2)
  2. CompareDate(Date date, Date otherdate)
  3. compareDate(Date date1)
  4. compareDate(Date date1, Date date2)
  5. compareDate(Date DATE1, Date DATE2)
  6. compareDate(Date date1, Date date2)
  7. compareDate(Date date1, Date date2)
  8. compareDate(Date date1, Date date2)
  9. compareDate(Date date1, Date date2)