Example usage for java.sql Time toString

List of usage examples for java.sql Time toString

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public String toString() 

Source Link

Document

Formats a time in JDBC time escape format.

Usage

From source file:Main.java

public static void main(String[] args) {
    java.util.Date javaDate = new java.util.Date();
    long javaTime = javaDate.getTime();
    System.out.println("The Java Date is: " + javaDate.toString());

    java.sql.Date sqlDate = new java.sql.Date(javaTime);
    System.out.println("The SQL DATE is: " + sqlDate.toString());

    java.sql.Time sqlTime = new java.sql.Time(javaTime);
    System.out.println("The SQL TIME is: " + sqlTime.toString());

    java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(javaTime);
    System.out.println("The SQL TIMESTAMP is: " + sqlTimestamp.toString());
}

From source file:Main.java

public static void main(String[] args) {
    java.util.Date javaDate = new java.util.Date();
    long javaTime = javaDate.getTime();
    System.out.println("The Java Date is:" + javaDate.toString());

    // SQL DATE//from  ww  w . j  av a 2 s .  co  m
    java.sql.Date sqlDate = new java.sql.Date(javaTime);
    System.out.println("The SQL DATE is: " + sqlDate.toString());

    // SQL TIME
    java.sql.Time sqlTime = new java.sql.Time(javaTime);
    System.out.println("The SQL TIME is: " + sqlTime.toString());

    // SQL TIMESTAMP
    java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(javaTime);
    System.out.println("The SQL TIMESTAMP is: " + sqlTimestamp.toString());
}

From source file:CSVWriter.java

private static String getColumnValue(ResultSet rs, int colType, int colIndex)
    throws SQLException, IOException {

  String value = "";
      //  w w w . ja  v a2  s .c  o  m
switch (colType)
{
  case Types.BIT:
    Object bit = rs.getObject(colIndex);
    if (bit != null) {
      value = String.valueOf(bit);
    }
  break;
  case Types.BOOLEAN:
    boolean b = rs.getBoolean(colIndex);
    if (!rs.wasNull()) {
      value = Boolean.valueOf(b).toString();
    }
  break;
  case Types.CLOB:
    Clob c = rs.getClob(colIndex);
    if (c != null) {
      value = read(c);
    }
  break;
  case Types.BIGINT:
  case Types.DECIMAL:
  case Types.DOUBLE:
  case Types.FLOAT:
  case Types.REAL:
  case Types.NUMERIC:
    BigDecimal bd = rs.getBigDecimal(colIndex);
    if (bd != null) {
      value = "" + bd.doubleValue();
    }
  break;
  case Types.INTEGER:
  case Types.TINYINT:
  case Types.SMALLINT:
    int intValue = rs.getInt(colIndex);
    if (!rs.wasNull()) {
      value = "" + intValue;
    }
  break;
  case Types.JAVA_OBJECT:
    Object obj = rs.getObject(colIndex);
    if (obj != null) {
      value = String.valueOf(obj);
    }
  break;
  case Types.DATE:
    java.sql.Date date = rs.getDate(colIndex);
    if (date != null) {
      value = DATE_FORMATTER.format(date);;
    }
  break;
  case Types.TIME:
    Time t = rs.getTime(colIndex);
    if (t != null) {
      value = t.toString();
    }
  break;
  case Types.TIMESTAMP:
    Timestamp tstamp = rs.getTimestamp(colIndex);
    if (tstamp != null) {
      value = TIMESTAMP_FORMATTER.format(tstamp);
    }
  break;
  case Types.LONGVARCHAR:
  case Types.VARCHAR:
  case Types.CHAR:
    value = rs.getString(colIndex);
  break;
  default:
    value = "";
}

    
if (value == null)
{
  value = "";
}
    
return value;
      
}

From source file:com.microsoft.sqlserver.testframework.sqlType.SqlTime.java

public Object createdata() {
    Time temp = new Time(
            ThreadLocalRandom.current().nextLong(((Time) minvalue).getTime(), ((Time) maxvalue).getTime()));
    String timeNano = temp.toString() + "." + RandomStringUtils.randomNumeric(this.precision);
    // can pass String rather than converting to loacTime, but leaving it
    // unchanged for now to handle prepared statements
    return LocalTime.parse(timeNano, formatter);
}

From source file:com.hihframework.core.utils.DateUtils.java

public static String getCurrTime() {
    Date currdate = getTodayDate();
    java.sql.Time tt = new java.sql.Time(currdate.getTime());

    return tt.toString();
}

From source file:com.hihframework.core.utils.DateUtils.java

public static String getDateTimeString(java.sql.Date dd) {
    if (dd == null) {
        return "";
    }//from  ww  w .j a v  a 2  s  .  co  m

    String tds = dd.toString();
    java.sql.Time tt = new java.sql.Time(dd.getTime());
    tds += (" " + tt.toString());

    return tds;
}

From source file:CSVWriter.java

private String handleTime(Time time) {
    return time == null ? null : time.toString();
}

From source file:main.UIController.java

public void showDateOfToday(boolean useCacheSunset) {
    Preferences data = this.getCurrentPreferences();
    String city = data.get(FIELD_CITY, DEF_CITY);
    String country = data.get(FIELD_COUNTRY, DEF_COUNTRY);
    TimeZone tz = TimeZone.getTimeZone(data.get(FIELD_TIMEZONE, DEF_TIMEZONE));
    GregorianCalendar gcal = new GregorianCalendar(tz);
    Time time = this.calculateSunsetForActualLocation(gcal, useCacheSunset);
    ImladrisCalendar cal;//  w w w .ja v  a  2  s  .co  m
    String sunsetStr = "";
    String locationInfo = "";
    if (time == null) {
        cal = new ImladrisCalendar(gcal);
    } else {
        cal = new ImladrisCalendar(time, gcal);
        /*check if before sunset*/
        String gtimeStr = gcal.get(GregorianCalendar.HOUR_OF_DAY) + ":" + gcal.get(GregorianCalendar.MINUTE)
                + ":" + gcal.get(GregorianCalendar.SECOND);
        Time gtime = Time.valueOf(gtimeStr);
        if (gtime.before(time)) { // before sunset
            sunsetStr = " - before sunset";
        } else { // after/during sunset
            sunsetStr = " - after sunset";
        }
        String sunsetTimeStr = time.toString();
        sunsetTimeStr = sunsetTimeStr.substring(0, sunsetTimeStr.length() - 3);
        sunsetStr += " " + Lang.punctuation.parenthesis_open + "occurring at " + sunsetTimeStr
                + Lang.punctuation.parenthesis_close;
        locationInfo = this.makeLocationString(city, country);
        if (locationInfo.length() > 0) {
            locationInfo = Lang.punctuation.parenthesis_open + Lang.common.location_label
                    + Lang.punctuation.double_dot + " " + locationInfo + " " + Lang.punctuation.pipe + " "
                    + Lang.common.timezone_label + Lang.punctuation.double_dot + " "
                    + data.get(FIELD_TIMEZONE, DEF_TIMEZONE) + Lang.punctuation.parenthesis_close;
        }
    }
    String gstr = this.gregorianToString(gcal);
    String istr = cal.toString();
    UI ui = this.getUi();
    ui.getTodayGregorian().setText(gstr + sunsetStr);
    ui.getTodayImladris().setText(istr);
    ui.getLocationInfo().setText(locationInfo);
}

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

