Example usage for java.sql Time Time

List of usage examples for java.sql Time Time

Introduction

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

Prototype

public Time(long time) 

Source Link

Document

Constructs a Time object using a milliseconds time value.

Usage

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

public static Time newTime(java.util.Date date) {
    return date == null ? null : new Time(newTimeCalendar(date).getTimeInMillis());
}

From source file:org.apache.cocoon.util.JDBCTypeConversions.java

/**
 * Set the Statement column so that the results are mapped correctly.
 *
 * @param statement the prepared statement
 * @param position the position of the column
 * @param value the value of the column//from  w w  w  .j  a  va2  s.  co m
 */
public static void setColumn(PreparedStatement statement, int position, Object value, Integer typeObject)
        throws Exception {
    if (value instanceof String) {
        value = ((String) value).trim();
    }
    if (typeObject == null) {
        throw new SQLException("Can't set column because the type is unrecognized");
    }
    if (value == null) {
        /** If the value is null, set the column value null and return **/
        statement.setNull(position, typeObject.intValue());
        return;
    }
    if ("".equals(value)) {
        switch (typeObject.intValue()) {
        case Types.CHAR:
        case Types.CLOB:
        case Types.VARCHAR:
            /** If the value is an empty string and the column is
            a string type, we can continue **/
            break;
        default:
            /** If the value is an empty string and the column
            is something else, we treat it as a null value **/
            statement.setNull(position, typeObject.intValue());
            return;
        }
    }

    File file = null;
    int length = -1;
    InputStream asciiStream = null;

    //System.out.println("========================================================================");
    //System.out.println("JDBCTypeConversions: setting type "+typeObject.intValue());
    switch (typeObject.intValue()) {
    case Types.CLOB:
        //System.out.println("CLOB");
        Clob clob = null;
        if (value instanceof Clob) {
            clob = (Clob) value;
        } else if (value instanceof File) {
            File asciiFile = (File) value;
            asciiStream = new BufferedInputStream(new FileInputStream(asciiFile));
            length = (int) asciiFile.length();
            clob = new ClobHelper(asciiStream, length);
        } else if (value instanceof Part) {
            Part anyFile = (Part) value;
            asciiStream = new BufferedInputStream(anyFile.getInputStream());
            length = anyFile.getSize();
            clob = new ClobHelper(asciiStream, length);
        } else if (value instanceof JDBCxlobHelper) {
            asciiStream = ((JDBCxlobHelper) value).inputStream;
            length = ((JDBCxlobHelper) value).length;
            clob = new ClobHelper(asciiStream, length);
        } else if (value instanceof Source) {
            asciiStream = ((Source) value).getInputStream();
            length = (int) ((Source) value).getContentLength();
            clob = new ClobHelper(asciiStream, length);
        } else {
            String asciiText = value.toString();
            asciiStream = new ByteArrayInputStream(asciiText.getBytes());
            length = asciiText.length();
            clob = new ClobHelper(asciiStream, length);
        }

        statement.setClob(position, clob);
        break;
    case Types.CHAR:
        // simple large object, e.g. Informix's TEXT
        //System.out.println("CHAR");

        if (value instanceof File) {
            File asciiFile = (File) value;
            asciiStream = new BufferedInputStream(new FileInputStream(asciiFile));
            length = (int) asciiFile.length();
        } else if (value instanceof JDBCxlobHelper) {
            asciiStream = ((JDBCxlobHelper) value).inputStream;
            length = ((JDBCxlobHelper) value).length;
        } else if (value instanceof Source) {
            asciiStream = ((Source) value).getInputStream();
            length = (int) ((Source) value).getContentLength();
        } else if (value instanceof Part) {
            Part anyFile = (Part) value;
            asciiStream = new BufferedInputStream(anyFile.getInputStream());
            length = anyFile.getSize();
            clob = new ClobHelper(asciiStream, length);
        } else {
            String asciiText = value.toString();
            asciiStream = new BufferedInputStream(new ByteArrayInputStream(asciiText.getBytes()));
            length = asciiText.length();
        }

        statement.setAsciiStream(position, asciiStream, length);
        break;
    case Types.BIGINT:
        //System.out.println("BIGINT");
        BigDecimal bd = null;

        if (value instanceof BigDecimal) {
            bd = (BigDecimal) value;
        } else if (value instanceof Number) {
            bd = BigDecimal.valueOf(((Number) value).longValue());
        } else {
            bd = new BigDecimal(value.toString());
        }

        statement.setBigDecimal(position, bd);
        break;
    case Types.TINYINT:
        //System.out.println("TINYINT");
        Byte b = null;

        if (value instanceof Byte) {
            b = (Byte) value;
        } else if (value instanceof Number) {
            b = new Byte(((Number) value).byteValue());
        } else {
            b = new Byte(value.toString());
        }

        statement.setByte(position, b.byteValue());
        break;
    case Types.DATE:
        //System.out.println("DATE");
        Date d = null;

        if (value instanceof Date) {
            d = (Date) value;
        } else if (value instanceof java.util.Date) {
            d = new Date(((java.util.Date) value).getTime());
        } else if (value instanceof Calendar) {
            d = new Date(((Calendar) value).getTime().getTime());
        } else {
            d = Date.valueOf(value.toString());
        }

        statement.setDate(position, d);
        break;
    case Types.DOUBLE:
        //System.out.println("DOUBLE");
        double db;

        if (value instanceof Number) {
            db = (((Number) value).doubleValue());
        } else {
            db = Double.parseDouble(value.toString());
        }
        statement.setDouble(position, db);
        break;
    case Types.FLOAT:
        //System.out.println("FLOAT");
        float f;

        if (value instanceof Number) {
            f = (((Number) value).floatValue());
        } else {
            f = Float.parseFloat(value.toString());
        }
        statement.setFloat(position, f);
        break;
    case Types.NUMERIC:
        //System.out.println("NUMERIC");
        long l;

        if (value instanceof Number) {
            l = (((Number) value).longValue());
        } else {
            l = Long.parseLong(value.toString());
        }

        statement.setLong(position, l);
        break;
    case Types.SMALLINT:
        //System.out.println("SMALLINT");
        Short s = null;

        if (value instanceof Short) {
            s = (Short) value;
        } else if (value instanceof Number) {
            s = new Short(((Number) value).shortValue());
        } else {
            s = new Short(value.toString());
        }

        statement.setShort(position, s.shortValue());
        break;
    case Types.TIME:
        //System.out.println("TIME");
        Time t = null;

        if (value instanceof Time) {
            t = (Time) value;
        } else if (value instanceof java.util.Date) {
            t = new Time(((java.util.Date) value).getTime());
        } else {
            t = Time.valueOf(value.toString());
        }

        statement.setTime(position, t);
        break;
    case Types.TIMESTAMP:
        //System.out.println("TIMESTAMP");
        Timestamp ts = null;

        if (value instanceof Time) {
            ts = (Timestamp) value;
        } else if (value instanceof java.util.Date) {
            ts = new Timestamp(((java.util.Date) value).getTime());
        } else {
            ts = Timestamp.valueOf(value.toString());
        }

        statement.setTimestamp(position, ts);
        break;
    case Types.ARRAY:
        //System.out.println("ARRAY");
        statement.setArray(position, (Array) value); // no way to convert string to array
        break;
    case Types.STRUCT:
        //System.out.println("STRUCT");
    case Types.OTHER:
        //System.out.println("OTHER");
        statement.setObject(position, value);
        break;
    case Types.LONGVARBINARY:
        //System.out.println("LONGVARBINARY");
        statement.setTimestamp(position, new Timestamp((new java.util.Date()).getTime()));
        break;
    case Types.VARCHAR:
        //System.out.println("VARCHAR");
        statement.setString(position, value.toString());
        break;
    case Types.BLOB:
        //System.out.println("BLOB");
        if (value instanceof JDBCxlobHelper) {
            statement.setBinaryStream(position, ((JDBCxlobHelper) value).inputStream,
                    ((JDBCxlobHelper) value).length);
        } else if (value instanceof Source) {
            statement.setBinaryStream(position, ((Source) value).getInputStream(),
                    (int) ((Source) value).getContentLength());
        } else {
            Blob blob = null;
            if (value instanceof Blob) {
                blob = (Blob) value;
            } else if (value instanceof File) {
                file = (File) value;
                blob = new BlobHelper(new FileInputStream(file), (int) file.length());
            } else if (value instanceof String) {
                file = new File((String) value);
                blob = new BlobHelper(new FileInputStream(file), (int) file.length());
            } else if (value instanceof Part) {
                Part anyFile = (Part) value;
                blob = new BlobHelper(new BufferedInputStream(anyFile.getInputStream()), anyFile.getSize());
            } else {
                throw new SQLException("Invalid type for blob: " + value.getClass().getName());
            }
            //InputStream input = new BufferedInputStream(new FileInputStream(file));
            statement.setBlob(position, blob);
        }
        break;
    case Types.VARBINARY:
        //System.out.println("VARBINARY");
        if (value instanceof JDBCxlobHelper) {
            statement.setBinaryStream(position, ((JDBCxlobHelper) value).inputStream,
                    ((JDBCxlobHelper) value).length);
        } else if (value instanceof Source) {
            statement.setBinaryStream(position, ((Source) value).getInputStream(),
                    (int) ((Source) value).getContentLength());
        } else if (value instanceof Part) {
            statement.setBinaryStream(position, ((Part) value).getInputStream(), ((Part) value).getSize());
        } else {
            if (value instanceof File) {
                file = (File) value;
            } else if (value instanceof String) {
                file = new File((String) value);
            } else {
                throw new SQLException("Invalid type for blob: " + value.getClass().getName());
            }
            //InputStream input = new BufferedInputStream(new FileInputStream(file));
            FileInputStream input = new FileInputStream(file);
            statement.setBinaryStream(position, input, (int) file.length());
        }
        break;
    case Types.INTEGER:
        //System.out.println("INTEGER");
        Integer i = null;
        if (value instanceof Integer) {
            i = (Integer) value;
        } else if (value instanceof Number) {
            i = new Integer(((Number) value).intValue());
        } else {
            i = new Integer(value.toString());
        }
        statement.setInt(position, i.intValue());
        break;
    case Types.BIT:
        //System.out.println("BIT");
        Boolean bo = null;
        if (value instanceof Boolean) {
            bo = (Boolean) value;
        } else if (value instanceof Number) {
            bo = BooleanUtils.toBooleanObject(((Number) value).intValue() == 1);
        } else {
            bo = BooleanUtils.toBooleanObject(value.toString());
        }
        statement.setBoolean(position, bo.booleanValue());
        break;

    default:
        //System.out.println("default");
        throw new SQLException("Impossible exception - invalid type ");
    }
    //System.out.println("========================================================================");
}

