Example usage for java.util Calendar MILLISECOND

List of usage examples for java.util Calendar MILLISECOND

Introduction

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

Prototype

int MILLISECOND

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

Click Source Link

Document

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

Usage

From source file:io.apiman.gateway.engine.jdbc.JdbcMetrics.java

/**
 * Process the next item in the queue.//from   w  w w.j ava2 s  . c o  m
 */
@SuppressWarnings("nls")
protected void processQueue() {
    try {
        RequestMetric metric = queue.take();
        QueryRunner run = new QueryRunner(ds);

        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("UTC"));
        cal.setTime(metric.getRequestStart());

        long rstart = cal.getTimeInMillis();
        long rend = metric.getRequestEnd().getTime();
        long duration = metric.getRequestDuration();
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        long minute = cal.getTimeInMillis();
        cal.set(Calendar.MINUTE, 0);
        long hour = cal.getTimeInMillis();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        long day = cal.getTimeInMillis();
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        long week = cal.getTimeInMillis();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        long month = cal.getTimeInMillis();
        String api_org_id = metric.getApiOrgId();
        String api_id = metric.getApiId();
        String api_version = metric.getApiVersion();
        String client_org_id = metric.getClientOrgId();
        String client_id = metric.getClientId();
        String client_version = metric.getClientVersion();
        String plan = metric.getPlanId();
        String user_id = metric.getUser();
        String rtype = null;
        if (metric.isFailure()) {
            rtype = "failure";
        } else if (metric.isError()) {
            rtype = "error";
        }
        long bytes_up = metric.getBytesUploaded();
        long bytes_down = metric.getBytesDownloaded();

        // Now insert a row for the metric.
        run.update("INSERT INTO gw_requests (" + "rstart, rend, duration, month, week, day, hour, minute, "
                + "api_org_id, api_id, api_version, " + "client_org_id, client_id, client_version, plan, "
                + "user_id, resp_type, bytes_up, bytes_down) VALUES (" + "?, ?, ?, ?, ?, ?, ?, ?," + "?, ?, ?,"
                + "?, ?, ?, ?," + "?, ?, ?, ?)", rstart, rend, duration, month, week, day, hour, minute,
                api_org_id, api_id, api_version, client_org_id, client_id, client_version, plan, user_id, rtype,
                bytes_up, bytes_down);
    } catch (InterruptedException ie) {
        // This means that the thread was stopped.
    } catch (Exception e) {
        // TODO better logging of this unlikely error
        System.err.println("Error adding metric to database:"); //$NON-NLS-1$
        e.printStackTrace();
        return;
    }
}

From source file:org.semispace.semimeter.dao.mongo.SemiMeterDaoMongo.java

@Override
public void performInsertion(final Collection<Item> items) {
    DBCollection meterCollection = mongoTemplate.getCollection("meter");
    DBCollection sumsCollection = mongoTemplate.getCollection("sums");
    for (Item item : items) {
        //some time calculations
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(item.getWhen());
        cal.set(Calendar.MILLISECOND, 0);
        long second = cal.getTimeInMillis();
        cal.set(Calendar.SECOND, 0);
        long minute = cal.getTimeInMillis();
        cal.set(Calendar.MINUTE, 0);
        long hour = cal.getTimeInMillis();
        cal.setTimeInMillis(item.getWhen());

        BasicDBObject query = new BasicDBObject();
        PathElements pathElements = MeterHit.calcPath(item.getPath(), "/");
        query.append("id", integerForCompatibilityReasonOrString(pathElements.getE4()));
        query.append("sectionId", integerForCompatibilityReasonOrString(pathElements.getE3()));
        query.append("publicationId", integerForCompatibilityReasonOrString(pathElements.getE2()));
        query.append("type", pathElements.getE1());

        StringBuilder sb = new StringBuilder();
        sb.append("{ '$inc': ");
        sb.append(" { 'day.count' : " + item.getAccessNumber() + ", ");
        sb.append("   'day.last15minutes' : " + item.getAccessNumber() + ", ");
        sb.append("   'day.last180minutes' : " + item.getAccessNumber() + ", ");
        sb.append("   'day.hours." + hour + ".count' : " + item.getAccessNumber() + ",  ");
        sb.append(//from  w  w  w . j  a  va  2s .  c o m
                "   'day.hours." + hour + ".minutes." + minute + ".count' : " + item.getAccessNumber() + "  ");
        sb.append("}}");

        DBObject update = (DBObject) JSON.parse(sb.toString());

        meterCollection.update(query, update, true, false, WriteConcern.UNACKNOWLEDGED);

        query = new BasicDBObject();
        BasicDBObject time = new BasicDBObject();
        query.append("time", time);
        time.append("ts", minute);
        time.append("year", cal.get(Calendar.YEAR));
        time.append("month", cal.get(Calendar.MONTH));
        time.append("day", cal.get(Calendar.DAY_OF_MONTH));
        time.append("hour", cal.get(Calendar.HOUR_OF_DAY));
        time.append("minute", cal.get(Calendar.MINUTE));

        sb = new StringBuilder();
        sb.append(" { '$inc': ");
        sb.append("{ 'total' : ").append(item.getAccessNumber());
        if (pathElements.getE1().equals("article")) {
            sb.append(", 'article' : ").append(item.getAccessNumber());
        } else if (pathElements.getE1().equals("album")) {
            sb.append(", 'album' : ").append(item.getAccessNumber());
        } else if (pathElements.getE1().equals("video")) {
            sb.append(", 'video' : ").append(item.getAccessNumber());
        } else {
            sb.append(", 'other' : ").append(item.getAccessNumber());
        }
        sb.append(" } }");

        update = (DBObject) JSON.parse(sb.toString());

        sumsCollection.update(query, update, true, false, WriteConcern.UNACKNOWLEDGED);

    }
}

