Example usage for java.sql Date valueOf

List of usage examples for java.sql Date valueOf

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public static Date valueOf(LocalDate date) 

Source Link

Document

Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate .

Usage

From source file:adalid.core.programmers.AbstractJavaProgrammer.java

protected String getString(Object object, Class<?> type) {
    if (object == null || type == null) {
        return null;
    }//  w  w  w  .j  a va 2s . c  om
    String errmsg = "cannot get \"" + javaLangLess(type) + "\" from \"" + object + "\"";
    String string = getString(object);
    try {
        if (string == null) {
            return null;
        } else if (Boolean.class.isAssignableFrom(type)) {
            return "" + Boolean.valueOf(string);
        } else if (Character.class.isAssignableFrom(type)) {
            return getCharacterString(string);
        } else if (String.class.isAssignableFrom(type)) {
            return string;
        } else if (Byte.class.isAssignableFrom(type)) {
            return "" + Byte.valueOf(string);
        } else if (Short.class.isAssignableFrom(type)) {
            return "" + Short.valueOf(string);
        } else if (Integer.class.isAssignableFrom(type)) {
            return "" + Integer.valueOf(string);
        } else if (Long.class.isAssignableFrom(type)) {
            return "" + Long.valueOf(string);
        } else if (Float.class.isAssignableFrom(type)) {
            return "" + Float.valueOf(string);
        } else if (Double.class.isAssignableFrom(type)) {
            return "" + Double.valueOf(string);
        } else if (BigInteger.class.isAssignableFrom(type)) {
            return "" + new BigInteger(string);
        } else if (BigDecimal.class.isAssignableFrom(type)) {
            return "" + new BigDecimal(string);
        } else if (object instanceof java.util.Date && Date.class.isAssignableFrom(type)) {
            string = TimeUtils.jdbcDateString(object);
            return getString(Date.valueOf(string));
        } else if (object instanceof java.util.Date && Time.class.isAssignableFrom(type)) {
            string = TimeUtils.jdbcTimeString(object);
            return getString(Time.valueOf(string));
        } else if (object instanceof java.util.Date && Timestamp.class.isAssignableFrom(type)) {
            string = TimeUtils.jdbcTimestampString(object);
            return getString(Timestamp.valueOf(string));
        } else {
            return null;
        }
    } catch (IllegalArgumentException ex) {
        //          logger.error(errmsg, ThrowableUtils.getCause(ex));
        logger.error(errmsg);
        return null;
    }
}

From source file:org.kuali.kfs.gl.businessobject.lookup.CurrentAccountBalanceLookupableHelperServiceTest.java

private Account generateAccount() {
    Account account = AccountFixture.ACCOUNT_PRESENCE_ACCOUNT.createAccount();
    account.setAccountExpirationDate(Date.valueOf(ACCOUNT_EXPIRATION_DATE));
    account.setActive(true);//  w  w  w . ja v  a 2s .  c o  m
    account.setAccountNumber(testDataGenerator.getPropertyValue(KFSPropertyConstants.ACCOUNT_NUMBER));
    account.setChartOfAccountsCode(pendingEntry.getChartOfAccountsCode());
    account.setOrganizationCode(optionalFieldToValueMap.get(ORGINIZATION_CODE_KEY));

    Person accountFiscalOfficerUser = personService
            .getPersonByPrincipalName(semiRequiredFieldToValueMap.get(FISCAL_OFFICER_PRINCIPAL_NAME_KEY));
    account.setAccountFiscalOfficerSystemIdentifier(accountFiscalOfficerUser.getPrincipalId());
    account.setAccountFiscalOfficerUser(accountFiscalOfficerUser);

    Person accountSupervisoryUser = personService
            .getPersonByPrincipalName(semiRequiredFieldToValueMap.get(ACCOUNT_SUPERVISOR_PRINCIPAL_NAME_KEY));
    account.setAccountFiscalOfficerSystemIdentifier(accountSupervisoryUser.getPrincipalId());
    account.setAccountSupervisoryUser(accountSupervisoryUser);

    return account;
}

From source file:org.apache.hadoop.hive.ql.io.orc.RecordReaderImpl.java

