Example usage for java.util Calendar SECOND

List of usage examples for java.util Calendar SECOND

Introduction

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

Prototype

int SECOND

To view the source code for java.util Calendar SECOND.

Click Source Link

Document

Field number for get and set indicating the second within the minute.

Usage

From source file:gbc.jtimecalc.AbstractTimeDifferenceCalculatorTest.java

protected static Calendar prepareCalendar(int year, int month, int day, int hour, int minute, int second,
        int millisecond) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MILLISECOND, millisecond);
    calendar.set(Calendar.SECOND, second);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.DATE, day);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.YEAR, year);
    return calendar;
}

From source file:frk.gpssimulator.service.impl.DefaultGpsdService.java

@Override
public void updatePosition(PositionInfo position) {

    // an NMEA RMC position sentence (report) is of form:
    // $GPRMC,124426,A,5920.7019,N,02803.2893,E,,,121212,,

    Calendar cal = Calendar.getInstance();
    Integer hour = cal.get(Calendar.HOUR_OF_DAY);
    Integer minute = cal.get(Calendar.MINUTE);
    Integer second = cal.get(Calendar.SECOND);
    Integer date = cal.get(Calendar.DATE);
    Integer month = cal.get(Calendar.MONTH) + 1; // java Calendar month
    // starts at 0
    Integer year = cal.get(Calendar.YEAR) % 100; // convert to 2 digit year

    String sHour = String.format("%02d", hour);
    String sMinute = String.format("%02d", minute);
    String sSecond = String.format("%02d", second);
    String sDate = String.format("%02d", date);
    String sMonth = String.format("%02d", month);
    String sYear = String.format("%02d", year);

    String sentence = null;/*from  w  w  w . java  2s  . c  o m*/
    if (position == null) {
        sentence = "$GPRMC," + sHour + sMinute + sSecond + ",A,,,,,,," + sDate + sMonth + sYear + ",,";
    } else {
        double lat = position.getPosition().getLatitude();
        double lon = position.getPosition().getLongitude();

        String latSuffix = "N";
        if (lat < 0) {
            latSuffix = "S";
        }
        lat = Math.abs(lat);
        String lonSuffix = "E";
        if (lon < 0) {
            lonSuffix = "W";
        }
        lon = Math.abs(lon);

        long latDeg = (long) lat; // degree part
        double latMin = (lat - latDeg) * 60; // minute part
        long latMinWhole = (long) latMin; // whole part of minute
        Double latMinFrac = latMin - latMinWhole; // fractional part of
        // minute
        String sLatDeg = String.format("%02d", latDeg);
        String sLatMinWhole = String.format("%02d", latMinWhole);
        String sLatMinFrac = latMinFrac.toString().replace("0.", ".");
        long lonDeg = (long) lon; // degree part
        double lonMin = (lon - lonDeg) * 60; // minute part
        long lonMinWhole = (long) lonMin; // whole part of minute
        Double lonMinFrac = lonMin - lonMinWhole; // fractional part of
        // minute
        String sLonDeg = String.format("%02d", lonDeg);
        String sLonMinWhole = String.format("%02d", lonMinWhole);
        String sLonMinFrac = lonMinFrac.toString().replace("0.", ".");

        sentence = "$GPRMC," + sHour + sMinute + sSecond + ",A," + sLatDeg + sLatMinWhole + sLatMinFrac + ","
                + latSuffix + "," + sLonDeg + sLonMinWhole + sLonMinFrac + "," + lonSuffix + ",,," + sDate
                + sMonth + sYear + ",,";
    }

    try {
        pipeWriter.write(sentence + "\r\n");
        pipeWriter.flush();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsLoggingDao.java

/**
 * Get a {@link Date}, where hours, minutes, seconds and milliseconds are
 * set to 0./*ww w.j  av  a 2 s.c  o  m*/
 *
 * @return the {@link Date} .
 */
private static Date getDayEnd(final Date d) {
    final Calendar gc = new GregorianCalendar(LOGGING_TIMEZONE);
    gc.setTime(d);
    gc.set(Calendar.HOUR_OF_DAY, 23);
    gc.set(Calendar.MINUTE, 59);
    gc.set(Calendar.SECOND, 59);
    gc.set(Calendar.MILLISECOND, 0);
    return gc.getTime();
}

From source file:com.autentia.common.util.DateFormater.java

public static Date normalizeEndDate(Date date) {

    GregorianCalendar gCalendar = new GregorianCalendar();
    gCalendar.setTime(date);/* w  w w .j a v a2 s .  c om*/
    gCalendar.set(Calendar.HOUR_OF_DAY, 23);
    gCalendar.set(Calendar.MINUTE, 59);
    gCalendar.set(Calendar.SECOND, 59);
    gCalendar.set(Calendar.MILLISECOND, 999);

    return gCalendar.getTime();
}

From source file:org.openmrs.module.openconceptlab.scheduler.UpdateScheduler.java

@SuppressWarnings("unchecked")
public synchronized void schedule(Subscription subscription) {
    updateService.saveSubscription(subscription);

    if (scheduledUpdate != null) {
        scheduledUpdate.cancel(false);//from www  .  j  a v  a2  s.  c o m
    }

    if (subscription.getDays() != 0 || subscription.getHours() != 0 || subscription.getMinutes() != 0) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, subscription.getHours());
        calendar.set(Calendar.MINUTE, subscription.getMinutes());
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        scheduledUpdate = scheduler.scheduleAtFixedRate(updater, calendar.getTime(),
                subscription.getDays() * DAY_PERIOD);
    }
}

