Java Date Difference DateDiff(String strDateBegin, String strDateEnd, int iType)

Here you can find the source of DateDiff(String strDateBegin, String strDateEnd, int iType)

Description

Date Diff

License

Open Source License

Declaration

public static int DateDiff(String strDateBegin, String strDateEnd, int iType) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    public static int DateDiff(String strDateBegin, String strDateEnd, int iType) {
        Calendar calBegin = parseDateTime(strDateBegin);
        Calendar calEnd = parseDateTime(strDateEnd);
        long lBegin = calBegin.getTimeInMillis();
        long lEnd = calEnd.getTimeInMillis();
        int ss = (int) ((lBegin - lEnd) / 1000L);
        int min = ss / 60;
        int hour = min / 60;
        int day = hour / 24;
        if (iType == 0)
            return hour;
        if (iType == 1)
            return min;
        if (iType == 2)
            return day;
        else//from  w  w w  . ja va2 s .c o  m
            return -1;
    }

    public static Calendar parseDateTime(String baseDate) {
        Calendar cal = null;
        cal = new GregorianCalendar();
        int yy = Integer.parseInt(baseDate.substring(0, 4));
        int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1;
        int dd = Integer.parseInt(baseDate.substring(8, 10));
        int hh = 0;
        int mi = 0;
        int ss = 0;
        if (baseDate.length() > 12) {
            hh = Integer.parseInt(baseDate.substring(11, 13));
            mi = Integer.parseInt(baseDate.substring(14, 16));
            ss = Integer.parseInt(baseDate.substring(17, 19));
        }
        cal.set(yy, mm, dd, hh, mi, ss);
        return cal;
    }
}

Related

  1. dateDiff(int category, Date date1, Date date2)
  2. dateDiff(int interval, Date begin, Date end)
  3. dateDiff(java.util.Date a, java.util.Date b)
  4. dateDiff(String startDate, String endDate)
  5. dateDiff(String startTime, String endTime, String format)
  6. dateDiffer(String time1, String time2, String formatStr)
  7. dateDiffInDays(Date dateStart, Date dateEnd)
  8. dateDiffInDaysIgnoreTime(Date dateStart, Date dateEnd)
  9. dateDiffMonth(Date d1, Date d2)