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:org.jbpm.formModeler.core.processing.formProcessing.Functions.java

public Map getValidDays(String value) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.set(GregorianCalendar.DAY_OF_MONTH, 1);

    if (value == null || value.equals("") || value.startsWith("/")) {
        gc.set(GregorianCalendar.MONTH, 0);
    } else if (value.endsWith("/")) {
        int month = Integer.decode(value.substring(0, value.indexOf("/"))).intValue();
        gc.set(GregorianCalendar.MONTH, month);
    } else {/* w w w  . ja v a2 s .c o m*/
        SimpleDateFormat sdf = new SimpleDateFormat("MM/yyyy");
        try {
            gc.setTime(sdf.parse(value));
            gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) + 1);
        } catch (Exception e) {
            log.warn("Error parsing date " + value + " : ", e);
        }
    }

    Map days = new TreeMap();

    int month = gc.get(GregorianCalendar.MONTH);
    while (gc.get(GregorianCalendar.MONTH) == month) {
        int intValue = gc.get(GregorianCalendar.DAY_OF_MONTH);
        String key = java.lang.String.valueOf(intValue);
        if (key.length() == 1)
            key = "0" + key;
        days.put(key, key);
        gc.set(GregorianCalendar.DAY_OF_MONTH, intValue + 1);
    }

    return days;
}

From source file:op.tools.SYSCalendar.java

public static GregorianCalendar setDate2Time(GregorianCalendar source, GregorianCalendar target) {
    target.set(GregorianCalendar.DATE, source.get(GregorianCalendar.DATE));
    target.set(GregorianCalendar.MONTH, source.get(GregorianCalendar.MONTH));
    target.set(GregorianCalendar.YEAR, source.get(GregorianCalendar.YEAR));
    return (GregorianCalendar) target.clone();
}

From source file:op.tools.SYSCalendar.java

public static Date bom(Date d) {
    GregorianCalendar gc = toGC(d);
    gc.set(GregorianCalendar.DATE, 1);
    return new Date(startOfDay(new Date(gc.getTimeInMillis())));
}

From source file:org.hoteia.qalingo.app.business.job.email.EmailCleanerTasklet.java

public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    GregorianCalendar calendar = new GregorianCalendar();
    int day = calendar.get(GregorianCalendar.DAY_OF_YEAR);

    // TODO : Denis : 20130924 : add number of day configuration in database

    calendar.set(GregorianCalendar.DAY_OF_YEAR, day - 7);
    int row = emailService.deleteSendedEmail(new Timestamp(calendar.getTimeInMillis()));
    logger.debug(row + " emails deleted");
    return RepeatStatus.FINISHED;
}

From source file:org.hoteia.qalingo.app.business.job.status.ServerStatusCleanerTasklet.java

public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    GregorianCalendar calendar = new GregorianCalendar();
    int day = calendar.get(GregorianCalendar.DAY_OF_YEAR);

    // TODO : Denis : 20131209 : add number of day configuration in database ?

    calendar.set(GregorianCalendar.DAY_OF_YEAR, day - 7);
    int row = serverService.deleteSendedServerStatus(new Timestamp(calendar.getTimeInMillis()));
    logger.debug(row + " server status deleted");
    return RepeatStatus.FINISHED;
}

From source file:org.novoj.utils.datePattern.DatePatternConverter.java

/**
 * Method returns date object with normalized time. When resetTime = true, time will be set to 0:0:0.0.
 * When date respresents current day (current date) and pattern doesn't contain appropriate pattern parts for
 * hour, minute, second, milliseconds - those values will be set to current time values, otherwise they will be
 * set to initial values (ie. 0).//from w  w w .  j ava2  s .  c  om
 */
