Example usage for java.util Calendar get

List of usage examples for java.util Calendar get

Introduction

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

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:Main.java

public static int getMonth() {
    long ts = System.currentTimeMillis();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(ts);//  ww  w .j  a  v  a2s .com
    return calendar.get(Calendar.MONTH) + 1;
}

From source file:Main.java

public static int getYesterdayMonth() {
    long ts = System.currentTimeMillis() - 24 * 3600000;
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(ts);/*from w w  w.j a v a2s . co  m*/
    return calendar.get(Calendar.MONTH) + 1;
}

From source file:Main.java

static Calendar toNearestWholeMinute(Calendar d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d.getTime());// w w  w .  j  a v a2s.c  o  m

    if (c.get(Calendar.SECOND) >= 30)
        c.add(Calendar.MINUTE, 1);

    c.set(Calendar.SECOND, 0);

    return c;
}

From source file:Main.java

public static int getYesterdayDay() {
    long ts = System.currentTimeMillis() - 24 * 3600000;
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(ts);/*from w  ww .j  a v  a2s .co  m*/
    return calendar.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static int getDay() {
    long ts = System.currentTimeMillis();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(ts);//from  ww  w. ja  v  a  2 s .co  m
    return calendar.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

/**
 * Compares only the month, day, and year of the provided calendars
 * @param date1 The calendar to compare against
 * @param date2 The calendar to compare/*from   ww  w.j  a  va2 s . co m*/
 * @return -1 if the first calendar is earlier, 1 if the second calendar is earlier, 0 otherwise
 */
public static int compareDates(Calendar date1, Calendar date2) {
    if (date1.get(Calendar.YEAR) < date2.get(Calendar.YEAR)) {
        return -1;
    } else if (date1.get(Calendar.YEAR) > date2.get(Calendar.YEAR)) {
        return 1;
    }
    // Years are equal
    if (date1.get(Calendar.MONTH) < date2.get(Calendar.MONTH)) {
        return -1;
    } else if (date1.get(Calendar.MONTH) > date2.get(Calendar.MONTH)) {
        return 1;
    }
    // Years and months are equal
    if (date1.get(Calendar.DAY_OF_MONTH) < date2.get(Calendar.DAY_OF_MONTH)) {
        return -1;
    } else if (date1.get(Calendar.DAY_OF_MONTH) > date2.get(Calendar.DAY_OF_MONTH)) {
        return 1;
    }
    return 0;
}

From source file:Main.java

/**
 *
 * @param c1/* w  ww  .  jav a 2  s. co m*/
 * @param c2
 * @return
 */
public static boolean isSameMonth(Calendar c1, Calendar c2) {
    return !(c1 == null || c2 == null)
            && (c1.get(Calendar.ERA) == c2.get(Calendar.ERA) && (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
                    && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)));
}

From source file:Main.java

public static int getYear() {
    long ts = System.currentTimeMillis();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(ts);//from  ww w  . j  a va 2s .  c o  m
    return calendar.get(Calendar.YEAR);
}

From source file:Main.java

public static Date getDateAfter(Date d, int day) {
    Calendar now = Calendar.getInstance();
    now.setTime(d);// w  w w.  j  a v  a 2 s.c o m
    now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
    return now.getTime();
}

From source file:Main.java

public static Date moveToToday(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/*from w  ww . j av a 2s .c  o m*/
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);

    Calendar result = Calendar.getInstance();
    result.set(Calendar.HOUR_OF_DAY, hour);
    result.set(Calendar.MINUTE, minute);
    return result.getTime();
}