在 Java 中设置日期和时间戳 属性 值

Set Date and Timestamp property value in Java

我 class 看起来像这样

class SomeClass {
  Date date;
  Timestamp timestamp;
}

当我想像这样设置 属性 值时,日期和时间戳的值是多少:

SomeClass someClass = new SomeClass();

someClass.setDate(what value goes here?);
someClass.setTimestamp(what value goes here?)

我必须在 DB2 中插入这些值,其中的列类型为 Date 和 Timestamp

您应该在 setter 方法中传递相同类型的对象。

SomeClass someClass = new SomeClass();

someClass.setDate(new Date()); //
someClass.setTimestamp(new TimeStamp(LONG VALUE));

您在此处具有 Java 的所有数据类型 DB2 映射

https://www.ibm.com/docs/en/db2-for-zos/12?topic=jsri-data-types-that-map-database-data-types-in-java-applications

java.sql.Date       DATE
java.sql.Time       TIME
java.sql.Timestamp  TIMESTAMP, TIMESTAMP(p), TIMESTAMP WITH TIME ZONE, 
                    TIMESTAMP(p) WITH TIME ZONE15,1

Java代码

import java.sql.Date;
import java.sql.Timestamp;

public class Test {

public static void main(String[] args) {

    Date dt = new Date(System.currentTimeMillis());
    Timestamp ts = new Timestamp(System.currentTimeMillis());
    
    System.err.println("current date "+dt);
    System.err.println("current timestamps "+ts);
}

}

输出:

current date 2022-05-25
current timestamps 2022-05-25 11:45:12.858