Home > Back-end >  Set Date and Timestamp property value in Java
Set Date and Timestamp property value in Java

Time:05-25

I have class looking like this

class SomeClass {
  Date date;
  Timestamp timestamp;
}

What is the value of Date and Timestamp when I want to set property values like this:

SomeClass someClass = new SomeClass();

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

I have to insert those values in DB2 which has columns with types Date and Timestamp

CodePudding user response:

You have all the datatype DB2 mapping with Java here

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 code

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);
}

}

The output :

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

CodePudding user response:

You should be passing the same type of objects in the setter methods.

SomeClass someClass = new SomeClass();

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