From source file:net.netheos.pcsapi.providers.hubic.SwiftTest.java

@Test
public void testParseLastModified() {
    // Check we won't break if a header is missing :
    JSONObject json = new JSONObject();
    Date timestamp = Swift.parseLastModified(json);
    assertNull(timestamp);//from  w  ww.  j a v  a2s  . c  o m

    json.put("last_modified", "2014-02-12T16:13:49.346540"); // this is UTC
    timestamp = Swift.parseLastModified(json);
    assertNotNull(timestamp);

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTime(timestamp);
    assertEquals(2014, cal.get(Calendar.YEAR));
    assertEquals(Calendar.FEBRUARY, cal.get(Calendar.MONTH));
    assertEquals(12, cal.get(Calendar.DAY_OF_MONTH));
    assertEquals(16, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(13, cal.get(Calendar.MINUTE));
    assertEquals(49, cal.get(Calendar.SECOND));
    assertEquals(346, cal.get(Calendar.MILLISECOND));

    checkLastModified("2014-02-12T16:13:49.346540", cal.getTime());
    checkLastModified("2014-02-12T16:13:49.3460", cal.getTime());
    checkLastModified("2014-02-12T16:13:49.346", cal.getTime());
    cal.set(Calendar.MILLISECOND, 340);
    checkLastModified("2014-02-12T16:13:49.34", cal.getTime());
    cal.set(Calendar.MILLISECOND, 300);
    checkLastModified("2014-02-12T16:13:49.3", cal.getTime());
    cal.set(Calendar.MILLISECOND, 0);
    checkLastModified("2014-02-12T16:13:49.", cal.getTime());
    checkLastModified("2014-02-12T16:13:49", cal.getTime());
}

From source file:com.ancientprogramming.fixedformat4j.format.impl.TestNullableFixedFormatManagerImpl.java

private MyNullableRecord createMyNonNullRecord() {
    Calendar someDay = Calendar.getInstance();
    someDay.set(2008, 4, 14, 0, 0, 0);//w  w w . j  a v a  2s  . c o m
    someDay.set(Calendar.MILLISECOND, 0);

    MyNullableRecord myRecord = new MyNullableRecord();
    myRecord.setBooleanData(true);
    myRecord.setCharData('C');
    myRecord.setDateData(someDay.getTime());
    myRecord.setDoubleData(10.35);
    myRecord.setFloatData(20.56F);
    myRecord.setLongData(11L);
    myRecord.setIntegerData(0);
    myRecord.setStringData("");
    myRecord.setBigDecimalData(new BigDecimal(-12.012));
    myRecord.setSimpleFloatData(20.56F);
    return myRecord;
}

From source file:info.raack.appliancedetection.common.util.DateUtils.java

public Calendar getPreviousFiveMinuteIncrement(Calendar calOrig) {
    Calendar cal = (Calendar) calOrig.clone();

    // round start time to 5 minute marker to greatly simplify evaluation
    cal.add(Calendar.MILLISECOND, -1 * cal.get(Calendar.MILLISECOND));
    cal.add(Calendar.SECOND, -1 * cal.get(Calendar.SECOND));
    cal.add(Calendar.MINUTE, -1 * cal.get(Calendar.MINUTE) % 5);

    return cal;//from  ww w. ja va 2 s . c o  m
}

From source file:DateUtil.java

/**
 * Convert the time to the midnight of the currently set date.
 * The internal date is changed after this call.
 *
 * @return a reference to this DateUtil, for concatenation.
 *///from w  ww .j a  v  a  2  s .  c o m
public DateUtil toMidnight() {

    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    return this;
}

From source file:DateUtility.java

/**
 * This will retun the first millisecond of the month 
 * that contains the timestamp provided/*from   w ww  .  j  ava 2  s . co m*/
 * @param timestamp
 * @return long
 */
static public long getFirstMilliOfMonth(long timestamp, TimeZone tz) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeZone(tz);
    cal.setTime(new Date(timestamp));
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    Date date = cal.getTime();

    return date.getTime();
}

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

