Example usage for java.sql Time valueOf

List of usage examples for java.sql Time valueOf

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public static Time valueOf(LocalTime time) 

Source Link

Document

Obtains an instance of Time from a LocalTime object with the same hour, minute and second time value as the given LocalTime .

Usage

From source file:org.apache.sqoop.mapreduce.hcat.SqoopHCatExportHelper.java

private Object convertStringTypes(Object val, String javaColType) {
    String valStr = val.toString();
    if (javaColType.equals(BIG_DECIMAL_TYPE)) {
        return new BigDecimal(valStr);
    } else if (javaColType.equals(DATE_TYPE) || javaColType.equals(TIME_TYPE)
            || javaColType.equals(TIMESTAMP_TYPE)) {
        // Oracle expects timestamps for Date also by default based on version
        // Just allow all date types to be assignment compatible
        if (valStr.length() == 10 && valStr.matches("^\\d{4}-\\d{2}-\\d{2}$")) {
            // Date in yyyy-mm-dd format
            Date d = Date.valueOf(valStr);
            if (javaColType.equals(DATE_TYPE)) {
                return d;
            } else if (javaColType.equals(TIME_TYPE)) {
                return new Time(d.getTime());
            } else if (javaColType.equals(TIMESTAMP_TYPE)) {
                return new Timestamp(d.getTime());
            }//from   w ww . java  2  s .  c  o m
        } else if (valStr.length() == 8 && valStr.matches("^\\d{2}:\\d{2}:\\d{2}$")) {
            // time in hh:mm:ss
            Time t = Time.valueOf(valStr);
            if (javaColType.equals(DATE_TYPE)) {
                return new Date(t.getTime());
            } else if (javaColType.equals(TIME_TYPE)) {
                return t;
            } else if (javaColType.equals(TIMESTAMP_TYPE)) {
                return new Timestamp(t.getTime());
            }
        } else if (valStr.length() >= 19 && valStr.length() <= 26
                && valStr.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}(.\\d+)?$")) {
            // timestamp in yyyy-mm-dd hh:mm:ss
            Timestamp ts = Timestamp.valueOf(valStr);
            if (javaColType.equals(DATE_TYPE)) {
                return new Date(ts.getTime());
            } else if (javaColType.equals(TIME_TYPE)) {
                return new Time(ts.getTime());
            } else if (javaColType.equals(TIMESTAMP_TYPE)) {
                return ts;
            }
        } else {
            return null;
        }
    } else if (javaColType.equals(STRING_TYPE)) {
        return valStr;
    } else if (javaColType.equals(BOOLEAN_TYPE)) {
        return Boolean.valueOf(valStr);
    } else if (javaColType.equals(BYTE_TYPE)) {
        return Byte.parseByte(valStr);
    } else if (javaColType.equals(SHORT_TYPE)) {
        return Short.parseShort(valStr);
    } else if (javaColType.equals(INTEGER_TYPE)) {
        return Integer.parseInt(valStr);
    } else if (javaColType.equals(LONG_TYPE)) {
        return Long.parseLong(valStr);
    } else if (javaColType.equals(FLOAT_TYPE)) {
        return Float.parseFloat(valStr);
    } else if (javaColType.equals(DOUBLE_TYPE)) {
        return Double.parseDouble(valStr);
    }
    return null;
}

From source file:org.apache.synapse.mediators.db.AbstractDBMediator.java

/**
 * Return a Prepared statement for the given Statement object, which is ready to be executed
 *
 * @param stmnt  SQL stataement to be executed
 * @param con    The connection to be used
 * @param msgCtx Current message context
 * @return a PreparedStatement/*from w  w w.  ja v  a 2 s  . com*/
 * @throws SQLException on error
 */
