Java Date Difference dateDiff(String startDate, String endDate)

Here you can find the source of dateDiff(String startDate, String endDate)

Description

date Diff

License

Open Source License

Declaration

public static long dateDiff(String startDate, String endDate) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    public static long dateDiff(String startDate, String endDate) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar endGC = new GregorianCalendar();
        long times, days1 = 0l;
        try {//from w  ww .j a v a  2 s .co m
            times = sd.parse(endDate).getTime() - sd.parse(startDate).getTime();
            long days = times / (1000 * 24 * 60 * 60);
            days1 = (days / 7) * 5;
            long days2 = days % 7;
            endGC.setTime(sd.parse(endDate));
            int weekDay = endGC.get(Calendar.DAY_OF_WEEK);
            if (weekDay == 1) {
                days1 += days2 > 2 ? days2 - 2 : 0;
            } else if (weekDay == 7) {
                days1 += days2 > 1 ? days2 - 1 : 0;
            } else if (weekDay - 1 < days2) {
                days1 += days2 - 2;
            } else if (weekDay - 1 > days2) {
                days1 += days2;
            } else if (weekDay - 1 == days2) {
                days1 += weekDay - 1;
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return days1;
    }
}

Related

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