@Override
public List<Chart> getDineroVendedores(int idVendedor) {
    Calendar calendarInitial = Calendar.getInstance();
    Calendar calendarClosing = Calendar.getInstance();
    calendarInitial.set(Calendar.HOUR_OF_DAY, 0);
    calendarInitial.set(Calendar.MINUTE, 0);
    calendarInitial.set(Calendar.SECOND, 0);
    calendarInitial.set(Calendar.MILLISECOND, 0);
    Date fromDate = calendarInitial.getTime();
    calendarClosing.set(Calendar.HOUR_OF_DAY, 23);
    calendarClosing.set(Calendar.MINUTE, 59);
    calendarClosing.set(Calendar.SECOND, 59);
    calendarClosing.set(Calendar.MILLISECOND, 59);
    Date toDate = calendarClosing.getTime();
    int days = 0;
    List<Chart> chartVenta = new ArrayList<>();
    while (days <= 6) {
        Chart chart = new Chart();
        Criteria facturas = getSession().createCriteria(Factura.class);
        facturas.add(Restrictions.like("estado", "CONFIRMADO"));
        facturas.add(Restrictions.between("fechaCreacion", fromDate, toDate));
        Criteria vendedorFactura = facturas.createCriteria("idVendedor");
        vendedorFactura.add(Restrictions.eq("idUsuario", idVendedor));
        facturas.setProjection(Projections.sum("total"));
        BigDecimal counter = (BigDecimal) facturas.uniqueResult();
        if (counter != null) {
            chart.setValue(counter.intValue());
        } else {/*from ww w  .  ja v  a2 s . com*/
            chart.setValue(0);
        }
        chart.setDate(fromDate);
        chartVenta.add(chart);
        calendarInitial.add(Calendar.DAY_OF_MONTH, -1);
        fromDate = calendarInitial.getTime();
        calendarClosing.add(Calendar.DAY_OF_MONTH, -1);
        toDate = calendarClosing.getTime();
        days++;
    }
    return chartVenta;
}

From source file:com.sirma.itt.emf.time.ISO8601DateFormat.java

/**
 * Format date into ISO format./*from  w w w  .  j  a  va  2  s  .  c o  m*/
 * 
 * @param isoDate
 *            the date to format
 * @return the ISO formatted string
 */
public static String format(Date isoDate) {
    if (isoDate == null) {
        return null;
    }
    // Note: always serialise to Gregorian Calendar
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(isoDate);

    StringBuilder formatted = new StringBuilder(28);
    padInt(formatted, calendar.get(Calendar.YEAR), 4);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.MONTH) + 1, 2);
    formatted.append('-');
    padInt(formatted, calendar.get(Calendar.DAY_OF_MONTH), 2);
    formatted.append('T');
    padInt(formatted, calendar.get(Calendar.HOUR_OF_DAY), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.MINUTE), 2);
    formatted.append(':');
    padInt(formatted, calendar.get(Calendar.SECOND), 2);
    formatted.append('.');
    padInt(formatted, calendar.get(Calendar.MILLISECOND), 3);

    TimeZone tz = calendar.getTimeZone();
    int offset = tz.getOffset(calendar.getTimeInMillis());
    formatted.append(getTimeZonePadding(offset));

    return formatted.toString();
}

From source file:com.autentia.intra.bean.holiday.HolidayBean.java

/**
 * List holidays. Order depends on Faces parameter sort.
 *
 * @return the list of all holidays sorted by requested criterion
 *//*from  w w  w  . ja va 2 s.c  o m*/
public List<Holiday> getAll() {
    if (selectedYear != null) {
        Calendar calMin = Calendar.getInstance();
        calMin.setTime(selectedYear);
        calMin.set(Calendar.MONTH, calMin.getMinimum(Calendar.MONTH));
        calMin.set(Calendar.DAY_OF_MONTH, calMin.getMinimum(Calendar.DAY_OF_MONTH));
        calMin.set(Calendar.HOUR_OF_DAY, calMin.getMinimum(Calendar.HOUR_OF_DAY));
        calMin.set(Calendar.MINUTE, calMin.getMinimum(Calendar.MINUTE));
        calMin.set(Calendar.SECOND, calMin.getMinimum(Calendar.SECOND));
        calMin.set(Calendar.MILLISECOND, calMin.getMinimum(Calendar.MILLISECOND));

        Calendar calMax = Calendar.getInstance();
        calMax.setTime(selectedYear);
        calMax.set(Calendar.MONTH, calMax.getMaximum(Calendar.MONTH));
        calMax.set(Calendar.DAY_OF_MONTH, calMax.getMaximum(Calendar.DAY_OF_MONTH));
        calMax.set(Calendar.HOUR_OF_DAY, calMax.getMaximum(Calendar.HOUR_OF_DAY));
        calMax.set(Calendar.MINUTE, calMax.getMaximum(Calendar.MINUTE));
        calMax.set(Calendar.SECOND, calMax.getMaximum(Calendar.SECOND));
        calMax.set(Calendar.MILLISECOND, calMax.getMaximum(Calendar.MILLISECOND));

        setSearchStartDate(calMin.getTime());
        setSearchStartDateValid(true);
        setSearchEndDate(calMax.getTime());
        setSearchEndDateValid(true);
    }

    return manager.getAllEntities(search, new SortCriteria(sortColumn, sortAscending));
}