Example usage for java.util Calendar getMaximum

List of usage examples for java.util Calendar getMaximum

Introduction

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

Prototype

public abstract int getMaximum(int field);

Source Link

Document

Returns the maximum value for the given calendar field of this Calendar instance.

Usage

From source file:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    // return the maximum value for days
    int max = cal.getMaximum(Calendar.DAY_OF_WEEK);
    System.out.println("Maximum days  :" + max);

    // return the maximum value for months
    max = cal.getMaximum(Calendar.MONTH);
    System.out.println("Maximum months  :" + max);
}

From source file:Main.java

public static Calendar dataFimMesAtual() {
    Calendar data = Calendar.getInstance();
    data.set(Calendar.DAY_OF_MONTH, data.getMaximum(Calendar.DAY_OF_MONTH));
    return data;//from ww w .  j a v a  2  s .  c o  m
}

From source file:Main.java

public static int getItemsCounts(int periodType, long date) {
    switch (periodType) {
    case TYPE_DAY:
        return 24;

    case TYPE_WEEK:
        return 7;

    case TYPE_MONTH: {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date);//from w ww.  java 2 s .  c om
        return c.getMaximum(Calendar.DAY_OF_MONTH);
    }

    case TYPE_YEAR: {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date);
        return c.getMaximum(Calendar.MONTH) + 1;
    }
    }

    return 0;
}

From source file:Main.java

/**
 * Calculates how many months are in given period. If one of the dates only partially covers the month, it still counts as full month.
 *
 * @param start Start of the period./*from w  ww .ja  v a2 s.c o m*/
 * @param end   End of the period.
 * @return Number of days in given period. If {@code end < start}, returns -1.
 */
public static int getMonthCountInPeriod(long start, long end) {
    if (end < start)
        return -1;

    final Calendar cal = Calendar.getInstance();
    final int monthCountInYear = cal.getMaximum(Calendar.MONTH);

    cal.setTimeInMillis(start);
    final int startYear = cal.get(Calendar.YEAR);
    final int startMonth = cal.get(Calendar.MONTH) + 1;

    cal.setTimeInMillis(end);
    final int endYear = cal.get(Calendar.YEAR);
    final int endMonth = cal.get(Calendar.MONTH) + 1;

    int monthsCount;

    if (startYear != endYear) {
        monthsCount = monthCountInYear * Math.max(0, endYear - startYear - 1);
        monthsCount += monthCountInYear - startMonth + 1;
        monthsCount += endMonth;
    } else {
        monthsCount = endMonth - startMonth + 1;
    }

    return monthsCount;
}

From source file:Main.java

private static void setEndOfMonthToCalendar(Calendar c) {
    // Get the First Day of next month so it will e considered until 0 hours
    // of next month
    int max = c.getMaximum(Calendar.DAY_OF_MONTH);
    c.set(Calendar.DAY_OF_MONTH, max);
    setEndOfDayToCalendar(c);/*  w w  w . j  ava2s. co  m*/
    Log.d("La Cuenta: ", "Max of Current Month: " + DateFormat.getInstance().format(c.getTime()));
}

From source file:Utils.java

public static Date getEndOfDay(Date day, Calendar cal) {
    if (day == null)
        day = new Date();
    cal.setTime(day);/*from ww w . ja  va 2  s  .  c  o  m*/
    cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
    return cal.getTime();
}

From source file:Main.java

private static void setEndOfWeekToCalendar(Calendar c) {
    // Get First Day of next week so it will be considered until 0 hrs of
    // next week/*from   w w w  . j  a  va  2 s.c o m*/
    c.set(Calendar.DAY_OF_WEEK, c.getMaximum(Calendar.DAY_OF_WEEK));
    setEndOfDayToCalendar(c);
    Log.d("La Cuenta: ", "Max of Current Week: " + DateFormat.getInstance().format(c.getTime()));
}

From source file:Utils.java

public static Date getEndOfMinute(Date day, Calendar cal) {
    if (day == null || cal == null) {
        return day;
    }/* w w w .  ja  v  a2s.c  o m*/

    cal.setTime(day);
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
    return cal.getTime();
}

From source file:org.jahia.utils.DateUtils.java

/**
 * Returns the end of the day (23:59:59:999) for the specified date.
 * /*from  w w  w . j a v  a 2  s . co  m*/
 * @param date the date to be processed
 * @return the end of the day (23:59:59:999) for the specified date
 */
public static Calendar dayEnd(Calendar date) {
    Calendar c = (Calendar) date.clone();

    c.set(Calendar.HOUR_OF_DAY, c.getMaximum(Calendar.HOUR_OF_DAY));
    c.set(Calendar.MINUTE, c.getMaximum(Calendar.MINUTE));
    c.set(Calendar.SECOND, c.getMaximum(Calendar.SECOND));
    c.set(Calendar.MILLISECOND, c.getMaximum(Calendar.MILLISECOND));

    return c;
}

From source file:org.openbravo.erpCommon.utility.OBDateUtils.java

/**
 * Returns the last second of the day passed as argument
 * //from   w w w . ja v a  2  s.c  o m
 */
public static Date getEndOfDay(Date day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(day);
    cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    return cal.getTime();
}