Example usage for java.util GregorianCalendar set

List of usage examples for java.util GregorianCalendar set

Introduction

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

Prototype

public void set(int field, int value) 

Source Link

Document

Sets the given calendar field to the given value.

Usage

From source file:DateUtility.java

/**
 * This utility returns the last millsecond 
 * of the month, and year, and timezone supplied as arguments.
 *
 * @param int month//from  w w  w  . ja  v a  2s  .  c  o  m
 * @param int year
 * @param TimeZone tz
 * @return long
 */
static public long getLastMilliOfMonth(int year, int month, TimeZone tz) {
    GregorianCalendar cal = new GregorianCalendar(year, (month - 1), 1);
    cal.setTimeZone(tz);

    // set the maximum last day
    int lastday = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
    cal.set(GregorianCalendar.DAY_OF_MONTH, lastday);

    // set other calendar maximums.  - we should do this programatically
    // too but i'm too lazy, and i dont think they're gonna change the gregorian
    // calendar anytime soon.. eh?
    cal.set(GregorianCalendar.HOUR_OF_DAY, 23);
    cal.set(GregorianCalendar.MINUTE, 59);
    cal.set(GregorianCalendar.SECOND, 59);
    cal.set(GregorianCalendar.MILLISECOND, 999);

    long time = cal.getTime().getTime();
    return time;
}

From source file:HSSFDateUtil.java

/**
 * Given an Excel date with either 1900 or 1904 date windowing, converts it to
 * a java.util.Date./*from  w  w w  .  jav  a  2 s .  co m*/
 * 
 * NOTE: If the default <code>TimeZone</code> in Java uses Daylight Saving
 * Time then the conversion back to an Excel date may not give the same value,
 * that is the comparison <CODE>excelDate ==
 * getExcelDate(getJavaDate(excelDate,false))</CODE> is not always true. For
 * example if default timezone is <code>Europe/Copenhagen</code>, on
 * 2004-03-28 the minute after 01:59 CET is 03:00 CEST, if the excel date
 * represents a time between 02:00 and 03:00 then it is converted to past
 * 03:00 summer time
 * 
 * @param date
 *          The Excel date.
 * @param use1904windowing
 *          true if date uses 1904 windowing, or false if using 1900 date
 *          windowing.
 * @return Java representation of the date, or null if date is not a valid
 *         Excel date
 * @see java.util.TimeZone
 */
public static Date getJavaDate(final double date, final boolean use1904windowing) {
    if (isValidExcelDate(date)) {
        int startYear = 1900;
        int dayAdjust = -1; // Excel thinks 2/29/1900 is a valid date, which it
                            // isn't
        final int wholeDays = (int) Math.floor(date);
        if (use1904windowing) {
            startYear = 1904;
            dayAdjust = 1; // 1904 date windowing uses 1/2/1904 as the first day
        } else if (wholeDays < 61) {
            // Date is prior to 3/1/1900, so adjust because Excel thinks 2/29/1900
            // exists
            // If Excel date == 2/29/1900, will become 3/1/1900 in Java
            // representation
            dayAdjust = 0;
        }
        final GregorianCalendar calendar = new GregorianCalendar(startYear, 0, wholeDays + dayAdjust);
        final int millisecondsInDay = (int) ((date - Math.floor(date)) * (double) DAY_MILLISECONDS + 0.5);
        calendar.set(GregorianCalendar.MILLISECOND, millisecondsInDay);
        return calendar.getTime();
    } else {
        return null;
    }
}

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

public static DateTime getNextDateForMonthOnDate(final DateTime startDate, final int dayOfMonth,
        final int every) {

    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate.toDate());//www  . java  2s .  c om

    gc.add(GregorianCalendar.MONTH, every);
    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().getTime());
}

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);/*from ww w. jav  a  2  s .c om*/
    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:org.openvpms.archetype.rules.util.DateRules.java

/**
 * Adds a date and time./*from  w  w  w . ja  v a 2  s  .  c  om*/
 *
 * @param date the date part
 * @param time the time to add
 * @return the date+time
 */
public static Date addDateTime(Date date, Date time) {
    GregorianCalendar dateCal = new GregorianCalendar();
    dateCal.setTime(date);
    GregorianCalendar timeCal = new GregorianCalendar();
    timeCal.setTime(time);

    dateCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
    dateCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
    dateCal.set(Calendar.SECOND, timeCal.get(Calendar.SECOND));
    return dateCal.getTime();
}

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