protected PreparedStatement getPreparedStatement(Statement stmnt, Connection con, MessageContext msgCtx)
        throws SQLException {

    SynapseLog synLog = getLog(msgCtx);

    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Getting a connection from DataSource " + getDSName()
                + " and preparing statement : " + stmnt.getRawStatement());
    }

    if (con == null) {
        String msg = "Connection from DataSource " + getDSName() + " is null.";
        log.error(msg);
        throw new SynapseException(msg);
    }

    if (dataSource instanceof BasicDataSource) {

        BasicDataSource basicDataSource = (BasicDataSource) dataSource;
        int numActive = basicDataSource.getNumActive();
        int numIdle = basicDataSource.getNumIdle();
        String connectionId = Integer.toHexString(con.hashCode());

        DBPoolView dbPoolView = getDbPoolView();
        if (dbPoolView != null) {
            dbPoolView.setNumActive(numActive);
            dbPoolView.setNumIdle(numIdle);
            dbPoolView.updateConnectionUsage(connectionId);
        }

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("[ DB Connection : " + con + " ]");
            synLog.traceOrDebug("[ DB Connection instance identifier : " + connectionId + " ]");
            synLog.traceOrDebug("[ Number of Active Connection : " + numActive + " ]");
            synLog.traceOrDebug("[ Number of Idle Connection : " + numIdle + " ]");
        }
    }

    PreparedStatement ps = con.prepareStatement(stmnt.getRawStatement());

    // set parameters if any
    List<Statement.Parameter> params = stmnt.getParameters();
    int column = 1;

    for (Statement.Parameter param : params) {
        if (param == null) {
            continue;
        }
        String value = (param.getPropertyName() != null ? param.getPropertyName()
                : param.getXpath().stringValueOf(msgCtx));

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Setting as parameter : " + column + " value : " + value + " as JDBC Type : "
                    + param.getType() + "(see java.sql.Types for valid " + "types)");
        }

        switch (param.getType()) {
        // according to J2SE 1.5 /docs/guide/jdbc/getstart/mapping.html
        case Types.CHAR:
        case Types.VARCHAR:
        case Types.LONGVARCHAR: {
            if (value != null && value.length() != 0) {
                ps.setString(column++, value);
            } else {
                ps.setString(column++, null);
            }
            break;
        }
        case Types.NUMERIC:
        case Types.DECIMAL: {
            if (value != null && value.length() != 0) {
                ps.setBigDecimal(column++, new BigDecimal(value));
            } else {
                ps.setBigDecimal(column++, null);
            }
            break;
        }
        case Types.BIT: {
            if (value != null && value.length() != 0) {
                ps.setBoolean(column++, Boolean.parseBoolean(value));
            } else {
                ps.setNull(column++, Types.BIT);
            }
            break;
        }
        case Types.TINYINT: {
            if (value != null && value.length() != 0) {
                ps.setByte(column++, Byte.parseByte(value));
            } else {
                ps.setNull(column++, Types.TINYINT);
            }
            break;
        }
        case Types.SMALLINT: {
            if (value != null && value.length() != 0) {
                ps.setShort(column++, Short.parseShort(value));
            } else {
                ps.setNull(column++, Types.SMALLINT);
            }
            break;
        }
        case Types.INTEGER: {
            if (value != null && value.length() != 0) {
                ps.setInt(column++, Integer.parseInt(value));
            } else {
                ps.setNull(column++, Types.INTEGER);
            }
            break;
        }
        case Types.BIGINT: {
            if (value != null && value.length() != 0) {
                ps.setLong(column++, Long.parseLong(value));
            } else {
                ps.setNull(column++, Types.BIGINT);
            }
            break;
        }
        case Types.REAL: {
            if (value != null && value.length() != 0) {
                ps.setFloat(column++, Float.parseFloat(value));
            } else {
                ps.setNull(column++, Types.REAL);
            }
            break;
        }
        case Types.FLOAT: {
            if (value != null && value.length() != 0) {
                ps.setDouble(column++, Double.parseDouble(value));
            } else {
                ps.setNull(column++, Types.FLOAT);
            }
            break;
        }
        case Types.DOUBLE: {
            if (value != null && value.length() != 0) {
                ps.setDouble(column++, Double.parseDouble(value));
            } else {
                ps.setNull(column++, Types.DOUBLE);
            }
            break;
        }
        // skip BINARY, VARBINARY and LONGVARBINARY
        case Types.DATE: {
            if (value != null && value.length() != 0) {
                ps.setDate(column++, Date.valueOf(value));
            } else {
                ps.setNull(column++, Types.DATE);
            }
            break;
        }
        case Types.TIME: {
            if (value != null && value.length() != 0) {
                ps.setTime(column++, Time.valueOf(value));
            } else {
                ps.setNull(column++, Types.TIME);
            }
            break;
        }
        case Types.TIMESTAMP: {
            if (value != null && value.length() != 0) {
                ps.setTimestamp(column++, Timestamp.valueOf(value));
            } else {
                ps.setNull(column++, Types.TIMESTAMP);
            }
            break;
        }
        // skip CLOB, BLOB, ARRAY, DISTINCT, STRUCT, REF, JAVA_OBJECT
        default: {
            String msg = "Trying to set an un-supported JDBC Type : " + param.getType() + " against column : "
                    + column + " and statement : " + stmnt.getRawStatement()
                    + " used by a DB mediator against DataSource : " + getDSName()
                    + " (see java.sql.Types for valid type values)";
            handleException(msg, msgCtx);
        }
        }
    }

    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Successfully prepared statement : " + stmnt.getRawStatement()
                + " against DataSource : " + getDSName());
    }
    return ps;
}

