Java Today getBetweenTodaysStartDateAndEndDate(Date startDate, Date endDate)

Here you can find the source of getBetweenTodaysStartDateAndEndDate(Date startDate, Date endDate)

Description

get Between Todays Start Date And End Date

License

Open Source License

Declaration

public static int getBetweenTodaysStartDateAndEndDate(Date startDate, Date endDate) 

Method Source Code


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

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

public class Main {
    public static int getBetweenTodaysStartDateAndEndDate(Date startDate, Date endDate) {
        int betweentoday = 0;
        if (null == startDate || null == endDate)
            return betweentoday;

        if (endDate == null) {
            Calendar calendar = Calendar.getInstance();
            String year = new Integer(calendar.get(1)).toString();
            String month = new Integer(calendar.get(2) + 1).toString();

            String day = new Integer(calendar.get(5)).toString();

            String strtodaytime = year + "-" + month + "-" + day;
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            try {
                endDate = formatter.parse(strtodaytime);
            } catch (ParseException e) {
                e.printStackTrace();/*from w w  w .j  a  v  a  2s. com*/
            }
        }

        if (endDate.after(startDate))
            betweentoday = (int) ((endDate.getTime() - startDate.getTime()) / 86400000L);
        else
            betweentoday = (int) ((startDate.getTime() - endDate.getTime()) / 86400000L);

        return betweentoday;
    }

    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. endOfToday()
  2. formatToday()
  3. formatToday()
  4. formatToDays(Date date)
  5. getAllToday()
  6. getCalendarTodayZero(Calendar today)
  7. getEndOfToday()
  8. getFirstDate(Date today)
  9. getLastCalendarOfToday()