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:com.strider.datadefender.extensions.BiographicFunctions.java

/**
 * Generates random 9-digit social insurance number
 * @return String/*from w  w w  .j  a  v a2  s  .  c o m*/
 * @throws java.text.ParseException
 */
public java.sql.Date randomBirthDate() throws java.text.ParseException {
    final GregorianCalendar gc = new GregorianCalendar();
    final int year = randBetween(1900, 2016);

    gc.set(GregorianCalendar.YEAR, year);

    final int dayOfYear = randBetween(1, gc.getActualMaximum(GregorianCalendar.DAY_OF_YEAR));

    gc.set(GregorianCalendar.DAY_OF_YEAR, dayOfYear);

    final String birthDate = prependZero(gc.get(GregorianCalendar.DAY_OF_MONTH)) + "-"
            + prependZero(gc.get(GregorianCalendar.MONTH) + 1) + "-" + gc.get(GregorianCalendar.YEAR);

    log.debug("BirthDate:[" + birthDate + "]");

    final DateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
    final java.sql.Date date = new java.sql.Date(format.parse(birthDate).getTime());

    log.debug("Generated BirthDate:[" + date.toString() + "]");

    return date;
}

From source file:nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (!GBApplication.getPrefs().getBoolean("send_sunrise_sunset", false)) {
        LOG.info("won't send sunrise and sunset events (disabled in preferences)");
        return;/*from   w ww .  j  av  a  2  s  . com*/
    }
    LOG.info("will resend sunrise and sunset events");

    final GregorianCalendar dateTimeTomorrow = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    dateTimeTomorrow.set(Calendar.HOUR, 0);
    dateTimeTomorrow.set(Calendar.MINUTE, 0);
    dateTimeTomorrow.set(Calendar.SECOND, 0);
    dateTimeTomorrow.set(Calendar.MILLISECOND, 0);
    dateTimeTomorrow.add(GregorianCalendar.DAY_OF_MONTH, 1);

    /*
     * rotate ids ud reuse the id from two days ago for tomorrow, this way we will have
     * sunrise /sunset for 3 days while sending only sunrise/sunset per day
     */
    byte id_tomorrow = (byte) ((dateTimeTomorrow.getTimeInMillis() / (1000L * 60L * 60L * 24L)) % 3);

    GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, id_tomorrow);
    GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, id_tomorrow);

    Prefs prefs = GBApplication.getPrefs();

    float latitude = prefs.getFloat("location_latitude", 0);
    float longitude = prefs.getFloat("location_longitude", 0);
    LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude);

    if (ActivityCompat.checkSelfPermission(context,
            Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && prefs.getBoolean("use_updated_location_if_available", false)) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, false);
        if (provider != null) {
            Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
            if (lastKnownLocation != null) {
                latitude = (float) lastKnownLocation.getLatitude();
                longitude = (float) lastKnownLocation.getLongitude();
                LOG.info("got longitude/latitude from last known location: " + latitude + "/" + longitude);
            }
        }
    }
    GregorianCalendar[] sunriseTransitSetTomorrow = SPA.calculateSunriseTransitSet(dateTimeTomorrow, latitude,
            longitude, DeltaT.estimate(dateTimeTomorrow));

    CalendarEventSpec calendarEventSpec = new CalendarEventSpec();
    calendarEventSpec.durationInSeconds = 0;
    calendarEventSpec.description = null;

    calendarEventSpec.type = CalendarEventSpec.TYPE_SUNRISE;
    calendarEventSpec.title = "Sunrise";
    if (sunriseTransitSetTomorrow[0] != null) {
        calendarEventSpec.id = id_tomorrow;
        calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[0].getTimeInMillis() / 1000);
        GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
    }

    calendarEventSpec.type = CalendarEventSpec.TYPE_SUNSET;
    calendarEventSpec.title = "Sunset";
    if (sunriseTransitSetTomorrow[2] != null) {
        calendarEventSpec.id = id_tomorrow;
        calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[2].getTimeInMillis() / 1000);
        GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
    }
}

From source file:org.apache.torque.generated.peer.DefaultValuesFromJavaTest.java

