Java Calendar Create getCalendarRange(Calendar date, int i)

Here you can find the source of getCalendarRange(Calendar date, int i)

Description

Get date range ("20090101", 1) => Calendar[2] = { CalendarStart( "20090101 00:00:00") , CalendarEnd("20090102 00:00:00") }

License

Apache License

Parameter

Parameter Description
date a parameter
i day range

Declaration

public static Calendar[] getCalendarRange(Calendar date, int i) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**//w  w  w .j av a2 s  .  c om
     * Get date range 
     * ("20090101", 1) => Calendar[2] = { CalendarStart(
     * "20090101 00:00:00") , CalendarEnd("20090102 00:00:00") }
     *
     * @param date
     *            
     * @param i day range
     * @return
     */
    public static Calendar[] getCalendarRange(Calendar date, int i) {
        if (null == date) {
            return null;
        }
        Calendar dateStart = getFirstMinuteOfDay(date);

        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(date.getTime());
        dateEnd.add(Calendar.DAY_OF_WEEK, i);
        dateEnd = getFirstMinuteOfDay(dateEnd);

        Calendar[] range = { dateStart, dateEnd };
        return range;
    }

    /**
     * 
     * Get date range
     * @return
     */
    public static Calendar[] getCalendarRange(Calendar date, int field, int amount) {
        if (null == date) {
            return null;
        }
        Calendar dateStart = getFirstMinuteOfDay(date);
        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(date.getTime());
        dateEnd.add(field, amount);
        dateEnd = getFirstMinuteOfDay(dateEnd);

        Calendar[] range = { dateStart, dateEnd };
        return range;
    }

    /**
     * 
     * Get Calendar (am 00:00)
     * @param Calendar
     * @return Calendar
     */
    public static Calendar getFirstMinuteOfDay(Calendar dt) {

        if (null == dt) {
            return null;
        }

        Calendar calendar = dt;

        Calendar rtnCalendar = Calendar.getInstance();
        rtnCalendar.clear();
        rtnCalendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        rtnCalendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        rtnCalendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));

        return rtnCalendar;
    }

    /**
     * 
     * Get Date (am 00:00)
     * @param Date
     * @return Date
     */
    public static Date getFirstMinuteOfDay(Date dt) {

        if (null == dt) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);

        Calendar rtnCalendar = Calendar.getInstance();
        rtnCalendar.clear();
        rtnCalendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        rtnCalendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        rtnCalendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));

        return rtnCalendar.getTime();
    }
}

Related

  1. getCalendarFromSystemTimeInMillis(long date)
  2. getCalendarGMT()
  3. getCalendarInstance()
  4. getCalendarInstance()
  5. getCalendarOf(int slot)
  6. getCalendarTime(long timestamp)
  7. getCalendarTodayZero(Calendar today)
  8. getCalendarTypeForString(String oneChar)
  9. getLocalCalendar()