Example usage for java.util Calendar getActualMaximum

List of usage examples for java.util Calendar getActualMaximum

Introduction

In this page you can find the example usage for java.util Calendar getActualMaximum.

Prototype

public int getActualMaximum(int field) 

Source Link

Document

Returns the maximum value that the specified calendar field could have, given the time value of this Calendar.

Usage

From source file:com.project.framework.util.DateUtils.java

/**
 * ?./*from www  .  j  av a2s  .c  o m*/
 * 
 * @param date ?
 * @return Date ?
 */
public static Date getMonthLastDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int actualMaximum = calendar.getActualMaximum(Calendar.DATE);
    return setDays(date, actualMaximum);
}

From source file:net.kamhon.ieagle.util.DateUtil.java

/**
 * given date and return first date and last date of the month
 * //from   w w  w . j  a va  2  s  .c  om
 * @param date
 * @return
 */
public static Date[] getFirstAndLastDateOfMonth(Date date) {
    date = truncateTime(date);
    int last = 0;

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    cal.set(Calendar.DAY_OF_MONTH, 1);
    Date firstDate = cal.getTime();

    cal.set(Calendar.DAY_OF_MONTH, last);
    Date lastDate = cal.getTime();

    return new Date[] { firstDate, lastDate };
}

From source file:org.sigmah.server.handler.util.Handlers.java

public static Month monthFromRange(Date date1, Date date2) {

    final Calendar c1 = Calendar.getInstance();
    c1.setTime(date1);//from w w w .  java  2 s .com
    if (c1.get(Calendar.DAY_OF_MONTH) != 1) {
        return null;
    }

    final Calendar c2 = Calendar.getInstance();
    c2.setTime(date2);
    if (c2.get(Calendar.DAY_OF_MONTH) != c2.getActualMaximum(Calendar.DAY_OF_MONTH)) {
        return null;
    }

    if (c2.get(Calendar.MONTH) != c1.get(Calendar.MONTH) || c2.get(Calendar.YEAR) != c2.get(Calendar.YEAR)) {
        return null;
    }

    return new Month(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH) + 1);
}

From source file:com.xumpy.timesheets.services.implementations.JobsSrvImpl.java

public static List<? extends Jobs> addZeroDates(List<? extends Jobs> jobs, String month, JobsGroup jobsGroup)
        throws ParseException {
    SimpleDateFormat dfOnlyDay = new SimpleDateFormat("dd");
    List<Jobs> allJobsInMonth = new ArrayList<Jobs>();

    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Date startDate = df.parse("01/" + month);

    Calendar endDate = Calendar.getInstance();
    endDate.setTime(startDate);/*from ww w.ja  va2 s .  c o  m*/
    endDate.set(Calendar.DAY_OF_MONTH, endDate.getActualMaximum(Calendar.DAY_OF_MONTH));
    endDate.add(Calendar.DATE, 1);

    Calendar iteratorDate = Calendar.getInstance();
    iteratorDate.setTime(startDate);

    while (iteratorDate.before(endDate)) {
        boolean found = false;
        JobsSrvPojo jobsSrvPojo = new JobsSrvPojo();

        for (Jobs job : jobs) {
            if (df.format(job.getJobDate()).equals(df.format(iteratorDate.getTime()))) {
                found = true;
                jobsSrvPojo = new JobsSrvPojo(job);
            }
        }

        if (!found) {
            jobsSrvPojo.setJobDate(iteratorDate.getTime());
            jobsSrvPojo.setJobsGroup(new JobsGroupSrvPojo(jobsGroup));
            jobsSrvPojo.setWorkedHours("0");
            jobsSrvPojo.setWeekendDay(jobInWeekend(jobsSrvPojo));
        }
        jobsSrvPojo.setJobDay(dfOnlyDay.format(jobsSrvPojo.getJobDate()));
        allJobsInMonth.add(jobsSrvPojo);
        iteratorDate.add(Calendar.DATE, 1);
    }

    return allJobsInMonth;
}

From source file:gov.nih.nci.cabig.caaers.utils.DateUtils.java

/**
 * Will return the date representing the last day of this month
 * @param currDate//ww w  . j ava  2s.c o  m
 * @return
 */