From source file:org.jboss.bqt.client.xml.XMLQueryVisitationStrategy.java

/**
 * Consume an XML message and update the specified Time instance.
 * <br>/*from   w ww .  ja  v  a 2s  .c  o  m*/
 * @param object the instance that is to be updated with the XML message data.
 * @param cellElement the XML element that contains the data
 * @return the updated instance.
 * @exception JDOMException if there is an error consuming the message.
 */
private Object consumeMsg(Time object, Element cellElement) throws JDOMException {

    // -----------------------
    // Process the element ...
    // -----------------------
    Time result;
    try {
        result = Time.valueOf(cellElement.getTextTrim());
    } catch (Exception e) {
        throw new JDOMException("Invalid input format ", e); //$NON-NLS-1$
    }
    return result;
}

From source file:org.jumpmind.db.model.Column.java

/**
 * Tries to parse the default value of the column and returns it as an
 * object of the corresponding java type. If the value could not be parsed,
 * then the original definition is returned.
 * //www  . j  a va2  s.c om
 * @return The parsed default value
 */
public Object getParsedDefaultValue() {
    if ((defaultValue != null) && (defaultValue.length() > 0)) {
        try {
            switch (mappedTypeCode) {
            case Types.TINYINT:
            case Types.SMALLINT:
                return new Short(defaultValue);
            case Types.INTEGER:
                try {
                    return new Integer(defaultValue);
                } catch (NumberFormatException e) {
                    return new Long(defaultValue);
                }
            case Types.BIGINT:
                return new Long(defaultValue);
            case Types.DECIMAL:
            case Types.NUMERIC:
                return new BigDecimal(defaultValue);
            case Types.REAL:
                return new Float(defaultValue);
            case Types.DOUBLE:
            case Types.FLOAT:
                return new Double(defaultValue);
            case Types.DATE:
                return Date.valueOf(defaultValue);
            case Types.TIME:
                return Time.valueOf(defaultValue);
            case Types.TIMESTAMP:
                return Timestamp.valueOf(defaultValue);
            case Types.BIT:
                return FormatUtils.toBoolean(defaultValue);
            default:
                if (PlatformUtils.supportsJava14JdbcTypes()
                        && (mappedTypeCode == PlatformUtils.determineBooleanTypeCode())) {
                    return FormatUtils.toBoolean(defaultValue);
                }
                break;
            }
        } catch (NumberFormatException ex) {
            return null;
        } catch (IllegalArgumentException ex) {
            return null;
        }
    }
    return defaultValue;
}

From source file:org.jumpmind.db.platform.mssql.MsSqlDdlReader.java