private static Date normalizeHours(Calendar nowCld, Date date, Locale locale, String pattern,
        boolean resetTime) {
    GregorianCalendar dateCld = new GregorianCalendar();
    dateCld.setTime(date);
    boolean currentYear = nowCld.get(Calendar.YEAR) == dateCld.get(Calendar.YEAR);
    boolean currentMonth = nowCld.get(Calendar.MONTH) == dateCld.get(Calendar.MONTH);
    boolean currentDay = nowCld.get(Calendar.DAY_OF_MONTH) == dateCld.get(Calendar.DAY_OF_MONTH);
    //noinspection OverlyComplexBooleanExpression
    if (!resetTime && currentYear && currentMonth && currentDay) {
        //set current time
        if (pattern.toLowerCase(locale).indexOf('h') == -1) {
            dateCld.set(Calendar.HOUR, nowCld.get(Calendar.HOUR));
        }
        if (pattern.indexOf('m') == -1) {
            dateCld.set(Calendar.MINUTE, nowCld.get(Calendar.MINUTE));
        }
        if (pattern.indexOf('s') == -1) {
            dateCld.set(Calendar.SECOND, nowCld.get(Calendar.SECOND));
        }
        if (pattern.indexOf('S') == -1) {
            dateCld.set(Calendar.MILLISECOND, nowCld.get(Calendar.MILLISECOND));
        }
    } else {
        //set zero time
        if (pattern.toLowerCase(locale).indexOf('h') == -1) {
            dateCld.set(Calendar.HOUR, 0);
        }
        if (pattern.indexOf('m') == -1) {
            dateCld.set(Calendar.MINUTE, 0);
        }
        if (pattern.indexOf('s') == -1) {
            dateCld.set(Calendar.SECOND, 0);
        }
        if (pattern.indexOf('S') == -1) {
            dateCld.set(Calendar.MILLISECOND, 0);
        }
    }
    return dateCld.getTime();
}

From source file:org.openmrs.module.kenyaui.KenyaUiUtilsTest.java

/**
 * @see KenyaUiUtils#formatDate(java.util.Date)
 * @verifies format date as a string without time information
 *//*from  www  .j a v a2 s.c  o m*/
@Test
public void formatDate_shouldFormatDateWithoutTime() throws Exception {
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 1981);
    cal.set(Calendar.MONTH, Calendar.MAY);
    cal.set(Calendar.DAY_OF_MONTH, 28);
    cal.set(Calendar.HOUR, 7);
    cal.set(Calendar.MINUTE, 30);
    cal.set(Calendar.SECOND, 12);

    Assert.assertThat(kenyaUi.formatDate(cal.getTime()), is("28-May-1981"));
}

From source file:org.openmrs.module.kenyaui.KenyaUiUtilsTest.java

/**
 * @see KenyaUiUtils#formatDateParam(java.util.Date)
 * @verifies format null date as empty string
 *///from w  w  w  .  ja  va2s .com
@Test
public void formatDateParam_shouldFormatDateAsISO8601() throws Exception {
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 1981);
    cal.set(Calendar.MONTH, Calendar.MAY);
    cal.set(Calendar.DAY_OF_MONTH, 28);
    cal.set(Calendar.HOUR, 7);
    cal.set(Calendar.MINUTE, 30);
    cal.set(Calendar.SECOND, 12);

    Assert.assertThat(kenyaUi.formatDateParam(cal.getTime()), is("1981-05-28"));
}

From source file:op.tools.SYSCalendar.java

/**
 * gibt das heutige Datum zurck, allerdings um die Uhrzeitanteile bereinigt.
 *
 * @return das ein um die Uhrzeit bereinigtes Datum.
 *///w  w w  .  j a  va2 s  .  c o m
public static GregorianCalendar today() {
    GregorianCalendar gc = new GregorianCalendar();
    gc.set(GregorianCalendar.HOUR, 0);
    gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
    gc.set(GregorianCalendar.MINUTE, 0);
    gc.set(GregorianCalendar.SECOND, 0);
    gc.set(GregorianCalendar.MILLISECOND, 0);
    return gc;
}

From source file:op.tools.SYSCalendar.java

/**
 * Setzt die Zeitkomponente eines GregorianCalendars. Das heisst ganz einfach,
 * wenn man ein Datum hat (in einem GC) und in einem anderen GC steht die Uhrzeit, dann
 * fgt diese Methode die beiden Komponenten zu einem Datum und Uhrzeit Wert zusammen
 * und gibt diesen zurck./*from   w  w  w  . ja v  a  2  s. com*/
 *
 * @param date - Datumsanteil
 * @param time - Uhrzeitanzeil
 * @return Datum und Uhrzeit kombiniert.
 */
public static GregorianCalendar addTime2Date(GregorianCalendar date, GregorianCalendar time) {
    date.set(GregorianCalendar.HOUR_OF_DAY, time.get(GregorianCalendar.HOUR_OF_DAY));
    date.set(GregorianCalendar.MINUTE, time.get(GregorianCalendar.MINUTE));
    date.set(GregorianCalendar.SECOND, time.get(GregorianCalendar.SECOND));
    date.set(GregorianCalendar.MILLISECOND, time.get(GregorianCalendar.MILLISECOND));
    return (GregorianCalendar) date.clone();
}