![]() Lets convert a date string to the date data type instead of its. a small utility method to generate a string value of a date that can be inserted into an Oracle Database Date Field, abit dirty but works. Problem: Youd like to convert a string containing a date to the PostgreSQL date data type. Search Stack Overflow for posts on parsing a string as a ZonedDateTime. The strptime() is available in DateTime and time modules and is used for Date-Time Conversion. The approach described here assumes the input string was intended for UTC. Look to new methods added to the old classes. If your driver does not comply, convert to the legacy class. …or perhaps: tObject( …, ld, JDBCType.DATE ) If your JDBC driver complies with JDBC 4.2 or later, you can pass the LocalDate directly via PreparedStatement::setObject. With padding zeros on month and day-of-month we know the first 10 digits are the date. ![]() Your JDBC driver will handle the details of communicating that to the database.įirst extract your date value by calling String::substring. You will need to use LocalDate.format method like this: String str '' DateTimeFormatter formatter DateTimeFormatter.ofPattern ('MM/dd/yyyy') LocalDate dateTime LocalDate.parse (str, formatter) (dateTime. But here is something shorter.įirst of all, you should not be submitting a date-only value to a database as a string. The format of the string representation must match the specified format exactly. The “best” code is rarely the “smallest” code. Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. But I do not recommend actually crunching so much into a single line. To convert a string to a datetime in Python, you can use the strptime function from the datetime module. What could be the best possible and smallest code? ![]() I would recommend you to better use a PreparedStatement instead of a plain SQL INSERT statement so your statement is sanitized: PreparedStatement ps = cn.prepareStatement("INSERT INTO table VALUES(?, ?, ?)") Īnd then, insert your date as a Date object without the need of formatting into a String: ps.setDate(, d) ToDateTime (Convert String or Number to a Date or Timestamp) Argument is a fact, metric, column, or constant value that provides the strings that are converted. Then, if you are using plain statements, reformat it the way Oracle would accept it in an SQL INSERT command, using again a SimpleDateFormat object: String sd = new SimpleDateFormat("dd/MM/yyyy").format(d) Īnd insert it into your Statement: cn.createStatement().executeUpdate("INSERT INTO TABLE(.) VALUES(" + sd + ") Well, first, convert that String into a Date object using a SimpleDateFormat object: String s = " 00.00.00.0" ĭate d = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss.S").parse(s) OK, so you have a String with the value 00.00.00.0 and you want to insert that value in a DATE field inside a Oracle Database Table, using Java's JDBC?
0 Comments
Leave a Reply. |