Example usage for java.util GregorianCalendar get

List of usage examples for java.util GregorianCalendar get

Introduction

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

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:TimeUtil.java

public static String stringSecsFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();
    StringBuffer sBuf = new StringBuffer(11);

    cal.setTime(new Date(msecs));

    int hour = cal.get(Calendar.HOUR);

    if (hour == 0)
        hour = 12;/*w ww  .jav a2 s .c o m*/

    if (hour < 10)
        sBuf.append(" ");

    sBuf.append(Integer.toString(hour));
    sBuf.append(":");

    int minute = cal.get(Calendar.MINUTE);

    if (minute < 10)
        sBuf.append("0");

    sBuf.append(Integer.toString(minute));
    sBuf.append(":");

    int secs = cal.get(Calendar.SECOND);

    if (secs < 10) {
        sBuf.append("0");
    }
    sBuf.append(Integer.toString(secs));

    sBuf.append(" ");
    sBuf.append(cal.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM");

    return (sBuf.toString());
}

From source file:Main.java

/**
 * Method to check if a date is this week. Note that it checks if it is this
 * week and not 7 days ahead!/* w  ww.  j  ava  2  s .  c  o m*/
 * 
 * @param theDate
 *            the date to see if it is this week
 * @return true if it is this week
 */
public static boolean isThisWeek(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time
    Calendar tmpDate = Calendar.getInstance();

    return tmpDate.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
            && tmpDate.get(Calendar.WEEK_OF_YEAR) == theCalendar.get(Calendar.WEEK_OF_YEAR);
}

From source file:au.com.jwatmuff.eventmanager.model.misc.PoolChecker.java

public static int calculateAge(Date dob, Date censusDate) {
    if (dob == null)
        return -1;

    /* calculate age */
    GregorianCalendar birthCal = new GregorianCalendar();
    birthCal.setTime(dob);// ww  w .jav a 2  s. c  o  m
    int birthYear = birthCal.get(GregorianCalendar.YEAR);

    GregorianCalendar censusCal = new GregorianCalendar();
    censusCal.setTime(censusDate);
    int censusYear = censusCal.get(GregorianCalendar.YEAR);

    int age = censusYear - birthYear;

    birthCal.set(GregorianCalendar.YEAR, censusYear);
    if (censusCal.before(birthCal)) {
        age--;
    }

    return age;
}

From source file:name.martingeisse.common.util.Month.java

/**
 * @param c a {@link GregorianCalendar} that contains the month to return
 * @return the corresponding month/* w  w w.  ja v  a  2s. c  o  m*/
 */
public static Month from(GregorianCalendar c) {
    return fromGregorianCalendarValue(c.get(Calendar.MONTH));
}

From source file:com.labs64.utils.swid.support.JAXBUtils.java

/**
 * Convert {@link Date} to {@link XMLGregorianCalendar}.
 * /*w ww .j  av  a 2s  .c  o m*/
 * @param date
 *            XML entity
 */
public static XMLGregorianCalendar convertDateToXMLGregorianCalendar(final Date date) {
    try {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        XMLGregorianCalendar calXml = DatatypeFactory.newInstance().newXMLGregorianCalendar(
                cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH),
                cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND),
                cal.get(Calendar.MILLISECOND), 0);
        return calXml;
    } catch (DatatypeConfigurationException e) {
        throw new SwidException("Cannot convert date", e);
    }
}

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

/** takes current date and converts it to something like 20130709 for July 9 2013 */
private static String dateToString(Date d) {
    GregorianCalendar c = new GregorianCalendar();
    c.setTime(d);//from  ww  w  .j a  v a  2 s .  c o  m
    int yyyy = c.get(Calendar.YEAR);
    int mm = c.get(Calendar.MONTH) + 1; // rememeber + 1 adj cos calendar is 0 based
    int dd = c.get(Calendar.DATE);
    return String.format("%04d", yyyy) + String.format("%02d", mm) + String.format("%02d", dd);
}

From source file:com.projity.util.DateTime.java

/**
 * Get an integer for the date in form YYYYMMDD where the months go from 1 to 12 (unlike calendar where they go from 0 to 11)
 * @param date/*from w w  w.j  a  v a  2 s.c om*/
 * @return
 */
public static int currentToYYMM() {
    GregorianCalendar cal = DateTime.calendarInstance();
    return (cal.get(Calendar.YEAR) % 100) * 100 + (1 + cal.get(Calendar.MONTH));

}

From source file:Main.java

/**
 * Method to check if a date is today. Also returns true if the date is
 * earlier than today// w  w  w  .  j av a 2 s  . c o  m
 * 
 * @param theDate
 *            the date to see if it is today
 * @return true if theDate is today or earlier
 */
public static boolean isToday(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time to compare with
    Calendar systemCalendar = Calendar.getInstance();
    // If it should return true only if today and not before use == instead
    // of >=
    return systemCalendar.get(Calendar.YEAR) >= theCalendar.get(Calendar.YEAR)
            && systemCalendar.get(Calendar.DAY_OF_YEAR) >= theCalendar.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

/**
 * Method to see if a date has already passed. the time of the day has no
 * relevance./*from  ww w.  j av  a 2 s .c  o m*/
 * 
 * @param theDate
 *            the date to see if it has passed
 * @return true if the date has passed
 */
public static boolean dateHasPassed(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time to compare with
    Calendar systemCalendar = Calendar.getInstance();
    // If it should return true only if today and not before use == instead
    // of >=
    return systemCalendar.get(Calendar.YEAR) > theCalendar.get(Calendar.YEAR)
            || (systemCalendar.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
                    && systemCalendar.get(Calendar.DAY_OF_YEAR) > theCalendar.get(Calendar.DAY_OF_YEAR));
}

From source file:org.mifos.calendar.CalendarUtils.java

/**
 * for monthly on date return the next date falling on the same day. If date has passed, pass in the date of next
 * month, adjust to day number if day number exceed total number of days in month
 *//*from   w ww .  j  a va 2  s  .  c  om*/
public static DateTime getFirstDateForMonthOnDate(final DateTime startDate, final int dayOfMonth) {

    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate.toDate());

    int dt = gc.get(GregorianCalendar.DATE);
    // if date passed in, is after the date on which schedule has to
    // lie, move to next month
    if (dt > dayOfMonth) {
        gc.add(GregorianCalendar.MONTH, 1);
    }
    // set the date on which schedule has to lie
    int M1 = gc.get(GregorianCalendar.MONTH);
    gc.set(GregorianCalendar.DATE, dayOfMonth);
    int M2 = gc.get(GregorianCalendar.MONTH);

    int daynum = dayOfMonth;
    while (M1 != M2) {
        gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) - 1);
        gc.set(GregorianCalendar.DATE, daynum - 1);
        M2 = gc.get(GregorianCalendar.MONTH);
        daynum--;
    }

    return new DateTime(gc.getTime());
}