public static Date lastDayOfThisMonth(Date currDate) {
    Calendar c = Calendar.getInstance();
    c.setTime(currDate);
    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
    return c.getTime();
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ?//from   w  ww .j a va  2 s.  c o m
 *
 * @return
 */
public static int getCurrentMonthDays() {
    Calendar cal = new GregorianCalendar();// Calendar.getInstance();
    int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    return days;
}

From source file:edu.stanford.muse.email.CalendarUtil.java

/** cool method to convert a pair of <yy, mm, dd> specs per Gregorian calendar to a date range.
 * Note: startM, endM are 0-based, startD, endD are 1-based.
 * startY/endY must be valid (>= 0), otherwise a null is returned for start / end dates.
 * Note: all months are 0-based, but days-of-month start from 1.
 * startM/endM/startD/endD can be invalid (< 0, or also > 11 in the case of months) in which case they are treated as "*".
 * i.e. put to their min values for start, or to max values for end.
 * if startM is invalid, startD is ignored and also considered invalid. likewise for endM/endD.
 * no handling of time zone, default TZ is assumed.
 * *//*from w ww. j  a v  a  2s.  c om*/
public static Pair<Date, Date> getDateRange(int startY, int startM, int startD, int endY, int endM, int endD) {
    Date startDate = null;
    // Calendar.JANUARY is 0, and Calendar.DECEMBER is 11
    if (startY >= 0) {
        // check startM
        if (startM < Calendar.JANUARY || startM > Calendar.DECEMBER) {
            // invalid startM, assign M and D to Jan 1
            startM = Calendar.JANUARY;
            startD = 1;
        } else {
            if (startD <= 0) // invalid startD
                startD = 1;
        }
        startDate = convertYYMMDDToDate(startY, startM, startD, true);
    }

    // endM/endD will be set to be BEYOND the end of the desired day/month/year.
    // e.g. if the end y/m/d params are 2001/-1/<whatever>, we want end y/m/d to correspond to 2002 1st Jan
    // and we'll compute endDate back to EOD 2001 31st Dec.
    // if the end y/m/d params are 2001/5/-1, we want y/m/d to become correspond to 2001, 1st June and we'll set endDate back to
    // EOD on 2001, 31st May.
    Date endDate = null;

    if (endY >= 0) {
        if (endM < Calendar.JANUARY || endM > Calendar.DECEMBER) {
            // invalid endM (and endD), therefore set to end
            endM = Calendar.JANUARY;
            endD = 1;
            endY++;
        } else {
            if (endD <= 0) {
                // no date provided, so just bump month.
                endD = 1;
                endM++;
                if (endM > Calendar.DECEMBER) {
                    // obviously account for rollovers. so 2001/11/-1 sets end y/m/d to 2002/0/1
                    endY++;
                    endM = Calendar.JANUARY;
                }
            } else {
                endD++;
                // bump day, but need to check if its more than the allowed days for that month. misery!
                // http://stackoverflow.com/questions/8940438/number-of-days-in-particular-month-of-particular-year
                Calendar tmp = new GregorianCalendar(endY, endM, 1);
                int maxDays = tmp.getActualMaximum(Calendar.DAY_OF_MONTH);
                if (endD > maxDays) {
                    endD = 1;
                    // bump month
                    endM++;
                    if (endM > Calendar.DECEMBER) {
                        // obviously account for rollovers. so 2001/11/-1 sets end y/m/d to 2002/0/1
                        endY++;
                        endM = Calendar.JANUARY;
                    }
                }
            }
        }
        Date beyond_end = convertYYMMDDToDate(endY, endM, endD, true);
        endDate = new Date(beyond_end.getTime() - 1001L);
    }

    log.info("date range: " + startDate + "-" + endDate);
    return new Pair<Date, Date>(startDate, endDate);
}

From source file:org.openmrs.module.clinicalsummary.rule.util.ZScoreUtils.java

/**
 * Calculate age based on the birth date and the reference date. The returned age is in month. The remaining days will be rounded to the closest one
 * month value./*  ww w . j av  a  2 s . com*/
 *
 * @param birthDate the birth date
 * @param asOfDate  the reference date to calculate the age
 * @return the age in month
 */
private static int calculateAgeInMonth(final Date birthDate, final Date asOfDate) {

    Calendar birthCalendar = Calendar.getInstance();
    birthCalendar.setTime(birthDate);

    Calendar todayCalendar = Calendar.getInstance();
    todayCalendar.setTime(asOfDate);

    int birthYear = birthCalendar.get(Calendar.YEAR);
    int todayYear = todayCalendar.get(Calendar.YEAR);

    int ageInYear = todayYear - birthYear;

    int birthMonth = birthCalendar.get(Calendar.MONTH);
    int todayMonth = todayCalendar.get(Calendar.MONTH);

    int ageInMonth = todayMonth - birthMonth;
    if (ageInMonth < 0) {
        ageInYear--;
        ageInMonth = 12 - birthMonth + todayMonth;
    }

    int birthDay = birthCalendar.get(Calendar.DATE);
    int todayDay = todayCalendar.get(Calendar.DATE);

    int ageInDay = todayDay - birthDay;
    if (ageInDay < 0) {
        ageInMonth--;

        // we need to calculate the age in day using previous month
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.set(Calendar.MONTH, todayMonth - 1);

        birthCalendar.add(Calendar.MONTH, -1);
        ageInDay = calendar.getActualMaximum(Calendar.DATE) - birthDay + todayDay;
    }

    if (ageInDay > todayCalendar.getActualMaximum(Calendar.DATE) / 2)
        ageInMonth++;

    ageInMonth = ageInMonth + (ageInYear * 12);
    return ageInMonth;
}

From source file:jp.co.nemuzuka.utils.DateTimeUtils.java

/**
 * ?????//  w  w w  .  j a  va2s .co  m
 * @param targetDate ?
 * @param cal Calendar
 * @return 
 */
public static int getLastDay(Date targetDate, Calendar cal) {
    cal.setTime(targetDate);
    return cal.getActualMaximum(Calendar.DATE);
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * /*w w w.  j a v a  2s.  c om*/
 *
 * @param date  yyyy-MM
 * @return
 */
public static int getSpecifiedMonthDays(String date) {
    Calendar cal = Calendar.getInstance();
    try {
        cal.setTime(DateUtils.parseDate(date, MONTH_FORMAT));
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return days;
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return 0;
}