From source file:be.boyenvaesen.scheduling.HumiditySchedules.java

@Scheduled(fixedRate = 2000)
public void updateIntervalDatabases() {
    log.info("Starting database cleanup");

    Calendar c = Calendar.getInstance();
    Calendar calForCalculations = Calendar.getInstance();
    //Clean records dating till :
    c.add(Calendar.HOUR, -5);//from ww  w .  ja  va  2s  .c  om
    Date now = new Date();

    List<Humidity> allHumidities = service.getBetweenDates(c.getTime(), now);
    List<HumidityByMinute> humiditiesByMinute = service.getBetweenDatesByInterval(HumidityByMinute.class,
            c.getTime(), now);
    List<HumidityByHour> humidityByHour = service.getBetweenDatesByInterval(HumidityByHour.class, c.getTime(),
            now);

    //Group humidities by minute
    Map<Date, List<Humidity>> map = allHumidities.stream().collect(Collectors.groupingBy((t) -> {

        calForCalculations.setTime(t.getMeasured());
        calForCalculations.set(Calendar.SECOND, 0);
        calForCalculations.set(Calendar.MILLISECOND, 0);

        return calForCalculations.getTime();
    }));
    //CALCULATE MINUTES
    map.forEach((t, u) -> {
        HumidityByMinute toCalc = humiditiesByMinute.stream().filter((q) -> {
            return q.getAtTime().compareTo(t) == 0;
        }).findAny().orElse(new HumidityByMinute(t));
        float average = (float) u.stream().mapToDouble(Humidity::getPercentage).average().getAsDouble();
        toCalc.setAveragePercentage(average);
        service.saveByInterval(toCalc);
    });

    List<HumidityByMinute> newByMinute = service.getBetweenDatesByInterval(HumidityByMinute.class, c.getTime(),
            now);
    //Group HumidityByMinute By hour
    Map<Date, List<HumidityByMinute>> mapByHour = newByMinute.stream().collect(Collectors.groupingBy((t) -> {
        calForCalculations.setTime(t.getAtTime());
        calForCalculations.set(Calendar.MINUTE, 0);
        return calForCalculations.getTime();
    }));
    //CALCULATE HOURS
    mapByHour.forEach((t, u) -> {

        HumidityByHour toCalc = humidityByHour.stream().filter((q) -> {
            return q.getAtTime().compareTo(t) == 0;
        }).findAny().orElse(new HumidityByHour(t));
        float average = (float) u.stream().mapToDouble(HumidityByMinute::getAveragePercentage).average()
                .getAsDouble();
        toCalc.setAveragePercentage(average);
        service.saveByInterval(toCalc);

    });
    log.info("finished cleaning database");

}

From source file:com.ar.dev.tierra.api.dao.impl.DetalleTransferenciaDAOimpl.java

@Override
public List<DetalleTransferencia> getDaily() {
    Criteria criteria = getSession().createCriteria(DetalleTransferencia.class);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    Date fromDate = calendar.getTime();
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    Date toDate = calendar.getTime();
    criteria.add(Restrictions.between("fechaCreacion", fromDate, toDate));
    criteria.addOrder(Order.desc("idDetalleTransferencia"));
    List<DetalleTransferencia> list = criteria.list();
    return list;/*w ww.  ja  v  a2  s . c o  m*/
}