@Override
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException {
    Column column = super.readColumn(metaData, values);
    String defaultValue = column.getDefaultValue();

    // Sql Server tends to surround the returned default value with one or
    // two sets of parentheses
    if (defaultValue != null) {
        while (defaultValue.startsWith("(") && defaultValue.endsWith(")")) {
            defaultValue = defaultValue.substring(1, defaultValue.length() - 1);
        }//  ww  w.  ja  v  a2 s. com

        if (column.getMappedTypeCode() == Types.TIMESTAMP) {
            // Sql Server maintains the default values for DATE/TIME jdbc
            // types, so we have to
            // migrate the default value to TIMESTAMP
            Matcher matcher = isoDatePattern.matcher(defaultValue);
            Timestamp timestamp = null;

            if (matcher.matches()) {
                timestamp = new Timestamp(Date.valueOf(matcher.group(1)).getTime());
            } else {
                matcher = isoTimePattern.matcher(defaultValue);
                if (matcher.matches()) {
                    timestamp = new Timestamp(Time.valueOf(matcher.group(1)).getTime());
                }
            }
            if (timestamp != null) {
                defaultValue = timestamp.toString();
            }
        } else if (column.getMappedTypeCode() == Types.DECIMAL || column.getMappedTypeCode() == Types.BIGINT) {
            // For some reason, Sql Server 2005 always returns DECIMAL
            // default values with a dot
            // even if the scale is 0, so we remove the dot
            if ((column.getScale() == 0) && defaultValue.endsWith(".")) {
                defaultValue = defaultValue.substring(0, defaultValue.length() - 1);
            }
        } else if (TypeMap.isTextType(column.getMappedTypeCode())) {
            if (defaultValue.startsWith("N'") && defaultValue.endsWith("'")) {
                defaultValue = defaultValue.substring(2, defaultValue.length() - 1);
            }
            defaultValue = unescape(defaultValue, "'", "''");
        }

        column.setDefaultValue(defaultValue);
    }

    if ((column.getMappedTypeCode() == Types.DECIMAL) && (column.getSizeAsInt() == 19)
            && (column.getScale() == 0)) {
        column.setMappedTypeCode(Types.BIGINT);
    }

    // These columns return sizes and/or decimal places with the metat data from MSSql Server however
    // the values are not adjustable through the create table so they are omitted 
    if (column.getJdbcTypeName() != null && (column.getJdbcTypeName().equals("smallmoney")
            || column.getJdbcTypeName().equals("money") || column.getJdbcTypeName().equals("timestamp")
            || column.getJdbcTypeName().equals("uniqueidentifier") || column.getJdbcTypeName().equals("time")
            || column.getJdbcTypeName().equals("datetime2") || column.getJdbcTypeName().equals("date"))) {
        removePlatformSizeAndDecimal(column);
    }
    return column;
}

From source file:org.jumpmind.db.platform.oracle.OracleDdlReader.java

