Android Day Get getArrayDiffDays(String startDate, String endDate)

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

Description

get Array Diff Days

Declaration

public static String[] getArrayDiffDays(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.Date;
import java.util.GregorianCalendar;

public class Main {
    public static String[] getArrayDiffDays(String startDate, String endDate) {
        int LEN = 0;
        if (startDate.equals(endDate)) {
            return new String[] { startDate };
        }/*from  w  w w .  j  ava  2  s . c o m*/
        Date sdate = null;
        if (startDate.indexOf("/") > 0 && endDate.indexOf("/") > 0) {
            sdate = getDateObj(startDate, "/");
        }
        if (startDate.indexOf("-") > 0 && endDate.indexOf("-") > 0) {
            sdate = getDateObj(startDate, "-");
        }

        LEN = getDiffDays(startDate, endDate);
        String[] dateResult = new String[LEN + 1];
        dateResult[0] = startDate;
        for (int i = 1; i < LEN + 1; i++) {
            if (startDate.indexOf("/") > 0 && endDate.indexOf("/") > 0) {
                dateResult[i] = getFormatDateTime(getDateAdd(sdate, i),
                        "yyyy/MM/dd");
            }
            if (startDate.indexOf("-") > 0 && endDate.indexOf("-") > 0) {
                dateResult[i] = getFormatDateTime(getDateAdd(sdate, i),
                        "yyyy-MM-dd");
            }
        }

        return dateResult;
    }

    public static Date getDateObj(int year, int month, int day) {
        Calendar c = new GregorianCalendar();
        c.set(year, month - 1, day);
        return c.getTime();
    }

    public static Date getDateObj(String argsDate, String split) {
        String[] temp = argsDate.split(split);
        int year = Integer.valueOf(temp[0]);
        int month = Integer.valueOf(temp[1]);
        int day = Integer.valueOf(temp[2]);
        return getDateObj(year, month, day);
    }

    public static Date getDateObj() {
        Calendar c = new GregorianCalendar();
        return c.getTime();
    }

    public static int getDiffDays(String startDate, String endDate) {
        long diff = 0;
        SimpleDateFormat ft = null;
        if (startDate.indexOf("/") > 0 && endDate.indexOf("/") > 0) {
            ft = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        }
        if (startDate.indexOf("-") > 0 && endDate.indexOf("-") > 0) {
            ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        try {
            Date sDate = ft.parse(startDate + " 00:00:00");
            Date eDate = ft.parse(endDate + " 00:00:00");
            diff = eDate.getTime() - sDate.getTime();
            diff = diff / 86400000;// 1000*60*60*24;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (int) diff;

    }

    public static String getFormatDateTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Date getDateAdd(Date date, int amount) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(GregorianCalendar.DATE, amount);
        return cal.getTime();
    }
}

Related

  1. getDaySelect(String selectName, String value, boolean hasBlank)
  2. getDaySelect(String selectName, String value, boolean hasBlank, String js)
  3. getCurrentDay()
  4. getSmartDateString(long time, String extString, String zeroDayString)
  5. getWeatherDateStr(int day)
  6. getIndexDay(Date origin, int index)
  7. getDateOfLastSunday(Date d)
  8. getStartNextDay(Date d2)
  9. getNextDay(Date d2)