Convert "EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
This article describes how to convert a date in "EEE MMM dd HH:mm:ss ZZZ yyyy" format to "YYYY-MM-DD" format so that it can be inserted into a MySQL database.
Using Java 8 Date/Time API (recommended method):
Java 8’s date/time API provides a more concise and clear method:
<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>
Advantages of Java 8 Date/Time API:
Use SimpleDateFormat (alternative method):
If you use SimpleDateFormat, be sure to specify the correct date/time format and use the correct three-letter time zone abbreviation ("zzz" for the three-letter time zone name). The correct form of SimpleDateFormat is:
<code class="language-java">SimpleDateFormat formatnow = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH); SimpleDateFormat formatneeded = new SimpleDateFormat("yyyy-MM-dd");</code>
Other notes:
The above is the detailed content of How to Convert 'EEE MMM dd HH:mm:ss ZZZ yyyy' to java.sql.Date?. For more information, please follow other related articles on the PHP Chinese website!