Example usage for java.sql Timestamp getTime

List of usage examples for java.sql Timestamp getTime

Introduction

In this page you can find the example usage for java.sql Timestamp getTime.

Prototype

public long getTime() 

Source Link

Document

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object.

Usage

From source file:com.oltpbenchmark.benchmarks.seats.SEATSProfile.java

/**
 * /*ww w  .  j a va2  s  .co m*/
 * @param start_date
 */
public void setFlightStartDate(Timestamp start_date) {
    this.flight_start_date.setTime(start_date.getTime());
}

From source file:com.twineworks.kettle.ruby.step.execmodels.SimpleExecutionModel.java

public RubyHash createRubyInputRow(RowMetaInterface rowMeta, Object[] r) throws KettleException {

    // create a hash for the row, they are not reused on purpose, so the scripting user can safely use them to store entire rows between invocations
    RubyHash rubyRow = new RubyHash(data.runtime);

    String[] fieldNames = rowMeta.getFieldNames();
    for (int i = 0; i < fieldNames.length; i++) {

        String field = fieldNames[i];
        // null values don't need no special treatment, they'll become nil
        if (r[i] == null) {
            rubyRow.put(field, null);//from ww  w. j  av a  2s  . c  om
        } else {

            ValueMetaInterface vm = rowMeta.getValueMeta(i);

            switch (vm.getType()) {
            case ValueMetaInterface.TYPE_BOOLEAN:
                rubyRow.put(field, vm.getBoolean(r[i]));
                break;
            case ValueMetaInterface.TYPE_INTEGER:
                rubyRow.put(field, vm.getInteger(r[i]));
                break;
            case ValueMetaInterface.TYPE_STRING:
                rubyRow.put(field, vm.getString(r[i]));
                break;
            case ValueMetaInterface.TYPE_NUMBER:
                rubyRow.put(field, vm.getNumber(r[i]));
                break;
            case ValueMetaInterface.TYPE_NONE:
                rubyRow.put(field, r[i]);
                break;
            case ValueMetaInterface.TYPE_SERIALIZABLE:
                if (r[i] instanceof RubyStepMarshalledObject) {
                    Object restoredObject = getMarshal().callMethod(data.runtime.getCurrentContext(), "restore",
                            data.runtime.newString(r[i].toString()));
                    rubyRow.put(field, restoredObject);
                } else {
                    // try to put the object in there as it is.. should create a nice adapter for the java object
                    rubyRow.put(field, r[i]);
                }
                break;
            case ValueMetaInterface.TYPE_BINARY:
                // put a ruby array with bytes in there, that is expensive and should probably be avoided
                rubyRow.put(fieldNames[i],
                        data.runtime.newArrayNoCopy(JavaUtil.convertJavaArrayToRuby(data.runtime,
                                ArrayUtils.toObject((byte[]) vm.getBinary(r[i])))));

                break;

            case ValueMetaInterface.TYPE_BIGNUMBER:
                IRubyObject bigDecimalObject = getBigDecimal().callMethod(data.runtime.getCurrentContext(),
                        "new", data.runtime.newString((vm.getBigNumber(r[i])).toString()));
                rubyRow.put(field, bigDecimalObject);
                break;

            case ValueMetaInterface.TYPE_DATE:
                rubyRow.put(field, data.runtime.newTime((vm.getDate(r[i])).getTime()));
                break;

            case ValueMetaInterface.TYPE_TIMESTAMP:
                ValueMetaTimestamp vmTimestamp = (ValueMetaTimestamp) vm;
                Timestamp ts = vmTimestamp.getTimestamp(r[i]);
                RubyTime rubyTime = data.runtime.newTime(ts.getTime() / 1000 * 1000);
                rubyTime.setNSec(ts.getNanos());
                rubyRow.put(field, rubyTime);
                break;

            case ValueMetaInterface.TYPE_INET:
                ValueMetaInternetAddress vmInet = (ValueMetaInternetAddress) vm;
                InetAddress ip = vmInet.getInternetAddress(r[i]);
                IRubyObject ipObject = getIPAddr().callMethod(data.runtime.getCurrentContext(), "new",
                        data.runtime.newString(ip.getHostAddress()));
                rubyRow.put(field, ipObject);
                break;
            }

        }

    }

    return rubyRow;

}

