简体   繁体   中英

How to get the TIMESTAMP WITH TIME ZONE column value and rows from oracle with where clause query in oracle?

I have a table with column type as TIMESTAMP WITH TIME ZONE. I want to read the rows SELECT * FROM "TEST_TIMESTAMP_WTZ_t2" where time_with_zone = ?;

String d = "2021-10-18 16:11:10.0 Europe/Paris";
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n VV");
ZonedDateTime tm = ZonedDateTime.parse(d, dtf2); 
// OffsetDateTime odt =OffsetDateTime.parse(d,dtf2); 
// String z = tm.getZone().getId();
// System.out.println(tm.getZone());
// ThIS IS THE MAIN CODE 
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); 
// Calendar c = Calendar.getInstance(); 
// c.setTime(dateFormat.parse(d));
// c.setTimeZone(TimeZone.getTimeZone(z)); 
// Date date = dateFormat.parse(d); 
// String parsed = dateFormat.format(date); 
// Timestamp timestamp = new Timestamp(c.getTimeInMillis()); 
// Datum dtm = new TIMESTAMPTZ(con, new java.sql.Timestamp(c.getTimeInMillis()), c); 
// Datum dtm = new TIMESTAMPTZ(con, new java.sql.Timestamp(c.getTimeInMillis()), tm.getZone());
// PreparedStatement ps = con.prepareStatement(query);
// ps.setObject(1,dtm); 
// ResultSet rs = ps.executeQuery();
// Calendar c = Calendar.getInstance(); 
// while ((rs.next())) { 
//  System.out.println(rs.getObject(2)); 
// }

the date is present in DB but I am not able to get based on the where clause.

Can someone help me to correct me what I am doing wrong?

I suggest you not to mix the modern Date-Time API with the legacy one. The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API * .

Using java.time , the modern Date-Time API, given below is how you can extract the data from a field of type, TIMESTAMP WITH TIMEZONE :

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM mytable WHERE <some condition>");
while (rs.next()) {
    // Assuming the column index of columnfoo is 1
    OffsetDateTime odt = rs.getObject(1, OffsetDateTime.class));
    System.out.println(odt);
}
rs.close();
st.close();

where columnfoo is of type, TIMESTAMP WITH TIMEZONE .


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring . Note that Android 8.0 Oreo already provides support for java.time .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM