我想知道为什么setTime方法的行为与setDate完全一样,date没有时间,或者设置时间在2014-07-01 13:21:01它是在2014-07-01 00:00:00设置的? !?!
是否已弃用setTime? 我应该使用setTimestamp ???
答案 0 :(得分:3)
Oracle以外的数据库实际上区分了三种不同的数据类型:
DATE
只有约会,没有时间TIME
只有一天的时间,没有日期TIMESTAMP
both,date&时间。 JDBC尝试抽象标准SQL概念,上述三种数据类型由ANSI SQL定义,因此JDBC需要支持它们。
由于Oracle的日期始终包含时间,因此您 使用setTimestamp()
,否则将数据存储在数据库中时会丢失时间。
答案 1 :(得分:1)
setTime()方法:
java.util.Calendar.setTime(Date)方法使用给定日期设置Calendar的时间。
以下是java.util.Calendar.setTime()方法的声明
public final void setTime(Date date)
此方法不返回值。
示例: 以下示例显示了java.util.calendar.setTime()方法的用法。
package com.tutorialspoint;
import java.util.*;
public class CalendarDemo {
public static void main(String[] args) {
// create a calendar
Calendar cal = Calendar.getInstance();
// get the current time
System.out.println("Current time is :" + cal.getTime());
// create new date and set it
Date date = new Date(95, 10, 10);
cal.setTime(date);
// print the new time
System.out.println("After setting Time: " + cal.getTime());
}
}
SetTimestamp方法:
将指定参数设置为给定的时间戳和日历值。 语法
public void setTimestamp(java.lang.String sCol,
java.sql.Timestamp x,
java.util.Calendar c)