@Test
public void testDatetime() throws SQLException {
    trace("test DATETIME");
    ResultSet rs;//ww  w.ja  v a 2s  .  com
    Object o;

    // rs = stat.executeQuery("call date '99999-12-23'");
    // rs.next();
    // assertEquals("99999-12-23", rs.getString(1));
    // rs = stat.executeQuery("call timestamp '99999-12-23 01:02:03.000'");
    // rs.next();
    // assertEquals("99999-12-23 01:02:03.0", rs.getString(1));
    // rs = stat.executeQuery("call date '-99999-12-23'");
    // rs.next();
    // assertEquals("-99999-12-23", rs.getString(1));
    // rs = stat.executeQuery("call timestamp '-99999-12-23 01:02:03.000'");
    // rs.next();
    // assertEquals("-99999-12-23 01:02:03.0", rs.getString(1));

    stat = conn.createStatement();
    // stat.execute("CREATE TABLE test(ID INT PRIMARY KEY,VALUE DATETIME)");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (1,'2011-11-11 0:0:0', 13, 'testDatetime')");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (2,'2002-02-02 02:02:02', 13, 'testDatetime')");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (3,'1800-01-01 0:0:0', 13, 'testDatetime')");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (4,'9999-12-31 23:59:59', 13, 'testDatetime')");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (5,'9999-12-31 23:59:59', 13, 'testDatetime')");
    // stat.execute("INSERT INTO test (column1,column6,column2,column3) VALUES(5,NULL)");
    rs = stat.executeQuery("SELECT column1,column6 FROM test where column3='testDatetime' ORDER BY column1");
    // assertResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] {
    // Types.INTEGER, Types.TIMESTAMP }, new int[] { 10, 23 }, new int[] { 0,
    // 10 });
    // rs = stat.executeQuery("SELECT * FROM test ORDER BY ID");
    // assertResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] {
    // Types.INTEGER, Types.TIMESTAMP }, new int[] { 10, 23 }, new int[] { 0,
    // 10 });
    rs.next();
    java.sql.Date date;
    java.sql.Time time;
    Timestamp ts;
    date = rs.getDate(2);
    assertTrue(!rs.wasNull());
    time = rs.getTime(2);
    assertTrue(!rs.wasNull());
    ts = rs.getTimestamp(2);
    assertTrue(!rs.wasNull());
    trace("Date: " + date.toString() + " Time:" + time.toString() + " Timestamp:" + ts.toString());
    trace("Date ms: " + date.getTime() + " Time ms:" + time.getTime() + " Timestamp ms:" + ts.getTime());
    trace("1970 ms: " + Timestamp.valueOf("1970-01-01 00:00:00.0").getTime());
    assertEquals(Timestamp.valueOf("2011-11-11 00:00:00.0").getTime(), date.getTime());
    assertEquals(Timestamp.valueOf("1970-01-01 00:00:00.0").getTime(), time.getTime());
    assertEquals(Timestamp.valueOf("2011-11-11 00:00:00.0").getTime(), ts.getTime());
    assertTrue(date.equals(java.sql.Date.valueOf("2011-11-11")));
    assertTrue(time.equals(java.sql.Time.valueOf("00:00:00")));
    assertTrue(ts.equals(Timestamp.valueOf("2011-11-11 00:00:00.0")));
    assertFalse(rs.wasNull());
    o = rs.getObject(2);
    trace(o.getClass().getName());
    assertTrue(o instanceof Timestamp);
    assertTrue(((Timestamp) o).equals(Timestamp.valueOf("2011-11-11 00:00:00")));
    assertFalse(rs.wasNull());
    rs.next();
    date = rs.getDate("COLUMN6");
    assertTrue(!rs.wasNull());
    time = rs.getTime("COLUMN6");
    assertTrue(!rs.wasNull());
    ts = rs.getTimestamp("COLUMN6");
    assertTrue(!rs.wasNull());
    trace("Date: " + date.toString() + " Time:" + time.toString() + " Timestamp:" + ts.toString());
    assertEquals("2002-02-02", date.toString());
    assertEquals("02:02:02", time.toString());
    assertEquals("2002-02-02 02:02:02.0", ts.toString());
    rs.next();
    assertEquals("1800-01-01", rs.getDate("column6").toString());
    assertEquals("00:00:00", rs.getTime("column6").toString());
    assertEquals("1800-01-01 00:00:00.0", rs.getTimestamp("column6").toString());
    rs.next();
    assertEquals("9999-12-31", rs.getDate("Column6").toString());
    assertEquals("23:59:59", rs.getTime("Column6").toString());
    assertEquals("9999-12-31 23:59:59.0", rs.getTimestamp("Column6").toString());
    // assertTrue(!rs.next());
}

From source file:org.apache.ojb.broker.accesslayer.conversions.TimeList2VarcharFieldConversion.java

public Object javaToSql(Object source) throws ConversionException {
    if (source == null) {
        return NULLVALUE;
    }//from  www  .  j av  a  2s  .c o  m

    try {
        List timeList = (List) source;
        if (timeList.isEmpty()) {
            return NULLVALUE;
        }

        StringBuffer result = new StringBuffer();
        for (int i = 0; i < timeList.size(); i++) {
            Time obj = (Time) timeList.get(i);
            String newSt = obj.toString();
            // introduced in JDK 1.4, replace with commons-lang
            // newSt = newSt.replaceAll("#", "##");
            newSt = StringUtils.replace(newSt, "#", "##");
            result.append(newSt);
            result.append("#");
        }
        return result.toString();
    } catch (ClassCastException e) {
        throw new ConversionException("Object is not a List of Time it is a" + source.getClass().getName());
    }
}