From source file:com.ichi2.anki.PreferenceContext.java

private void initPreference(Preference pref) {
    // Load stored values from Preferences which are stored in the Collection
    if (Arrays.asList(sCollectionPreferences).contains(pref.getKey())) {
        Collection col = getCol();
        if (col != null) {
            try {
                JSONObject conf = col.getConf();
                switch (pref.getKey()) {
                case "showEstimates":
                    ((CheckBoxPreference) pref).setChecked(conf.getBoolean("estTimes"));
                    break;
                case "showProgress":
                    ((CheckBoxPreference) pref).setChecked(conf.getBoolean("dueCounts"));
                    break;
                case "learnCutoff":
                    ((NumberRangePreference) pref).setValue(conf.getInt("collapseTime") / 60);
                    break;
                case "timeLimit":
                    ((NumberRangePreference) pref).setValue(conf.getInt("timeLim") / 60);
                    break;
                case "useCurrent":
                    ((ListPreference) pref).setValueIndex(conf.optBoolean("addToCur", true) ? 0 : 1);
                    break;
                case "newSpread":
                    ((ListPreference) pref).setValueIndex(conf.getInt("newSpread"));
                    break;
                case "dayOffset":
                    Calendar calendar = new GregorianCalendar();
                    Timestamp timestamp = new Timestamp(col.getCrt() * 1000);
                    calendar.setTimeInMillis(timestamp.getTime());
                    ((SeekBarPreference) pref).setValue(calendar.get(Calendar.HOUR_OF_DAY));
                    break;
                }// w w  w  .  j av a  2s. co m
            } catch (JSONException | NumberFormatException e) {
                throw new RuntimeException();
            }
        } else {
            // Disable Col preferences if Collection closed
            pref.setEnabled(false);
        }
    } else if ("minimumCardsDueForNotification".equals(pref.getKey())) {
        updateNotificationPreference((ListPreference) pref);
    }
    // Set the value from the summary cache
    CharSequence s = pref.getSummary();
    mOriginalSumarries.put(pref.getKey(), (s != null) ? s.toString() : "");
    // Update summary

    updateSummary(pref);
}

From source file:com.aionemu.gameserver.services.BrokerService.java

private void checkExpiredItems() {
    Map<Integer, BrokerItem> asmoBrokerItems = getRaceBrokerItems(Race.ASMODIANS);
    Map<Integer, BrokerItem> elyosBrokerItems = getRaceBrokerItems(Race.ELYOS);

    Timestamp currentTime = new Timestamp(Calendar.getInstance().getTimeInMillis());

    for (BrokerItem item : asmoBrokerItems.values()) {
        if (item != null && item.getExpireTime().getTime() <= currentTime.getTime()) {
            putToSettled(Race.ASMODIANS, item, false);
            asmodianBrokerItems.remove(item.getItemUniqueId());
        }/*from w w w  .j  a v  a2  s .  com*/
    }

    for (BrokerItem item : elyosBrokerItems.values()) {
        if (item != null && item.getExpireTime().getTime() <= currentTime.getTime()) {
            putToSettled(Race.ELYOS, item, false);
            this.elyosBrokerItems.remove(item.getItemUniqueId());
        }
    }
}

From source file:uk.org.funcube.fcdw.server.extract.csv.RealTimeCsvExtractor.java

