Example usage for java.util Calendar MONTH

List of usage examples for java.util Calendar MONTH

Introduction

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

Prototype

int MONTH

To view the source code for java.util Calendar MONTH.

Click Source Link

Document

Field number for get and set indicating the month.

Usage

From source file:Main.java

/**
 * Add date time with almost one year./*from  w w w.  jav  a  2  s.  co  m*/
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithAlmostOneYear(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.MONTH, isPast ? -12 : 12);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

/**
 * Add date time with almost two years./* w w w  .j a  v  a  2 s . c o m*/
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithAlmostTwoYears(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.MONTH, isPast ? -10 : 10);
    calendar.add(Calendar.YEAR, isPast ? -1 : 1);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

/**
 * Calculate the Julian Day for a given date using the following formula:
 * JD = 367 * Y - INT(7 * (Y + INT((M + 9)/12))/4) + INT(275 * M / 9)
 *      + D + 1721013.5 + UT/24//from  w  w  w .  java2s. c  o  m
 *
 * Note that this is only valid for the year range 1900 - 2099.
 */
public static double calculateJulianDay(Date date) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTime(date);

    double hour = cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) / 60.0f
            + cal.get(Calendar.SECOND) / 3600.0f;

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);

    double jd = 367.0 * year - Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0)
            + Math.floor(275.0 * month / 9.0) + day + 1721013.5 + hour / 24.0;
    return jd;
}

From source file:Main.java

/**
 * Get month days by year, month//w w  w.j  av a  2s .c  o m
 *
 * @param year
 * @param month
 * @return
 */
public static int getMonthDays(int year, int month) {
    Calendar a = Calendar.getInstance();
    a.set(Calendar.YEAR, year);
    a.set(Calendar.MONTH, month - 1);
    a.set(Calendar.DATE, 1);
    a.roll(Calendar.DATE, -1);
    int maxDate = a.get(Calendar.DATE);
    return maxDate;
}

From source file:Util.java

public static Date getMonday(Date today) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(today);/*from   w w  w.  j ava  2s  . c om*/

    int dow = cal.get(Calendar.DAY_OF_WEEK);

    while (dow != Calendar.MONDAY) {
        int date = cal.get(Calendar.DATE);

        if (date == 1) {
            int month = cal.get(Calendar.MONTH);

            if (month == Calendar.JANUARY) {
                month = Calendar.DECEMBER;

                cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - 1);
            } else {
                month--;
            }

            cal.set(Calendar.MONTH, month);

            date = getMonthLastDate(month, cal.get(Calendar.YEAR));
        } else {
            date--;
        }

        cal.set(Calendar.DATE, date);

        dow = cal.get(Calendar.DAY_OF_WEEK);
    }

    return cal.getTime();
}

From source file:Main.java

/** Convert a calendar date into a string as specified by http://tools.ietf.org/html/rfc822#section-5.1
 * @param date the calendar instance to convert to a string
 * @param locale the locale to use when outputting strings such as a day of the week, or month of the year.
 * @return a string in the format: "day_abbreviation, day_of_month month_abbreviation year hour:minute:second GMT"
 *///from   ww  w  . j av a 2s. c  o m
// package-private - unused
static final String convertDateToStringRfc822(Calendar date, Locale locale) {
    Date a = Date.from(Instant.now());
    a.toString();
    int day = date.get(Calendar.DAY_OF_MONTH);
    int hour = date.get(Calendar.HOUR_OF_DAY);
    int minute = date.get(Calendar.MINUTE);
    int second = date.get(Calendar.SECOND);
    String str = date.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, locale) + ", "
            + (day < 10 ? "0" + day : day) + " " + date.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale)
            + " " + date.get(Calendar.YEAR) + " " + (hour < 10 ? "0" + hour : hour) + ":"
            + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second) + " GMT";
    return str;
}

From source file:Main.java

public static Date getEndDateOfMonth(int year, int month) throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.YEAR, year);

    String enddat = year + "-" + (month + 1) + "-" + cal.getActualMaximum(Calendar.DATE);
    Date enddate = sdf.parse(enddat);

    return enddate;

}

From source file:Main.java

public static Date getStartDateOfMonth(int year, int month) throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.YEAR, year);

    String startdat = year + "-" + (month + 1) + "-01";
    Date startdate = sdf.parse(startdat);

    return startdate;

}

From source file:DateTimeUtil.java

/**
 * //from ww  w .  j a  va2  s.co  m
 * @param month
 * @param year
 * @return
 */
public static final int getlastDateofThisMonth(int month, int year) {
    Calendar calendar = Calendar.getInstance();
    if (year > -1) {
        calendar.set(Calendar.YEAR, year);
    }

    if (month > -1) {
        month--;
        calendar.set(Calendar.MONTH, month);
    }

    return calendar.getActualMaximum(Calendar.DATE);
}

From source file:Main.java

/**
 * Encode a java date/time into a 16-bit encoded DOS date
 * /*from  w ww  . j a  v a  2  s. c  o m*/
 * @param javaDateTime
 * @return long
 */
public static int encodeDate(long javaDateTime) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(javaDateTime);
    return 512 * (cal.get(Calendar.YEAR) - 1980) + 32 * (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);
}