@Override
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException {
    Column column = super.readColumn(metaData, values);
    if (column.getMappedTypeCode() == Types.DECIMAL) {
        // We're back-mapping the NUMBER columns returned by Oracle
        // Note that the JDBC driver returns DECIMAL for these NUMBER
        // columns
        if (column.getScale() <= -127 || column.getScale() >= 127) {
            if (column.getSizeAsInt() == 0) {
                /*// w  w w  .java2  s .  c  o m
                 * Latest oracle jdbc drivers for 11g return (0,-127) for
                 * types defined as integer resulting in bad mappings.
                 * NUMBER without scale or precision looks the same as
                 * INTEGER in the jdbc driver. We must check the Oracle
                 * meta data to see if type is an INTEGER.
                 */
                if (isColumnInteger((String) values.get("TABLE_NAME"), (String) values.get("COLUMN_NAME"))) {
                    column.setMappedTypeCode(Types.BIGINT);
                }
            } else if (column.getSizeAsInt() <= 63) {
                column.setMappedTypeCode(Types.REAL);
            } else {
                column.setMappedTypeCode(Types.DOUBLE);
            }
        }
    } else if (column.getMappedTypeCode() == Types.FLOAT) {
        // Same for REAL, FLOAT, DOUBLE PRECISION, which all back-map to
        // FLOAT but with
        // different sizes (63 for REAL, 126 for FLOAT/DOUBLE PRECISION)
        switch (column.getSizeAsInt()) {
        case 63:
            column.setMappedTypeCode(Types.REAL);
            break;
        case 126:
            column.setMappedTypeCode(Types.DOUBLE);
            break;
        }
    } else if ((column.getMappedTypeCode() == Types.DATE) || (column.getMappedTypeCode() == Types.TIMESTAMP)) {
        // we also reverse the ISO-format adaptation, and adjust the default
        // value to timestamp
        if (column.getDefaultValue() != null) {
            Timestamp timestamp = null;

            Matcher matcher = oracleIsoTimestampPattern.matcher(column.getDefaultValue());

            if (matcher.matches()) {
                String timestampVal = matcher.group(1);
                timestamp = Timestamp.valueOf(timestampVal);
            } else {
                matcher = oracleIsoDatePattern.matcher(column.getDefaultValue());
                if (matcher.matches()) {
                    String dateVal = matcher.group(1);
                    timestamp = new Timestamp(Date.valueOf(dateVal).getTime());
                } else {
                    matcher = oracleIsoTimePattern.matcher(column.getDefaultValue());
                    if (matcher.matches()) {
                        String timeVal = matcher.group(1);

                        timestamp = new Timestamp(Time.valueOf(timeVal).getTime());
                    }
                }
            }
            if (timestamp != null) {
                column.setDefaultValue(timestamp.toString());
            }
        }
    } else if (TypeMap.isTextType(column.getMappedTypeCode())) {
        String defaultValue = column.getDefaultValue();
        if (isNotBlank(defaultValue) && defaultValue.startsWith("('") && defaultValue.endsWith("')")) {
            defaultValue = defaultValue.substring(2, defaultValue.length() - 2);
        }
        column.setDefaultValue(unescape(defaultValue, "'", "''"));
    }
    return column;
}

From source file:org.jumpmind.symmetric.model.NodeGroupChannelWindow.java

public void setStartTime(Time startTime) {
    this.startTime = Time.valueOf(HHmmss.format(startTime));
}

From source file:org.jumpmind.symmetric.model.NodeGroupChannelWindow.java

public void setEndTime(Time endTime) {
    this.endTime = Time.valueOf(HHmmss.format(endTime));
}

From source file:org.jumpmind.symmetric.model.NodeGroupChannelWindow.java

public boolean inTimeWindow(Date date) {
    Time time = Time.valueOf(HHmmss.format(date));
    return inTimeWindow(time);
}

From source file:org.motechproject.server.svc.impl.RegistrarBeanImplTest.java

public void testAdjustDateBlackoutInTheMorning() {
    Calendar calendar = getCalendarWithTime(2, 13, 54);
    Date morningMessageTime = calendar.getTime();

    expect(contextService.getMotechService()).andReturn(motechService);
    expect(motechService.getBlackoutSettings())
            .andReturn(new Blackout(Time.valueOf("22:00:00"), Time.valueOf("06:00:00")));
    replay(contextService, adminService, motechService);

    Date preferredDate = registrarBean.adjustDateForBlackout(morningMessageTime);
    verify(contextService, adminService, motechService);

    Calendar preferredCalendar = getCalendar(preferredDate);

    assertFalse("Hour not updated",
            calendar.get(Calendar.HOUR_OF_DAY) == preferredCalendar.get(Calendar.HOUR_OF_DAY));
    assertEquals(6, preferredCalendar.get(Calendar.HOUR_OF_DAY));
    assertFalse("Minute not updated", calendar.get(Calendar.MINUTE) == preferredCalendar.get(Calendar.MINUTE));
    assertEquals(0, preferredCalendar.get(Calendar.MINUTE));
    assertFalse("Second not updated", calendar.get(Calendar.SECOND) == preferredCalendar.get(Calendar.SECOND));
    assertEquals(0, preferredCalendar.get(Calendar.SECOND));
}