@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public void extract(long satelliteId) {

    Date currentDate = clock.currentDate();

    Timestamp latestSatelliteTime = realTimeDao.getLatestSatelliteTime(satelliteId);

    Timestamp since = new Timestamp(latestSatelliteTime.getTime() - (250 * 60 * 1000));

    List<RealTimeEntity> realTime12 = realTimeDao.getSinceSatelliteTime(satelliteId, since);

    if (realTime12.size() == 0) {
        return;/*from   w w  w .  java 2  s  .  com*/
    }

    File fileLocation = new File(System.getProperty("csv.realtime"));

    if (fileLocation.exists()) {
        fileLocation.delete();
    }

    try {
        // use FileWriter constructor that specifies open for appending
        CsvWriter csvOutput = new CsvWriter(new FileWriter(fileLocation, true), ',');

        // write out the headers
        csvOutput.write("Satellite Date/Time UTC");
        csvOutput.write("Solar Panel Voltage X mV");
        csvOutput.write("Solar Panel Voltage Y mV");
        csvOutput.write("Solar Panel Voltage Z mV");
        csvOutput.write("Total Photo Current mA");
        csvOutput.write("Battery Voltage mV");
        csvOutput.write("Total System Current mA");
        csvOutput.write("Boost Converter Temp 1 C");
        csvOutput.write("Boost Converter Temp 2 C");
        csvOutput.write("Boost Converter Temp 3 C");
        csvOutput.write("Battery Temp C");
        csvOutput.write("Sun Sensor X+");
        csvOutput.write("Sun Sensor Y+");
        csvOutput.write("Sun Sensor Z+");
        csvOutput.write("Solar Panel Temp X+ C");
        csvOutput.write("Solar Panel Temp X- C");
        csvOutput.write("Solar Panel Temp Y+ C");
        csvOutput.write("Solar Panel Temp Y- C");
        csvOutput.write("3.3 Bus Voltage mV");
        csvOutput.write("3.3 Bus Current mA");
        csvOutput.write("5.0 Bus Voltage mV");
        csvOutput.write("RF Temperature C");
        csvOutput.write("Receive Current mA");
        csvOutput.write("RF Current 3.3V Bus mA");
        csvOutput.write("RF Current 5.0V Bus mA");
        csvOutput.write("PA Device Temperature C");
        csvOutput.write("PA Bus Current mA");
        csvOutput.write("Antenna Temp 0 C");
        csvOutput.write("Antenna Temp 1 C");
        csvOutput.endRecord();

        long tsLong = 0;
        String c1 = "";
        String c2 = "";
        String c3 = "";
        String c4 = "";
        String c5 = "";
        String c6 = "";
        String c7 = "";
        String c8 = "";
        String c9 = "";
        String c10 = "";
        String c11 = "";
        String c12 = "";
        String c13 = "";
        String c14 = "";
        String c15 = "";
        String c16 = "";
        String c17 = "";
        String c18 = "";
        String c19 = "";
        String c20 = "";
        String c21 = "";
        String c22 = "";
        String c23 = "";
        String c24 = "";
        String c25 = "";
        String c26 = "";
        String c27 = "";
        String c28 = "";
        String c29 = "";

        for (RealTimeEntity entity : realTime12) {

            Timestamp satelliteTime = entity.getSatelliteTime();

            if (tsLong == 0) {
                tsLong = satelliteTime.getTime();

                // Solar Panel Voltage X mV
                c1 = String.format("%4d", entity.getC1());
                // Solar Panel Voltage Y mV
                c2 = String.format("%4d", entity.getC2());
                // Solar Panel Voltage Z mV
                c3 = String.format("%4d", entity.getC3());
                // Total Photo Current mA
                c4 = String.format("%4d", entity.getC4());
                // Battery Voltage mV
                c5 = String.format("%4d", entity.getC5());
                // Total System Current mA
                c6 = String.format("%4d", entity.getC6());
                // Boost Converter Temp 1 C
                c7 = String.format("%4d", twosComplement(entity.getC9()));
                // Boost Converter Temp 2 C
                c8 = String.format("%4d", twosComplement(entity.getC10()));
                // Boost Converter Temp 3 C
                c9 = String.format("%4d", twosComplement(entity.getC11()));
                // Battery Temp C
                c10 = String.format("%4d", twosComplement(entity.getC12()));
                // Sun Sensor X+
                c11 = String.format("%4.2f", SOL_ILLUMINATION[entity.getC17().intValue()]);
                // Sun Sensor Y+
                c12 = String.format("%4.2f", SOL_ILLUMINATION[entity.getC18().intValue()]);
                // Sun Sensor Z+
                c13 = String.format("%4.2f", SOL_ILLUMINATION[entity.getC19().intValue()]);
                // Solar Panel Temp X+ C
                c14 = scale(entity.getC20(), -0.2073, 158.239);
                // Solar Panel Temp X- C
                c15 = scale(entity.getC21(), -0.2083, 159.227);
                // Solar Panel Temp Y+ C
                c16 = scale(entity.getC22(), -0.2076, 158.656);
                // Solar Panel Temp Y- C
                c17 = scale(entity.getC23(), -0.2087, 159.045);
                // 3.3 Bus Voltage mV
                c18 = scale(entity.getC24(), 4.0, 0.0);
                // 3.3 Bus Current mA
                c19 = scale(entity.getC25(), 1.0, 0.0);
                // 5.0 Bus Voltage mV
                c20 = scale(entity.getC26(), 6.0, 0.0);
                // RF Temperature C
                c21 = scale(entity.getC34(), -0.857, 193.672);
                // Receive Current mA
                c22 = String.format("%4d", entity.getC35());
                // RF Current 3.3V Bus mA
                c23 = String.format("%4d", entity.getC36());
                // RF Current 5.0V Bus mA
                c24 = String.format("%4d", entity.getC37());
                // PA Device Temperature C
                c25 = String.format("%4.1f", getPaTemp(entity.getC40()));
                // PA Bus Current mA
                c26 = String.format("%4.1f", getPaCurrent(entity.getC41()));
                // Antenna Temp 0 C
                c27 = String.format("%5.1f", getAntsTemp(entity.getC42().intValue()));
                // Antenna Temp 1 C
                c28 = String.format("%5.1f", getAntsTemp(entity.getC43().intValue()));

                writeRecord(csvOutput, satelliteTime, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13,
                        c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28);
            } else {

                final long timeDiff = satelliteTime.getTime() - tsLong;
                if (timeDiff > 5000) {
                    // fill in the gaps
                    long gaps = (timeDiff / 5000);
                    for (long i = 1; i < gaps; i++) {
                        Timestamp intervalTime = new Timestamp(tsLong + (5000 * i));
                        writeRecord(csvOutput, intervalTime, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12,
                                c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28);
                    }
                }

                // Solar Panel Voltage X mV
                c1 = String.format("%4d", entity.getC1());
                // Solar Panel Voltage Y mV
                c2 = String.format("%4d", entity.getC2());
                // Solar Panel Voltage Z mV
                c3 = String.format("%4d", entity.getC3());
                // Total Photo Current mA
                c4 = String.format("%4d", entity.getC4());
                // Battery Voltage mV
                c5 = String.format("%4d", entity.getC5());
                // Total System Current mA
                c6 = String.format("%4d", entity.getC6());
                // Boost Converter Temp 1 C
                c7 = String.format("%4d", twosComplement(entity.getC9()));
                // Boost Converter Temp 2 C
                c8 = String.format("%4d", twosComplement(entity.getC10()));
                // Boost Converter Temp 3 C
                c9 = String.format("%4d", twosComplement(entity.getC11()));
                // Battery Temp C
                c10 = String.format("%4d", twosComplement(entity.getC12()));
                // Sun Sensor X+
                c11 = String.format("%4.2f", SOL_ILLUMINATION[entity.getC17().intValue()]);
                // Sun Sensor Y+
                c12 = String.format("%4.2f", SOL_ILLUMINATION[entity.getC18().intValue()]);
                // Sun Sensor Z+
                c13 = String.format("%4.2f", SOL_ILLUMINATION[entity.getC19().intValue()]);
                // Solar Panel Temp X+ C
                c14 = scale(entity.getC20(), -0.2073, 158.239);
                // Solar Panel Temp X- C
                c15 = scale(entity.getC21(), -0.2083, 159.227);
                // Solar Panel Temp Y+ C
                c16 = scale(entity.getC22(), -0.2076, 158.656);
                // Solar Panel Temp Y- C
                c17 = scale(entity.getC23(), -0.2087, 159.045);
                // 3.3 Bus Voltage mV
                c18 = scale(entity.getC24(), 4.0, 0.0);
                // 3.3 Bus Current mA
                c19 = scale(entity.getC25(), 1.0, 0.0);
                // 5.0 Bus Voltage mV
                c20 = scale(entity.getC26(), 6.0, 0.0);
                // RF Temperature C
                c21 = scale(entity.getC34(), -0.857, 193.672);
                // Receive Current mA
                c22 = String.format("%4d", entity.getC35());
                // RF Current 3.3V Bus mA
                c23 = String.format("%4d", entity.getC36());
                // RF Current 5.0V Bus mA
                c24 = String.format("%4d", entity.getC37());
                // PA Device Temperature C
                c25 = String.format("%4.1f", getPaTemp(entity.getC40()));
                // PA Bus Current mA
                c26 = String.format("%4.1f", getPaCurrent(entity.getC41()));
                // Antenna Temp 0 C
                c27 = String.format("%5.1f", getAntsTemp(entity.getC42().intValue()));
                // Antenna Temp 1 C
                c28 = String.format("%5.1f", getAntsTemp(entity.getC43().intValue()));

                writeRecord(csvOutput, satelliteTime, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13,
                        c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28);

                tsLong = satelliteTime.getTime();
            }
        }

        csvOutput.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.qtplaf.library.util.Calendar.java

/**
 * Constructor assigning a <code>Timestamp</code>.
 *
 * @param timestamp The <code>Timestamp</code>.
 *//*from   w w  w.j  av a  2 s.  c o  m*/
public Calendar(java.sql.Timestamp timestamp) {
    setTimeInMillis(timestamp.getTime());
}

From source file:com.alibaba.wasp.jdbc.TestPreparedStatement.java

public void testDateTimeTimestampWithCalendar() throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("create table ts(x timestamp primary key)");
    stat.execute("create table t(x time primary key)");
    stat.execute("create table d(x date)");
    Calendar utcCalendar = new GregorianCalendar(new SimpleTimeZone(0, "Z"));
    TimeZone old = TimeZone.getDefault();
    DateTimeUtils.resetCalendar();//from  w  w w . j a v a2  s .c o  m
    TimeZone.setDefault(TimeZone.getTimeZone("PST"));
    try {
        Timestamp ts1 = Timestamp.valueOf("2010-03-13 18:15:00");
        Time t1 = new Time(ts1.getTime());
        Date d1 = new Date(ts1.getTime());
        // when converted to UTC, this is 03:15, which doesn't actually exist
        // because of summer time change at that day
        Timestamp ts2 = Timestamp.valueOf("2010-03-13 19:15:00");
        Time t2 = new Time(ts2.getTime());
        Date d2 = new Date(ts2.getTime());
        PreparedStatement prep;
        ResultSet rs;
        prep = conn.prepareStatement("insert into ts values(?)");
        prep.setTimestamp(1, ts1, utcCalendar);
        prep.execute();
        prep.setTimestamp(1, ts2, utcCalendar);
        prep.execute();
        prep = conn.prepareStatement("insert into t values(?)");
        prep.setTime(1, t1, utcCalendar);
        prep.execute();
        prep.setTime(1, t2, utcCalendar);
        prep.execute();
        prep = conn.prepareStatement("insert into d values(?)");
        prep.setDate(1, d1, utcCalendar);
        prep.execute();
        prep.setDate(1, d2, utcCalendar);
        prep.execute();
        rs = stat.executeQuery("select * from ts order by x");
        rs.next();
        assertEquals("2010-03-14 02:15:00.0", rs.getString(1));
        assertEquals("2010-03-13 18:15:00.0", rs.getTimestamp(1, utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp(1).toString());
        assertEquals("2010-03-14 02:15:00.0", rs.getString("x"));
        assertEquals("2010-03-13 18:15:00.0", rs.getTimestamp("x", utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp("x").toString());
        rs.next();
        assertEquals("2010-03-14 03:15:00.0", rs.getString(1));
        assertEquals("2010-03-13 19:15:00.0", rs.getTimestamp(1, utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp(1).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getString("x"));
        assertEquals("2010-03-13 19:15:00.0", rs.getTimestamp("x", utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp("x").toString());
        rs = stat.executeQuery("select * from t order by x");
        rs.next();
        assertEquals("02:15:00", rs.getString(1));
        assertEquals("18:15:00", rs.getTime(1, utcCalendar).toString());
        assertEquals("02:15:00", rs.getTime(1).toString());
        assertEquals("02:15:00", rs.getString("x"));
        assertEquals("18:15:00", rs.getTime("x", utcCalendar).toString());
        assertEquals("02:15:00", rs.getTime("x").toString());
        rs.next();
        assertEquals("03:15:00", rs.getString(1));
        assertEquals("19:15:00", rs.getTime(1, utcCalendar).toString());
        assertEquals("03:15:00", rs.getTime(1).toString());
        assertEquals("03:15:00", rs.getString("x"));
        assertEquals("19:15:00", rs.getTime("x", utcCalendar).toString());
        assertEquals("03:15:00", rs.getTime("x").toString());
        rs = stat.executeQuery("select * from d order by x");
        rs.next();
        assertEquals("2010-03-14", rs.getString(1));
        assertEquals("2010-03-13", rs.getDate(1, utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate(1).toString());
        assertEquals("2010-03-14", rs.getString("x"));
        assertEquals("2010-03-13", rs.getDate("x", utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate("x").toString());
        rs.next();
        assertEquals("2010-03-14", rs.getString(1));
        assertEquals("2010-03-13", rs.getDate(1, utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate(1).toString());
        assertEquals("2010-03-14", rs.getString("x"));
        assertEquals("2010-03-13", rs.getDate("x", utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate("x").toString());
    } finally {
        TimeZone.setDefault(old);
        DateTimeUtils.resetCalendar();
    }
    stat.execute("drop table ts");
    stat.execute("drop table t");
    stat.execute("drop table d");
}

From source file:org.osaf.cosmo.migrate.ZeroPointFiveToZeroPointSixMigration.java

private void migrateUsers(Connection conn) throws Exception {
    PreparedStatement stmt = null;
    PreparedStatement updateStmt = null;
    PreparedStatement insertStmt = null;
    ResultSet rs = null;/*w  w  w. ja va 2 s. co m*/
    long count = 0;
    log.debug("starting migrateUsers()");

    try {
        stmt = conn.prepareStatement("select id, datecreated, datemodified, admin from users");
        updateStmt = conn.prepareStatement("update users set createdate=?, modifydate=? where id=?");
        insertStmt = conn.prepareStatement(
                "insert into user_preferences (userid, preferencename, preferencevalue) values (?, ?, ?)");
        insertStmt.setString(2, "Login.Url");

        rs = stmt.executeQuery();

        while (rs.next()) {
            count++;
            long userId = rs.getLong(1);
            Timestamp createTs = rs.getTimestamp(2);
            Timestamp modifyTs = rs.getTimestamp(3);
            boolean isAdmin = rs.getBoolean(4);

            // update user timestamps
            updateStmt.setLong(1, createTs.getTime());
            updateStmt.setLong(2, modifyTs.getTime());
            updateStmt.setLong(3, userId);

            updateStmt.executeUpdate();

            // insert account preference
            insertStmt.setLong(1, userId);
            insertStmt.setString(3, isAdmin ? "/account/view" : "/pim");
            insertStmt.executeUpdate();
        }
    } finally {
        if (rs != null)
            rs.close();
        if (stmt != null)
            stmt.close();
        if (updateStmt != null)
            updateStmt.close();
        if (insertStmt != null)
            insertStmt.close();
    }

    log.debug("processed " + count + " users");
}

From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java

/**
  * /*from  ww  w  .  ja v  a2 s.c  o m*/
  * From server to client. One packet for each row in the result set.
  * 
  * <pre>
  * Bytes                   Name
  * -----                   ----
  * n (Length Coded String) (column value)
  * ...
  * 
  * (column value):         The data in the column, as a character string.
  *                         If a column is defined as non-character, the
  *                         server converts the value into a character
  *                         before sending it. Since the value is a Length
  *                         Coded String, a NULL can be represented with a
  *                         single byte containing 251(see the description
  *                         of Length Coded Strings in section "Elements" above).
  * 
  * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Row_Data_Packet
  * </pre>
  * 
  * @param buffer
  * @param nColumns 
  * @param rowRec
  */
public void writeRowTextBody(ByteBuf buffer, long pRecord, int nColumns) {
    for (int i = 0; i < nColumns; i++) {
        Object fv = Record.getValue(pRecord, i);
        if (fv instanceof Boolean) {
            // mysql has no boolean it is actually tinyint
            fv = ((Boolean) fv) ? 1 : 0;
        }
        if (fv == null) {
            // null mark is 251
            buffer.writeByte((byte) 251);
        } else if (fv instanceof Duration) {
            Duration t = (Duration) fv;
            String text = DurationFormatUtils.formatDuration(t.toMillis(), "HH:mm:ss");
            BufferUtils.writeLenString(buffer, text, this.cs);
        } else if (fv instanceof Timestamp) {
            // @see ResultSetRow#getDateFast, mysql jdbc driver only take precision 19,21,29 if callers wants
            // to get a Date from a datetime column
            Timestamp ts = (Timestamp) fv;
            if (ts.getTime() == Long.MIN_VALUE) {
                // mysql '0000-00-00 00:00:00' is treated as null in jdbc
                buffer.writeByte((byte) 251);
            } else {
                String text;
                if (ts.getNanos() == 0) {
                    text = TIMESTAMP19_FORMAT.format(ts);
                } else {
                    text = TIMESTAMP29_FORMAT.format(ts);
                }
                BufferUtils.writeLenString(buffer, text, this.cs);
            }
        } else if (fv instanceof byte[]) {
            BufferUtils.writeWithLength(buffer, (byte[]) fv);
        } else if ((fv instanceof Date) && (((Date) fv).getTime() == Long.MIN_VALUE)) {
            // mysql '0000-00-00' is treated as null in jdbc
            buffer.writeByte((byte) 251);
        } else {
            String val = fv.toString();
            if (val.length() == 0) {
                // empty mark is 0
                buffer.writeByte((byte) 0);
            } else {
                BufferUtils.writeLenString(buffer, val, this.cs);
            }
        }
    }
}

From source file:org.apache.hadoop.hive.ql.exec.vector.expressions.TestVectorTimestampExpressions.java

private void compareToUDFUnixTimeStampLong(Timestamp ts, long y) {
    long seconds = ts.getTime() / 1000;
    if (seconds != y) {
        System.out.printf("%d vs %d for %s\n", seconds, y, ts.toString());
        Assert.assertTrue(false);//w  w w .  j a  v  a  2 s .  co m
    }
}