Home >Database >Mysql Tutorial >How to Convert 'EEE MMM dd HH:mm:ss ZZZ yyyy' to a MySQL-Compatible java.sql.Date?
Convert "EEE MMM dd HH:mm:ss ZZZ yyyy" format to java.sql.Date
This article describes how to convert a timestamp in the "EEE MMM dd HH:mm:ss ZZZ yyyy" format into the "YYYY-MM-DD" format acceptable to the MySQL database. Here’s how:
SimpleDateFormat
class to parse the input timestamp. Ensure format strings and locales are precisely defined to avoid ambiguity. SimpleDateFormat
to convert the parsed date into the desired "YYYY-MM-DD" format. java.sql.Date
object from the formatted date. Optimization plan
A more concise and efficient way is to use the modern java.time
package:
<code class="language-java">LocalDate date4 = ZonedDateTime.parse(date, DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH)) .toLocalDate(); java.sql.Date date5 = java.sql.Date.valueOf(date4);</code>
This code uses the ZonedDateTime
and LocalDate
classes to parse and convert timestamps.
Suggestion:
Locale.ROOT
. LocalDate
objects without conversion. The above is the detailed content of How to Convert 'EEE MMM dd HH:mm:ss ZZZ yyyy' to a MySQL-Compatible java.sql.Date?. For more information, please follow other related articles on the PHP Chinese website!