Java format current date is incorrect

Source: Internet
Author: User
Tags java format

Today, the ability to upload files, and to record the current time as the upload time, but found in the Java New Date () and the time after the format problem, can not be stored in the database, for example:

Today is July 31, 2014, the time is 6:51 P.M. 42 seconds, but the output is 2014-51-31 06:51:42, first of all, the month is certainly wrong, and secondly, 6 o'clock in the afternoon best can be shown as 18 points.



Don't know where the problem is, check the format date identifier:

Dateutils.asstring (New Date (), "Yyyy-mm-dd Hh:mm:ss")
Then Baidu a bit, found the problem:

Month with the "MM" actually represents the minutes, not the month, the month to use "MM", and to use the 24-hour system is "HH", "HH" is a 12-hour system.

The specific rules are as follows:

[plain] view Plain copy time date identifier: yyyy: Year MM: Month DD: Day hh:1~12 Hour System (1-12) [plain] view plain copy HH:24-hour System (0-23) mm: Divided ss: sec S: Milliseconds E: Days of the week D: Day of the Year F: the first few weeks of the January (will divide the total number of days in this month by 7) W: The first few weeks of the year W: The Week of January (depending on the actual situation   To calculate) A: The last afternoon logo K: and HH Almost, said 24 hours a day system (1-24). K: And Hh<span style= "font-family:arial, Helvetica, Sans-serif;" > Almost </span><span style= "font-family:arial, Helvetica, Sans-serif;" That means 12 hours a day (0-11). </span> Z: Indicates time zone
Summary: The date operation is often used, but has not been very skilled grasp, the note is not enough attention.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.