/**
 * Checks that if CURRENT_DATE is used as default value
 * then an object is constructed with current java time.
 *
 * @throws Exception if an error occurs.
 *///w  ww .ja  v a  2s .  com
public void testCurrentDateAsJavaDefault() throws Exception {
    if (defaultAdapter instanceof MssqlAdapter || defaultAdapter instanceof MysqlAdapter) {
        log.error("testCurrentDateAsJavaDefault(): "
                + "MSSQL and Mysql do not support the CURRENT_DATE function");
        return;
    }
    GregorianCalendar currentCalendarBefore = new GregorianCalendar();
    currentCalendarBefore.set(GregorianCalendar.HOUR_OF_DAY, 0);
    currentCalendarBefore.set(GregorianCalendar.MINUTE, 0);
    currentCalendarBefore.set(GregorianCalendar.SECOND, 0);
    currentCalendarBefore.set(GregorianCalendar.MILLISECOND, 0);
    JavaDefaultValues javaDefaultValues = new JavaDefaultValues();
    GregorianCalendar currentCalendarAfter = new GregorianCalendar();
    currentCalendarAfter.set(GregorianCalendar.HOUR_OF_DAY, 0);
    currentCalendarAfter.set(GregorianCalendar.MINUTE, 0);
    currentCalendarAfter.set(GregorianCalendar.SECOND, 0);
    currentCalendarAfter.set(GregorianCalendar.MILLISECOND, 0);

    assertFalse(
            "currentDate " + javaDefaultValues.getCurrentDateValue() + " should be >= currentDateBefore "
                    + currentCalendarBefore.getTime(),
            javaDefaultValues.getCurrentDateValue().before(currentCalendarBefore.getTime()));
    assertFalse(
            "currentDate " + javaDefaultValues.getCurrentDateValue() + " should be <= currentDateAfter "
                    + currentCalendarAfter.getTime(),
            javaDefaultValues.getCurrentDateValue().after(currentCalendarAfter.getTime()));
}

From source file:bkampfbot.state.Opponent.java

public final void checkNewDay() {
    GregorianCalendar today = new GregorianCalendar();
    today.setTime(Config.getDate());/*from ww  w  . j  a  v a  2s  . c  o  m*/
    today.set(GregorianCalendar.HOUR, 0);
    today.set(GregorianCalendar.MINUTE, 0);

    if (date.before(today)) {
        date = new GregorianCalendar();

        // Set all counters to zero
        setNew();
    }
}

From source file:op.tools.SYSCalendar.java

/**
 * EndOfMonth//  www  . jav  a 2s . c  o m
 * Berechnet das Enddatum eines Monats, passend zum Parameter
 *
 * @param d Datum innerhalb des entsprechenden Monats.
 * @return Enddatum des Monats
 */
public static Date eom(Date d) {
    GregorianCalendar gc = toGC(d);
    int ieom = eom(gc);
    gc.set(GregorianCalendar.DATE, ieom);
    return new Date(endOfDay(new Date(gc.getTimeInMillis())));
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.group.BookingRoomAjaxController.java

@Override
public Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
    Map map = new HashMap<String, Object>();
    GroupMultiController.setPermissionToRequestGroupRole(map, personDao.getLoggedPerson());

    if (request.getParameter("type").compareTo("info") == 0) {
        int id = Integer.parseInt(request.getParameter("id"));
        Reservation reservation = reservationDao.getReservationById(id);
        GregorianCalendar created = new GregorianCalendar();
        created.setTime(reservation.getCreationTime());
        GregorianCalendar startTime = new GregorianCalendar();
        startTime.setTime(reservation.getStartTime());
        GregorianCalendar endTime = new GregorianCalendar();
        endTime.setTime(reservation.getEndTime());

        Map data = new HashMap<String, Object>();
        data.put("id", id);
        data.put("person", reservation.getPerson());
        data.put("created", BookingRoomUtils.getDate(created) + ", " + BookingRoomUtils.getTime(created));
        data.put("date", BookingRoomUtils.getDate(startTime));
        data.put("start", BookingRoomUtils.getHoursAndMinutes(startTime));
        data.put("end", BookingRoomUtils.getHoursAndMinutes(endTime));

        map.put("data", data);
        return map;
    }//  w w  w . ja v a  2  s .co m

    if (request.getParameter("type").compareTo("delete") == 0) {
        int id = Integer.parseInt(request.getParameter("id"));

        if (reservationDao.deleteReservation(id)) {
            map.put("status", messageSource.getMessage("bookRoom.delete.success", null,
                    RequestContextUtils.getLocale(request)));
        } else {
            map.put("status", messageSource.getMessage("bookRoom.delete.error", null,
                    RequestContextUtils.getLocale(request)));
        }

        return map;
    }

    if (request.getParameter("type").compareTo("timeline") == 0) {
        String date = request.getParameter("date") + " 00:00:00";
        log.debug("XML DATE=" + date);
        GregorianCalendar monthStart = BookingRoomUtils.getCalendar(date);
        monthStart.set(Calendar.DAY_OF_MONTH, 1);
        GregorianCalendar monthEnd = (GregorianCalendar) monthStart.clone();
        monthEnd.add(Calendar.MONTH, 1);
        monthEnd.add(Calendar.SECOND, -1);

        String xml = BookingRoomXmlUtils.formatReservationsList(
                reservationDao.getReservationsBetween(monthStart, monthEnd), personDao.getLoggedPerson());

        map.put("xmlContent", xml);
        return map;
    }

    throw new InvalidAttributeValueException(
            "Attribute '" + request.getParameter("type") + "' is not allowed!");
}

