Home > Database > Mysql Tutorial > How to Convert 'EEE MMM dd HH:mm:ss ZZZ yyyy' to java.sql.Date?

How to Convert 'EEE MMM dd HH:mm:ss ZZZ yyyy' to java.sql.Date?

Linda Hamilton
Release: 2025-01-12 07:57:41
Original
146 people have browsed it

How to Convert

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>
Copy after login

Advantages of Java 8 Date/Time API:

  • The code is concise and smooth
  • The structure is clearer
  • Handle time zone automatically (provided your database driver supports LocalDate)

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>
Copy after login

Other notes:

  • Specify a locale for SimpleDateFormat to avoid parsing issues on computers with non-English locales.
  • Use the full time zone ID or UTC offset rather than the ambiguous three-letter time zone abbreviation.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template