public static DateTime getNextDayForMonthUsingWeekRankAndWeekday(final DateTime startDate,
        final int weekOfMonth, final int dayOfWeek, final int every) {

    /*//from ww  w.j  ava 2 s  . c  om
     * In Joda time MONDAY=1 and SUNDAY=7, so shift these to SUNDAY=1, SATURDAY=7 to match this class
     */
    int calendarDayOfWeek = (dayOfWeek % 7) + 1;

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

    DateTime scheduleDate;

    if (!RankOfDay.getRankOfDay(weekOfMonth).equals(RankOfDay.LAST)) {
        // apply month recurrence
        gc.add(GregorianCalendar.MONTH, every);
        gc.set(Calendar.DAY_OF_WEEK, calendarDayOfWeek);
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, weekOfMonth);
        scheduleDate = new DateTime(gc.getTime().getTime());
    } else {// weekCount=-1
        gc.set(GregorianCalendar.DATE, 15);
        gc.add(GregorianCalendar.MONTH, every);
        gc.set(Calendar.DAY_OF_WEEK, calendarDayOfWeek);
        int M1 = gc.get(GregorianCalendar.MONTH);
        // assumption: there are 5 weekdays in the month
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 5);
        int M2 = gc.get(GregorianCalendar.MONTH);
        // if assumption fails, it means there exists 4 weekdays in a
        // month, return last weekday date
        // if M1==M2, means there exists 5 weekdays otherwise 4 weekdays
        // in a month
        if (M1 != M2) {
            gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) - 1);
            gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 4);
        }
        scheduleDate = new DateTime(gc.getTime().getTime());
    }

    return scheduleDate;
}

From source file:org.gcaldaemon.core.GCalUtilities.java

private static final DateTime toOneDayEventDateTime(Date date) throws Exception {

    // Convert one day event's date to UTC date
    String text = date.toString();
    GregorianCalendar calendar = new GregorianCalendar(UTC);
    calendar.set(GregorianCalendar.YEAR, Integer.parseInt(text.substring(0, 4)));
    calendar.set(GregorianCalendar.MONTH, Integer.parseInt(text.substring(4, 6)) - 1);
    calendar.set(GregorianCalendar.DAY_OF_MONTH, Integer.parseInt(text.substring(6)));
    calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
    calendar.set(GregorianCalendar.MINUTE, 0);
    calendar.set(GregorianCalendar.SECOND, 0);
    calendar.set(GregorianCalendar.MILLISECOND, 0);
    DateTime dateTime = new DateTime(calendar.getTime(), UTC);
    return dateTime;
}

From source file:com.apextom.util.DateUtil.java

/**
 * /*from   www .  j  a  v a2  s  .  c o m*/
 *
 * @return
 */
public static String getFirstDay(boolean isNeedHH24MISS) {
    SimpleDateFormat df = new SimpleDateFormat(YYYY_MM_DD);
    Calendar calendar = Calendar.getInstance();
    Date theDate = calendar.getTime();
    GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
    gcLast.setTime(theDate);
    gcLast.set(Calendar.DAY_OF_MONTH, 1);
    String day_first = df.format(gcLast.getTime());
    StringBuffer str = new StringBuffer().append(day_first);
    if (isNeedHH24MISS) {
        str.append(" 00:00:00");
    }
    return str.toString();
}

From source file:dao.Graficos.java

public static Date dia_inicial_final_semana(boolean isPrimeiro) {
    //Seta a data atual.
    Date dataAtual = new Date();
    GregorianCalendar calendar = new GregorianCalendar();
    //Define que a semana comea no domingo.
    calendar.setFirstDayOfWeek(Calendar.SUNDAY);
    //Define a data atual.
    calendar.setTime(dataAtual);/*w  w w. j av a2 s .c  o m*/
    if (isPrimeiro) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    } else {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    }
    return calendar.getTime();
}

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

public static DateTime getFirstDayForMonthUsingWeekRankAndWeekday(final DateTime startDate,
        final int calendarWeekOfMonth, final int dayOfWeek) {

    /*// w w w.  j  a  v  a2 s .c  o  m
     * In Joda time MONDAY=1 and SUNDAY=7, so shift these to SUNDAY=1, SATURDAY=7 to match this class
     */
    int calendarDayOfWeek = (dayOfWeek % 7) + 1;

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

    DateTime scheduleDate = null;
    // if current weekday is after the weekday on which schedule has to
    // lie, move to next week
    if (gc.get(Calendar.DAY_OF_WEEK) > calendarDayOfWeek) {
        gc.add(Calendar.WEEK_OF_MONTH, 1);
    }
    // set the weekday on which schedule has to lie
    gc.set(Calendar.DAY_OF_WEEK, calendarDayOfWeek);
    // if week rank is First, Second, Third or Fourth, Set the
    // respective week.
    // if current week rank is after the weekrank on which schedule has
    // to lie, move to next month
    if (!RankOfDay.getRankOfDay(calendarWeekOfMonth).equals(RankOfDay.LAST)) {
        if (gc.get(Calendar.DAY_OF_WEEK_IN_MONTH) > calendarWeekOfMonth) {
            gc.add(GregorianCalendar.MONTH, 1);
            gc.set(GregorianCalendar.DATE, 1);
        }
        // set the weekrank on which schedule has to lie
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, calendarWeekOfMonth);
        scheduleDate = new DateTime(gc.getTime().getTime());
    } else {// scheduleData.getWeekRank()=Last
        int M1 = gc.get(GregorianCalendar.MONTH);
        // assumption: there are 5 weekdays in the month
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 5);
        int M2 = gc.get(GregorianCalendar.MONTH);
        // if assumption fails, it means there exists 4 weekdays in a
        // month, return last weekday date
        // if M1==M2, means there exists 5 weekdays otherwise 4 weekdays
        // in a month
        if (M1 != M2) {
            gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) - 1);
            gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 4);
        }
        scheduleDate = new DateTime(gc.getTime().getTime());
    }

    return scheduleDate;
}