private static Object getBaseObjectForComparison(PredicateLeaf.Type type, Object obj) {
    if (obj == null) {
        return null;
    }//ww w .j a v  a  2 s .  c o m
    switch (type) {
    case BOOLEAN:
        if (obj instanceof Boolean) {
            return obj;
        } else {
            // will only be true if the string conversion yields "true", all other values are
            // considered false
            return Boolean.valueOf(obj.toString());
        }
    case DATE:
        if (obj instanceof Date) {
            return obj;
        } else if (obj instanceof String) {
            return Date.valueOf((String) obj);
        } else if (obj instanceof Timestamp) {
            return DateWritable.timeToDate(((Timestamp) obj).getTime() / 1000L);
        }
        // always string, but prevent the comparison to numbers (are they days/seconds/milliseconds?)
        break;
    case DECIMAL:
        if (obj instanceof Boolean) {
            return new HiveDecimalWritable(((Boolean) obj).booleanValue() ? HiveDecimal.ONE : HiveDecimal.ZERO);
        } else if (obj instanceof Integer) {
            return new HiveDecimalWritable(((Integer) obj).intValue());
        } else if (obj instanceof Long) {
            return new HiveDecimalWritable(((Long) obj));
        } else if (obj instanceof Float || obj instanceof Double || obj instanceof String) {
            return new HiveDecimalWritable(obj.toString());
        } else if (obj instanceof BigDecimal) {
            return new HiveDecimalWritable(HiveDecimal.create((BigDecimal) obj));
        } else if (obj instanceof HiveDecimal) {
            return new HiveDecimalWritable((HiveDecimal) obj);
        } else if (obj instanceof HiveDecimalWritable) {
            return obj;
        } else if (obj instanceof Timestamp) {
            return new HiveDecimalWritable(
                    new Double(new TimestampWritable((Timestamp) obj).getDouble()).toString());
        }
        break;
    case FLOAT:
        if (obj instanceof Number) {
            // widening conversion
            return ((Number) obj).doubleValue();
        } else if (obj instanceof HiveDecimal) {
            return ((HiveDecimal) obj).doubleValue();
        } else if (obj instanceof String) {
            return Double.valueOf(obj.toString());
        } else if (obj instanceof Timestamp) {
            return new TimestampWritable((Timestamp) obj).getDouble();
        } else if (obj instanceof HiveDecimal) {
            return ((HiveDecimal) obj).doubleValue();
        } else if (obj instanceof BigDecimal) {
            return ((BigDecimal) obj).doubleValue();
        }
        break;
    case INTEGER:
        // fall through
    case LONG:
        if (obj instanceof Number) {
            // widening conversion
            return ((Number) obj).longValue();
        } else if (obj instanceof HiveDecimal) {
            return ((HiveDecimal) obj).longValue();
        } else if (obj instanceof String) {
            return Long.valueOf(obj.toString());
        }
        break;
    case STRING:
        if (obj != null) {
            return (obj.toString());
        }
        break;
    case TIMESTAMP:
        if (obj instanceof Timestamp) {
            return obj;
        } else if (obj instanceof Integer) {
            return TimestampWritable.longToTimestamp(((Number) obj).longValue(), false);
        } else if (obj instanceof Float) {
            return TimestampWritable.doubleToTimestamp(((Float) obj).doubleValue());
        } else if (obj instanceof Double) {
            return TimestampWritable.doubleToTimestamp(((Double) obj).doubleValue());
        } else if (obj instanceof HiveDecimal) {
            return TimestampWritable.decimalToTimestamp((HiveDecimal) obj);
        } else if (obj instanceof HiveDecimalWritable) {
            return TimestampWritable.decimalToTimestamp(((HiveDecimalWritable) obj).getHiveDecimal());
        } else if (obj instanceof Date) {
            return new Timestamp(((Date) obj).getTime());
        }
        // float/double conversion to timestamp is interpreted as seconds whereas integer conversion
        // to timestamp is interpreted as milliseconds by default. The integer to timestamp casting
        // is also config driven. The filter operator changes its promotion based on config:
        // "int.timestamp.conversion.in.seconds". Disable PPD for integer cases.
        break;
    default:
        break;
    }

    throw new IllegalArgumentException(String.format("ORC SARGS could not convert from %s to %s",
            obj == null ? "(null)" : obj.getClass().getSimpleName(), type));
}

From source file:org.rti.zcore.dar.transfer.access.ImportMshData.java