From source file:ca.oson.json.gson.functional.DefaultTypeAdaptersTest.java

public void testDefaultJavaSqlTimeSerialization() {
    Time now = new Time(1259875082000L);
    String json = oson.toJson(now);
    assertEquals("2009-12-03T13:18:02.00Z", json);
}

From source file:org.ojbc.adapters.analyticaldatastore.processor.IncidentReportProcessor.java

protected void processArrests(Document incidentReport, Integer incidentPk, String reportingSystem)
        throws Exception {
    NodeList arrestSubjectAssocationNodes = XmlUtils.xPathNodeListSearch(incidentReport,
            PATH_TO_LEXS_DIGEST + "/lexsdigest:Associations/lexsdigest:ArrestSubjectAssociation");

    if (arrestSubjectAssocationNodes == null || arrestSubjectAssocationNodes.getLength() == 0) {
        log.debug("No Arrest nodes in document");
        return;// w  w w.ja  v  a  2s.com
    }

    for (int i = 0; i < arrestSubjectAssocationNodes.getLength(); i++) {
        Arrest arrest = new Arrest();

        arrest.setIncidentID(incidentPk);
        arrest.setReportingSystem(reportingSystem);

        if (arrestSubjectAssocationNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
            String personId = XmlUtils.xPathStringSearch(arrestSubjectAssocationNodes.item(i),
                    "nc:PersonReference/@s:ref");
            log.debug("Arrestee Person ID: " + personId);

            String arrestId = XmlUtils.xPathStringSearch(arrestSubjectAssocationNodes.item(i),
                    "nc:ActivityReference/@s:ref");
            log.debug("Arrest ID: " + arrestId);

            Node personNode = XmlUtils.xPathNodeSearch(incidentReport, PATH_TO_LEXS_DIGEST
                    + "/lexsdigest:EntityPerson/lexsdigest:Person[@s:id='" + personId + "']");

            Map<String, Object> arrestee = AnalyticalDataStoreUtils.retrieveMapOfPersonAttributes(personNode);
            String personIdentifierKey = identifierGenerationStrategy.generateIdentifier(arrestee);
            log.debug("Arrestee person identifier keys: " + personIdentifierKey);

            String personBirthDateAsString = (String) arrestee
                    .get(IdentifierGenerationStrategy.BIRTHDATE_FIELD);
            String personRace = (String) arrestee.get("personRace");

            if (StringUtils.isBlank(personRace)) {
                personRace = XmlUtils.xPathStringSearch(incidentReport, PATH_TO_LEXS_DATA_ITEM_PACKAGE
                        + "/lexs:StructuredPayload/ndexia:IncidentReport/ndexia:Person/ndexia:PersonAugmentation[lexslib:SameAsDigestReference/@lexslib:ref='"
                        + personId + "']/ndexia:PersonRaceCode");
                log.debug("Person race retrieved from ndex payload: " + personRace);
            }

            String personSex = (String) arrestee.get(IdentifierGenerationStrategy.SEX_FIELD);

            int personPk = savePerson(personBirthDateAsString, personSex, personRace, personIdentifierKey);
            arrest.setPersonID(personPk);

            Date arrestDateTime = returnArrestDate(incidentReport, arrestSubjectAssocationNodes.item(i));

            arrest.setArrestDate(arrestDateTime);

            Time arrestTime = new Time(arrestDateTime.getTime());
            log.debug("Arrest Time: " + arrestTime.toString());

            arrest.setArrestTime(arrestTime);

            String arrestingAgency = returnArrestingAgency(incidentReport);
            log.debug("Arresting Agency: " + arrestingAgency);

            if (StringUtils.isNotBlank(arrestingAgency)) {
                arrest.setArrestingAgencyName(arrestingAgency);
            }

            //Save arrest
            int arrestPk = analyticalDatastoreDAO.saveArrest(arrest);

            //Save Charges
            processArrestCharge(incidentReport, arrestPk, arrestId);
        }
    }
}

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

