If you are calling Oracle from Java via JDBC, then the best thing to do would probably be to use a prepared statement:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String lower = "2018-07-23 04:00:00";
String upper = "2018-07-23 04:03:02";
LocalDateTime lowerdt = LocalDateTime.parse(lower, formatter);
LocalDateTime upperdt = LocalDateTime.parse(upper, formatter);
String sql = "SELECT * FROM yourTable WHERE ts_col BETWEEN ? AND ?;";
Statement statement = conn.createStatement(sql);
statement.setTimestamp(1, Timestamp.valueOf(lowerdt));
statement.setTimestamp(2, Timestamp.valueOf(upperdt));
ResultSet rs = statement.executeQuery();
while (rs.next()) {
// process result set
}
This is the Java 8+ way of using a prepared statement to run a select with timestamps against Oracle. Not much needs explaining, except that to form the BETWEEN clause, we start out with just fairly standard string timestamps for the two points. Then, we parse into LocalDateTime, and finally bring those into java.sql.Timestamp.
Here is a link to a helpful GitHub page which shows JDBC with Java 8 and Oracle being used in a variety of different ways.
Edit: Here is a sample of how you might do the same as the above in Java 7:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String lower = "2018-07-23 04:00:00";
String upper = "2018-07-23 04:03:02";
Date lowerdt = formatter.parse(lower);
Date upperdt = formatter.parse(upper);
String sql = "SELECT * FROM yourTable WHERE ts_col BETWEEN ? AND ?;";
Statement statement = conn.createStatement(sql);
statement.setTimestamp(1, new java.sql.Timestamp(lowerdt.getTime()));
statement.setTimestamp(2, new java.sql.Timestamp(upperdt.getTime()));
ResultSet rs = statement.executeQuery();
while (rs.next()) {
// process result set
}
setTimestamp(); or will they be fixed values in the query? Incidentally, timestamp columns have an internal representation, it's your client that formats that to something readable like the values you've shown.