private static EncounterData createApppointment(Connection conn, Long siteId, MshPatientMaster mshPatientMaster)
        throws NumberFormatException, Exception {
    Form formDef = null;//from   ww w. ja  v a 2s.com
    Long formId = (Long) DynaSiteObjects.getFormNameMap().get("Appointment");
    formDef = (Form) DynaSiteObjects.getForms().get(new Long(formId));
    Appointment formData = new Appointment();
    formData.setSiteId(siteId);
    formData.setCreatedBy("zepadmin");
    formData.setFlowId(formDef.getFlowId());
    formData.setFormId(formId);
    formData.setPatientId(mshPatientMaster.getPatientId());
    formData.setSessionPatient(mshPatientMaster.getSessionPatient());
    formData.setEvent(mshPatientMaster.getEvent());
    formData.setEventId(mshPatientMaster.getEventId());
    formData.setEventUuid(mshPatientMaster.getEventUuid());

    Integer dateOffset = mshPatientMaster.getDaysToNextAppointment();
    Date dateVisit = null;
    Date initialvisit = DateUtils.toDateSql(mshPatientMaster.getDateTherapyStarted());
    Date dateNextAppt = DateUtils.toDateSql(mshPatientMaster.getDateOfNextAppointment());
    if ((dateNextAppt != null) && (dateOffset != null)) {
        String datePastVisitStr = DateUtils.createFutureDate(dateNextAppt, 0 - dateOffset);
        Date datePastVisit = Date.valueOf(datePastVisitStr);
        if (datePastVisit.getTime() > initialvisit.getTime()) {
            dateVisit = datePastVisit;
        } else {
            dateVisit = initialvisit;
        }
    } else {
        dateVisit = initialvisit;
    }
    formData.setDateVisit(dateVisit);
    if (dateVisit != null) {
        Timestamp ts = new Timestamp(dateVisit.getTime());
        formData.setCreated(ts);
        formData.setLastModified(ts);
    }
    formData.setAppointment_date(dateNextAppt);
    EncounterData enc = null;
    if (dateNextAppt != null) {
        enc = FormDAO.create(conn, formData, formData.getCreatedBy(), formData.getSiteId(), formDef,
                formData.getFlowId(), null);
    }
    return enc;
}

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

public void testObject() throws SQLException {
    Statement stat = conn.createStatement();
    ResultSet rs;/*w ww  . j a  va  2 s.co  m*/
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
    stat.execute("INSERT INTO TEST VALUES(1, 'Hello')");
    PreparedStatement prep = conn
            .prepareStatement("SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM TEST");
    prep.setObject(1, Boolean.TRUE);
    prep.setObject(2, "Abc");
    prep.setObject(3, new BigDecimal("10.2"));
    prep.setObject(4, new Byte((byte) 0xff));
    prep.setObject(5, new Short(Short.MAX_VALUE));
    prep.setObject(6, new Integer(Integer.MIN_VALUE));
    prep.setObject(7, new Long(Long.MAX_VALUE));
    prep.setObject(8, new Float(Float.MAX_VALUE));
    prep.setObject(9, new Double(Double.MAX_VALUE));
    prep.setObject(10, Date.valueOf("2001-02-03"));
    prep.setObject(11, Time.valueOf("04:05:06"));
    prep.setObject(12, Timestamp.valueOf("2001-02-03 04:05:06.123456789"));
    prep.setObject(13, new java.util.Date(Date.valueOf("2001-02-03").getTime()));
    prep.setObject(14, new byte[] { 10, 20, 30 });
    prep.setObject(15, new Character('a'), Types.OTHER);
    prep.setObject(16, "2001-01-02", Types.DATE);
    // converting to null seems strange...
    prep.setObject(17, "2001-01-02", Types.NULL);
    prep.setObject(18, "3.725", Types.DOUBLE);
    prep.setObject(19, "23:22:21", Types.TIME);
    prep.setObject(20, new java.math.BigInteger("12345"), Types.OTHER);
    rs = prep.executeQuery();
    rs.next();
    assertTrue(rs.getObject(1).equals(Boolean.TRUE));
    assertTrue(rs.getObject(2).equals("Abc"));
    assertTrue(rs.getObject(3).equals(new BigDecimal("10.2")));
    assertTrue(rs.getObject(4).equals((byte) 0xff));
    assertTrue(rs.getObject(5).equals(new Short(Short.MAX_VALUE)));
    assertTrue(rs.getObject(6).equals(new Integer(Integer.MIN_VALUE)));
    assertTrue(rs.getObject(7).equals(new Long(Long.MAX_VALUE)));
    assertTrue(rs.getObject(8).equals(new Float(Float.MAX_VALUE)));
    assertTrue(rs.getObject(9).equals(new Double(Double.MAX_VALUE)));
    assertTrue(rs.getObject(10).equals(Date.valueOf("2001-02-03")));
    assertEquals("04:05:06", rs.getObject(11).toString());
    assertTrue(rs.getObject(11).equals(Time.valueOf("04:05:06")));
    assertTrue(rs.getObject(12).equals(Timestamp.valueOf("2001-02-03 04:05:06.123456789")));
    assertTrue(rs.getObject(13).equals(Timestamp.valueOf("2001-02-03 00:00:00")));
    assertEquals(new byte[] { 10, 20, 30 }, (byte[]) rs.getObject(14));
    assertTrue(rs.getObject(15).equals('a'));
    assertTrue(rs.getObject(16).equals(Date.valueOf("2001-01-02")));
    assertTrue(rs.getObject(17) == null && rs.wasNull());
    assertTrue(rs.getObject(18).equals(new Double(3.725)));
    assertTrue(rs.getObject(19).equals(Time.valueOf("23:22:21")));
    assertTrue(rs.getObject(20).equals(new java.math.BigInteger("12345")));

    // } else if(x instanceof java.io.Reader) {
    // return session.createLob(Value.CLOB,
    // TypeConverter.getInputStream((java.io.Reader)x), 0);
    // } else if(x instanceof java.io.InputStream) {
    // return session.createLob(Value.BLOB, (java.io.InputStream)x, 0);
    // } else {
    // return ValueBytes.get(TypeConverter.serialize(x));

    stat.execute("DROP TABLE TEST");

}