From source file:ru.apertum.qsystem.server.model.calendar.CalendarTableModel.java

/**
 *  ? ? //w ww.j  a  va2 s  .c o  m
 * @param year
 */
public void checkSaturday(int year) {
    QLog.l().logger().debug(" ? ?");
    final GregorianCalendar gc = new GregorianCalendar();
    gc.set(GregorianCalendar.YEAR, year);
    final int ye = year % 4 == 0 ? 366 : 365;
    for (int d = 1; d <= ye; d++) {
        gc.set(GregorianCalendar.DAY_OF_YEAR, d);
        if (gc.get(GregorianCalendar.DAY_OF_WEEK) == 7) {
            addDay(gc.getTime(), true);
        }
    }
    fireTableDataChanged();
}

From source file:ru.apertum.qsystem.server.model.calendar.CalendarTableModel.java

/**
 *  ? ??? //from ww  w .  j  a  v a  2  s.c om
 * @param year
 */
public void checkSunday(int year) {
    QLog.l().logger().debug(" ? ???");
    final GregorianCalendar gc = new GregorianCalendar();
    gc.set(GregorianCalendar.YEAR, year);
    final int ye = year % 4 == 0 ? 366 : 365;
    for (int d = 1; d <= ye; d++) {
        gc.set(GregorianCalendar.DAY_OF_YEAR, d);
        if (gc.get(GregorianCalendar.DAY_OF_WEEK) == 1) {
            addDay(gc.getTime(), true);
        }
    }
    fireTableDataChanged();
}

From source file:op.tools.SYSCalendar.java

/**
 * nimmt das bergebene Datum und setzt die Uhrzeitkomponente auf 23:59:59
 *
 * @param d// www. j  av  a 2 s  .c  o  m
 * @return das Ergebnis als TimeInMillis
 */
public static long endOfDay(Date d) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(d.getTime());
    gc.set(GregorianCalendar.HOUR_OF_DAY, 23);
    gc.set(GregorianCalendar.MINUTE, 59);
    gc.set(GregorianCalendar.SECOND, 59);
    gc.set(GregorianCalendar.MILLISECOND, 0);
    return gc.getTimeInMillis();
}

From source file:op.tools.SYSCalendar.java

/**
 * nimmt das bergebene Datum und setzt die Uhrzeitkomponente auf 00:00:00.
 *
 * @param d/*from   w w w .j  ava2  s  . com*/
 * @return das Ergebnis als TimeInMillis
 */
public static long startOfDay(Date d) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(d.getTime());
    gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
    gc.set(GregorianCalendar.MINUTE, 0);
    gc.set(GregorianCalendar.SECOND, 0);
    gc.set(GregorianCalendar.MILLISECOND, 0);
    return gc.getTimeInMillis();
}