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:
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!