Java Code Examples for java.sql.Date#valueOf()

The following examples show how to use java.sql.Date#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringUtils.java    From yyblog with MIT License 6 votes vote down vote up
public static Date stringToDate(String year, String month, String date) {

        if ((year != null) && (!year.equals("")) && (month != null)
                && (!month.equals("")) && (date != null) && (!date.equals(""))) {

            if (month.length() < 2) {
                month = "0" + month;
            }
            if (date.length() < 2) {
                date = "0" + date;
            }

            String string = year + "-" + month + "-" + date;

            return Date.valueOf(string);
        }
        return null;
    }
 
Example 2
Source File: DateTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test8() {
    Date d = Date.valueOf("1961-08-30");
    Date d2 = new Date(d.getTime());
    assertFalse(d.before(d2), "Error d.before(d2) = true");
    assertFalse(d2.before(d), "Error d2.before(d) = true");
}
 
Example 3
Source File: DateTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "validDateValues")
public void test00(String d, String expectedD) {
    Date d1 = Date.valueOf(d);
    Date d2 = Date.valueOf(expectedD);
    assertTrue(d1.equals(d2) && d2.equals(d1)
            && d1.toString().equals(expectedD), "Error d1 != d2");
}
 
Example 4
Source File: DateTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test14() {
    LocalDate ldt = LocalDate.now();
    Date d = Date.valueOf(ldt);
    assertTrue(ldt.equals(d.toLocalDate()),
            "Error LocalDate values are not equal");
}
 
Example 5
Source File: DateTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test14() {
    LocalDate ldt = LocalDate.now();
    Date d = Date.valueOf(ldt);
    assertTrue(ldt.equals(d.toLocalDate()),
            "Error LocalDate values are not equal");
}
 
Example 6
Source File: DateTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test8() {
    Date d = Date.valueOf("1961-08-30");
    Date d2 = new Date(d.getTime());
    assertFalse(d.before(d2), "Error d.before(d2) = true");
    assertFalse(d2.before(d), "Error d2.before(d) = true");
}
 
Example 7
Source File: DateTests.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test3() {
    Date d = Date.valueOf("1961-08-30");
    Date d2 = new Date(d.getTime());
    assertFalse(d.after(d2), "Error d.after(d2) = true");
}
 
Example 8
Source File: DateTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void test25() throws Exception {
    Date d = Date.valueOf("1961-08-30");
    d.setMinutes(0);
}
 
Example 9
Source File: DateTests.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test20() {

    Date d = Date.valueOf("1961-08-30");
    assertTrue(d.equals(d), "Error d != d");
}
 
Example 10
Source File: DateTests.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = UnsupportedOperationException.class)
public void test16() throws Exception {
    Date d = Date.valueOf("1961-08-30");
    Instant instant = d.toInstant();
}
 
Example 11
Source File: DateTests.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test10() {
    Date d = Date.valueOf("1961-08-30");
    Date d2 = new Date(d.getTime());
    assertTrue(d.compareTo(d2) == 0, "Error d.compareTo(d2) !=0");
}
 
Example 12
Source File: DateTests.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test11() {
    Date d = Date.valueOf("1961-08-30");
    Date d2 = new Date(System.currentTimeMillis());
    assertTrue(d.compareTo(d2) == -1, "Error d.compareTo(d2) != -1");
}
 
Example 13
Source File: DateTests.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test9() {
    Date d = Date.valueOf("1961-08-30");
    assertTrue(d.compareTo(d) == 0, "Error d.compareTo(d) !=0");
}
 
Example 14
Source File: DateTests.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test6() {
    Date d = Date.valueOf("1961-08-30");
    Date d2 = new Date(System.currentTimeMillis());
    assertTrue(d.before(d2), "Error d.before(d2) = false");
}
 
Example 15
Source File: DateTests.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test01() {
    Date d = Date.valueOf("1961-08-30");
    assertFalse(d.after(d), "Error d.after(d) = true");
}
 
Example 16
Source File: DateTests.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test10() {
    Date d = Date.valueOf("1961-08-30");
    Date d2 = new Date(d.getTime());
    assertTrue(d.compareTo(d2) == 0, "Error d.compareTo(d2) !=0");
}
 
Example 17
Source File: DateTests.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void test26() throws Exception {
    Date d = Date.valueOf("1961-08-30");
    d.setSeconds(0);
}
 
Example 18
Source File: DateTests.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void test15() throws Exception {
    LocalDate ld = null;
    Date.valueOf(ld);
}
 
Example 19
Source File: DateTests.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void test26() throws Exception {
    Date d = Date.valueOf("1961-08-30");
    d.setSeconds(0);
}
 
Example 20
Source File: CacheJdbcPojoStoreAbstractSelfTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * Check put in cache and store it in db.
 *
 * @throws Exception If failed.
 */
private void checkPutRemove() throws Exception {
    boolean binaryMarshaller = marshaller() instanceof BinaryMarshaller || marshaller() == null;

    IgniteCache<Object, Person> c1 = grid().cache(CACHE_NAME);

    Connection conn = getConnection();
    try {
        PreparedStatement stmt = conn.prepareStatement("SELECT ID, ORG_ID, BIRTHDAY, NAME, GENDER FROM PERSON WHERE ID = ?");

        stmt.setInt(1, -1);

        ResultSet rs = stmt.executeQuery();

        assertFalse("Unexpected non empty result set", rs.next());

        U.closeQuiet(rs);

        Date testDate = Date.valueOf("2001-05-05");
        Gender testGender = Gender.random();

        Person val = new Person(-1, -2, testDate, "Person-to-test-put-insert", 999, testGender);

        Object key = builtinKeys ? Integer.valueOf(-1) : new PersonKey(-1);

        // Test put-insert.
        c1.put(key, val);

        rs = stmt.executeQuery();

        assertTrue("Unexpected empty result set", rs.next());

        assertEquals(-1, rs.getInt(1));
        assertEquals(-2, rs.getInt(2));
        assertEquals(testDate, rs.getDate(3));
        assertEquals("Person-to-test-put-insert", rs.getString(4));

        assertEquals(testGender.toString(),
            binaryMarshaller ? Gender.values()[rs.getInt(5)].toString() : rs.getString(5));

        assertFalse("Unexpected more data in result set", rs.next());

        U.closeQuiet(rs);

        // Test put-update.
        testDate = Date.valueOf("2016-04-04");

        c1.put(key, new Person(-1, -3, testDate, "Person-to-test-put-update", 999, testGender));

        rs = stmt.executeQuery();

        assertTrue("Unexpected empty result set", rs.next());

        assertEquals(-1, rs.getInt(1));
        assertEquals(-3, rs.getInt(2));
        assertEquals(testDate, rs.getDate(3));
        assertEquals("Person-to-test-put-update", rs.getString(4));

        assertEquals(testGender.toString(),
            binaryMarshaller ? Gender.values()[rs.getInt(5)].toString() : rs.getString(5));

        assertFalse("Unexpected more data in result set", rs.next());

        // Test remove.
        c1.remove(key);

        rs = stmt.executeQuery();

        assertFalse("Unexpected non-empty result set", rs.next());

        U.closeQuiet(rs);
    }
    finally {
        U.closeQuiet(conn);
    }
}