Home > Database > Mysql Tutorial > Why Does SimpleDateFormat.parse Return a Different Format Than Specified?

Why Does SimpleDateFormat.parse Return a Different Format Than Specified?

Susan Sarandon
Release: 2024-10-27 16:10:02
Original
1017 people have browsed it

 Why Does SimpleDateFormat.parse Return a Different Format Than Specified?

SimpleDateFormatter.parse Giving Output in Different Format than Specified

This question arises when trying to convert a UNIX timestamp into a date with a specific format using SimpleDateFormat. Despite setting the desired format, the sdf.parse result appears in a different format.

Solution:

To resolve this issue, it is recommended to avoid passing dates as strings to a MySQL database. Instead, it's safer and more concise to pass date objects, particularly instances from the Java date and time API (java.time). A LocalDateTime object is suitable in this case:

For database storage, LocalDateTime represents datetime in the format '2018-05-23T15:30'.

To persist the LocalDateTime in a MySQL database:

Alternative for UTC Values:

If the database values should be stored in UTC (Universal Time Coordinated), use the following conversion:

Advantages of Using java.time:

  • Relieves formatting concerns
  • Provides a modern and comprehensive date and time API

The above is the detailed content of Why Does SimpleDateFormat.parse Return a Different Format Than Specified?. 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