From source file:us.mn.state.health.lims.common.util.SystemConfiguration.java

public String getPatternForDateLocale() {
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, getDateLocale());
    // yyyy/mm/dd
    Date date = Date.valueOf("2000-01-02");
    String pattern = dateFormat.format(date);
    pattern = pattern.replaceFirst("01", "MM");
    pattern = pattern.replaceFirst("02", "DD");
    pattern = pattern.replaceFirst("00", "YYYY");
    return pattern;
}

From source file:com.continuent.tungsten.common.mysql.MySQLPacket.java

/**
 * Reads a date from stream given its length and returns the corresponding
 * {@link Date}//  w w w . ja v a2s  .co  m
 * 
 * @param length size of the date data
 * @return jdbc Date decoded from stream
 */
public Date getDate(int length) {
    int year = 0;
    int month = 0;
    int day = 0;
    if (length >= 4) {
        year = getUnsignedShort();
        if (logger.isTraceEnabled())
            logger.trace("year=" + year);
        month = getByte();
        if (logger.isTraceEnabled())
            logger.trace("month=" + month);
        day = getByte();
        if (logger.isTraceEnabled())
            logger.trace("day=" + day);
    }
    // This is the easier way, probably not the fastest
    String date = "" + year + "-" + month + "-" + day;
    return Date.valueOf(date);
}

From source file:org.ojbc.adapters.analyticsstaging.custody.dao.AnalyticalDatastoreDAOImpl.java

private void saveCustodyRelease(Integer bookingId, String bookingNumber, LocalDate releaseDate,
        LocalTime releaseTime, String releaseCondition) {

    final String sql = "Insert into CustodyRelease (BookingID, BookingNumber, "
            + "ReleaseDate, ReleaseTime, ReleaseCondition) "
            + "values (:bookingId, :bookingNumber, :releaseDate, :releaseTime, :releaseCondition)";

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("releaseDate", Date.valueOf(releaseDate));
    params.put("releaseTime", Time.valueOf(releaseTime));
    params.put("bookingId", bookingId);
    params.put("bookingNumber", bookingNumber);
    params.put("releaseCondition", releaseCondition);

    namedParameterJdbcTemplate.update(sql, params);
}

From source file:com.ichi2.libanki.Utils.java

/**
 *  Returns the effective date of the present moment.
 *  If the time is prior the cut-off time (9:00am by default as of 11/02/10) return yesterday,
 *  otherwise today//from   w w w.ja v a2 s  .c  om
 *  Note that the Date class is java.sql.Date whose constructor sets hours, minutes etc to zero
 *
 * @param utcOffset The UTC offset in seconds we are going to use to determine today or yesterday.
 * @return The date (with time set to 00:00:00) that corresponds to today in Anki terms
 */
public static Date genToday(double utcOffset) {
    // The result is not adjusted for timezone anymore, following libanki model
    // Timezone adjustment happens explicitly in Deck.updateCutoff(), but not in Deck.checkDailyStats()
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    cal.setTimeInMillis(System.currentTimeMillis() - (long) utcOffset * 1000l);
    return Date.valueOf(df.format(cal.getTime()));
}

From source file:com.hichinaschool.flashcards.libanki.Utils.java

/**
 *  Returns the effective date of the present moment.
 *  If the time is prior the cut-off time (9:00am by default as of 11/02/10) return yesterday,
 *  otherwise today//from ww w .ja  v a 2s.  c om
 *  Note that the Date class is java.sql.Date whose constructor sets hours, minutes etc to zero
 *
 * @param utcOffset The UTC offset in seconds we are going to use to determine today or yesterday.
 * @return The date (with time set to 00:00:00) that corresponds to today in Anki terms
 */
public static Date genToday(double utcOffset) {
    // The result is not adjusted for timezone anymore, following libanki model
    // Timezone adjustment happens explicitly in Deck.updateCutoff(), but not in Deck.checkDailyStats()
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    cal.setTimeInMillis(System.currentTimeMillis() - (long) utcOffset * 1000l);
    Date today = Date.valueOf(df.format(cal.getTime()));
    return today;
}