Java Day Count getDaysBetweenTwoDays(String dateFrom, String dateEnd)

Here you can find the source of getDaysBetweenTwoDays(String dateFrom, String dateEnd)

Description

get Days Between Two Days

License

Open Source License

Declaration

public static String getDaysBetweenTwoDays(String dateFrom, String dateEnd) 

Method Source Code


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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static String getDaysBetweenTwoDays(String dateFrom, String dateEnd) {
        Date dtFrom = null;//from w w w .  j a v a2 s .  com
        Date dtEnd = null;
        dtFrom = toDate(dateFrom, "yyyy-MM-dd");
        dtEnd = toDate(dateEnd, "yyyy-MM-dd");
        long begin = dtFrom.getTime();
        long end = dtEnd.getTime();
        long inter = end - begin;
        if (inter < 0) {
            inter = inter * (-1);
        }
        long dateMillSec = 24 * 60 * 60 * 1000;

        long dateCnt = inter / dateMillSec;

        long remainder = inter % dateMillSec;

        if (remainder != 0) {
            dateCnt++;
        }
        return String.valueOf(dateCnt);
    }

    public static Date toDate(String sDate, String sFmt) {
        Date dt = null;
        try {
            dt = new SimpleDateFormat(sFmt).parse(sDate);
        } catch (ParseException e) {
            return dt;
        }
        return dt;
    }
}

Related

  1. getDayCount(String from, String to)
  2. getDayCount(String yyyy_mm_dd_1, String yyyy_mm_dd_2)
  3. GetDays(String strStartDate, String strEndDate)
  4. getDaysBetween(Calendar d1, Calendar d2)
  5. getDaysBetween(Map params)
  6. getDaySub(String beginDate, String endDate)
  7. getDaySub(String start_time, String end_time)