public static Time newTime(int hourOfDay, int minuteOfHour, int secondOfMinute) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, 1970);//from  w  w w .  jav a 2  s .  c  om
    c.set(Calendar.MONTH, Calendar.JANUARY);
    c.set(Calendar.DAY_OF_MONTH, 1);
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minuteOfHour);
    c.set(Calendar.SECOND, secondOfMinute);
    c.set(Calendar.MILLISECOND, 0);
    return new Time(c.getTimeInMillis());
}

From source file:jp.co.ctc_g.jfw.core.util.Dates.java

/**
 * ?????java.sql.Time???.//from   w  ww  . j  ava2  s  . c om
 * @return ???java.sql.Time
 */
public static Time getSqlTime() {

    return new Time(System.currentTimeMillis());
}

From source file:edu.lternet.pasta.portal.HarvestReport.java

private String reportIdFormatter(String reportId) {

    String newId;/* ww w .  java  2s  .  c o  m*/

    if (reportId != null) {
        String tokens[] = reportId.split("-");
        Integer length = tokens.length;

        Long epoch = Long.valueOf(tokens[length - 1]);

        Time time = new Time(epoch);
        Date date = new Date(epoch);

        newId = date.toString();

        return newId + " (" + tokens[1] + ")";
    } else {
        return "";
    }
}

