Java Date Difference dateDiff(int interval, Date begin, Date end)

Here you can find the source of dateDiff(int interval, Date begin, Date end)

Description

date Diff

License

Open Source License

Declaration

public static int dateDiff(int interval, Date begin, Date end) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {

    public static int dateDiff(int interval, Date begin, Date end) {
        long beginTime = begin.getTime() / 1000L;
        long endTime = end.getTime() / 1000L;
        long tmp = 0L;
        if (endTime == beginTime) {
            return 0;
        }/*from  w ww  . j a va2s  .c o m*/

        if (endTime < beginTime) {
            tmp = beginTime;
            beginTime = endTime;
            endTime = tmp;
        }

        long intervalTime = endTime - beginTime;
        long result = 0L;
        switch (interval) {
        case 1:
            result = intervalTime / 86400L;
            break;
        case 2:
            result = intervalTime / 604800L;
            break;
        case 3:
            result = intervalTime / 2678400L;
            break;
        case 4:
            result = intervalTime / 31536000L;
            break;
        case 5:
            result = intervalTime / 3600L;
            break;
        case 6:
            result = intervalTime / 60L;
            break;
        case 7:
            result = intervalTime / 1L;
        }

        if (tmp > 0L)
            result = 0L - result;

        return (int) result;
    }

    public static String getTime(int format) {
        StringBuffer cTime = new StringBuffer(10);
        Calendar time = Calendar.getInstance();
        int miltime = time.get(14);
        int second = time.get(13);
        int minute = time.get(12);
        int hour = time.get(11);
        int day = time.get(5);
        int month = time.get(2) + 1;
        int year = time.get(1);
        if (format != 14)
            if (year >= 2000)
                year -= 2000;
            else
                year -= 1900;

        if (format >= 2)
            if (format == 14)
                cTime.append(year);
            else
                cTime.append(getFormatTime(year, 2));

        if (format >= 4)
            cTime.append(getFormatTime(month, 2));
        if (format >= 6)
            cTime.append(getFormatTime(day, 2));
        if (format >= 8)
            cTime.append(getFormatTime(hour, 2));
        if (format >= 10)
            cTime.append(getFormatTime(minute, 2));
        if (format >= 12)
            cTime.append(getFormatTime(second, 2));
        if (format >= 15)
            cTime.append(getFormatTime(miltime, 3));
        return cTime.toString();
    }

    private static String getFormatTime(int time, int format) {
        StringBuffer numm = new StringBuffer();
        int length = String.valueOf(time).length();
        if (format < length)
            return null;
        for (int i = 0; i < format - length; ++i)
            numm.append("0");

        numm.append(time);
        return numm.toString().trim();
    }
}

Related

  1. dateDiff(Date date1, Date date2)
  2. dateDiff(Date fromDate, Date toDate)
  3. DateDiff(Date startDate, Date endDate)
  4. dateDiff(final Date date1, final Date date2)
  5. dateDiff(int category, Date date1, Date date2)
  6. dateDiff(java.util.Date a, java.util.Date b)
  7. dateDiff(String startDate, String endDate)
  8. dateDiff(String startTime, String endTime, String format)
  9. DateDiff(String strDateBegin, String strDateEnd, int iType)