【问题标题】:What's the difference between setTime(...) and setTimestamp(...) in Hibernate Query?Hibernate Query 中的 setTime(...) 和 setTimestamp(...) 有什么区别?
【发布时间】:2014-07-01 11:24:00
【问题描述】:

我想知道为什么 setTime 方法的行为与 setDate 完全一样,没有时间的日期,或者将时间设置为 2014-07-01 13:21:01 设置为 2014-07-01 00:00:00 ? !?!

setTime 是否已弃用? 我应该使用 setTimestamp 吗???

【问题讨论】:

    标签: java oracle hibernate


    【解决方案1】:

    Oracle 以外的数据库实际上区分了三种不同的数据类型:

    • DATE 只有日期,没有时间
    • TIME只有一天中的时间,没有日期
    • TIMESTAMP 两者,日期和时间。

    JDBC试图抽象出标准的SQL概念,以上三种数据类型是ANSI SQL定义的,因此JDBC需要支持它们。

    由于 Oracle 的日期始终包含时间,因此您必须使用setTimestamp(),否则当您将时间存储在数据库中时会丢失时间。

    【讨论】:

      【解决方案2】:

      setTime() 方法:

      java.util.Calendar.setTime(Date) 方法将日历的时间设置为给定的日期。

      以下是 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());
          }
       }
      

      设置时间戳方法:

      将指定参数设置为给定的时间戳和日历值。 语法

                public void setTimestamp(java.lang.String sCol,
                           java.sql.Timestamp x,
                           java.util.Calendar c)
      

      【讨论】:

      • 我猜这个问题不是关于Calendar.setTime(),而是关于PreparedStatement.setTime()
      猜你喜欢
      • 2014-07-26
      • 1970-01-01
      • 2011-08-06
      • 2021-09-11
      • 1970-01-01
      • 2011-11-18
      • 2011-08-17
      • 2011-12-27
      相关资源
      最近更新 更多