From source file:org.kuali.kpme.core.calendar.entry.CalendarEntryBo.java

public Time getBatchPayrollApprovalTime() {
    return batchPayrollApprovalDateTime != null ? new Time(batchPayrollApprovalDateTime.getTime()) : null;
}

From source file:fr.certu.chouette.dao.hibernate.AbstractDaoTemplateTests.java

protected AccessLink createAccessLink() {
    AccessLink accessLink = new AccessLink();
    accessLink.setAccessPoint(createAccessPoint());
    accessLink.setObjectId("AccessLink:" + getNextObjectId());
    accessLink.setCreationTime(new Date());
    accessLink.setStopArea(createStopArea());
    accessLink.addUserNeed(UserNeedEnum.ALLERGIC);
    accessLink.addUserNeed(UserNeedEnum.ASSISTEDWHEELCHAIR);
    accessLink.setDefaultDuration(new Time(new Date().getTime()));

    return accessLink;
}

From source file:jp.co.ctc_g.jfw.core.util.Dates.java

/**
 * ?????java.sql.Time???.//from   ww w .j  a v  a 2s  .com
 * @param hour 
 * @param minutes 
 * @param second 
 * @return ????java.sql.Time
 */
public static Time getSqlTime(int hour, int minutes, int second) {

    Calendar currentCalendar = Dates.getCalendarInstance();
    currentCalendar.set(Dates.getYear(), Dates.getMonth(), Dates.getDay(), hour, minutes, second);
    return new Time(currentCalendar.getTime().getTime());
}