From source file:org.kuali.coeus.s2sgen.impl.datetime.S2SDateTimeServiceImpl.java

/**
 *
 * This method computes the number of months between any 2 given {@link java.sql.Date} objects
 *
 * @param dateStart starting date./*from www  .ja v  a 2 s.com*/
 * @param dateEnd end date.
 *
 * @return number of months between the start date and end date.
 */
@Override
public ScaleTwoDecimal getNumberOfMonths(java.util.Date dateStart, java.util.Date dateEnd) {
    ScaleTwoDecimal monthCount = ScaleTwoDecimal.ZERO;
    int fullMonthCount = 0;

    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    startDate.setTime(dateStart);
    endDate.setTime(dateEnd);

    startDate.clear(Calendar.HOUR);
    startDate.clear(Calendar.MINUTE);
    startDate.clear(Calendar.SECOND);
    startDate.clear(Calendar.MILLISECOND);

    endDate.clear(Calendar.HOUR);
    endDate.clear(Calendar.MINUTE);
    endDate.clear(Calendar.SECOND);
    endDate.clear(Calendar.MILLISECOND);

    if (startDate.after(endDate)) {
        return ScaleTwoDecimal.ZERO;
    }
    int startMonthDays = startDate.getActualMaximum(Calendar.DATE) - startDate.get(Calendar.DATE);
    startMonthDays++;
    int startMonthMaxDays = startDate.getActualMaximum(Calendar.DATE);
    BigDecimal startMonthFraction = BigDecimal.valueOf(startMonthDays).setScale(2, RoundingMode.HALF_UP).divide(
            BigDecimal.valueOf(startMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);

    int endMonthDays = endDate.get(Calendar.DATE);
    int endMonthMaxDays = endDate.getActualMaximum(Calendar.DATE);

    BigDecimal endMonthFraction = BigDecimal.valueOf(endMonthDays).setScale(2, RoundingMode.HALF_UP).divide(
            BigDecimal.valueOf(endMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);

    startDate.set(Calendar.DATE, 1);
    endDate.set(Calendar.DATE, 1);

    while (startDate.getTimeInMillis() < endDate.getTimeInMillis()) {
        startDate.set(Calendar.MONTH, startDate.get(Calendar.MONTH) + 1);
        fullMonthCount++;
    }
    fullMonthCount = fullMonthCount - 1;
    monthCount = monthCount.add(new ScaleTwoDecimal(fullMonthCount))
            .add(new ScaleTwoDecimal(startMonthFraction)).add(new ScaleTwoDecimal(endMonthFraction));
    return monthCount;
}

From source file:org.openmrs.module.rwandaprimarycare.AllEncountersController.java

@RequestMapping("/module/rwandaprimarycare/allEncounters")
public String listAllEncounters(@RequestParam("patientId") int patientId, ModelMap model)
        throws PrimaryCareException {

    //LK: Need to ensure that all primary care methods only throw a PrimaryCareException
    //So that errors will be directed to a touch screen error page
    try {/*from  w  w w  .  j  a va 2s .com*/
        Patient patient = Context.getPatientService().getPatient(patientId);
        SortedMap<Date, List<Encounter>> encounters = new TreeMap<Date, List<Encounter>>(
                Collections.reverseOrder());
        for (Encounter e : Context.getEncounterService().getEncountersByPatient(patient)) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(e.getEncounterDatetime());
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Date day = cal.getTime();
            List<Encounter> holder = encounters.get(day);
            if (holder == null) {
                holder = new ArrayList<Encounter>();
                encounters.put(day, holder);
            }
            holder.add(e);
        }
        model.addAttribute("patient", patient);
        model.addAttribute("encounters", encounters);
        model.addAttribute("vitalsEncounterType", PrimaryCareBusinessLogic.getVitalsEncounterType());
        model.addAttribute("registrationEncounterType",
                PrimaryCareBusinessLogic.getRegistrationEncounterType());
    } catch (Exception e) {
        throw new PrimaryCareException(e);
    }
    return "/module/rwandaprimarycare/allEncounters";
}

From source file:adalid.commons.util.TimeUtils.java

public static synchronized Date currentDate() {
    calendar.setTimeInMillis(currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return new Date(calendar.getTimeInMillis());
}