SimpleDateFormat time format conversion pit

SimpleDateFormat time format conversion pit


Recently, the project encountered problems in a SimpleDateFormat time format conversion, similar to the code:

SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
            String dateStr = "2019-10-27";
            //前端传值
            Date date = format.parse(dateStr);

            SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
            //传给另外一方
            System.out.println("date:"+format2.format(date));

Export

date:20190127

In January a month to become? ? ? Debug look
Here Insert Picture Description
really is January, then we pass the last of which the last 10 months? A closer look at the code there to see Debug result, found that the format of the original formatting settings are not the same twice, the first time SimpleDateFormat format is yyyy-mm-dd month format set into mm , mm is minutes, and then observe our results ( pictured above) it is indeed 10 minutes position, equivalent to the value of the month does not pass, SimpleDateFormat to the default of January, and now the truth is out, really low stool stumbling dead ah, so we must be careful coding, to do the code review.
Example letters and their meanings date / time format

letter meaning Examples
Y years Generally represented by two year yy, yyyy represents the year 4; year yy is used, for example 11; using yyyy represents the year, such as 2011
M month MM is the month for general use, if you use MMM, the month will be displayed in different languages ​​using MM is the month, such as 05 according to the locale; using MMM for the month, in Locale.CHINA locales, such as the "October"; in Locale under .US locales, such as Oct
d The number of days of the month Generally expressed dd dd days indicated using days, such as 10
D The number of days in a year Said on that day is the first few days of the year, the number of days used in the year D represented by D, such as 295
E Day of the week Represented by E based on different locales, different language use E represents the day of the week day of the week, in Locale.CHINA locale, such as "Thursday"; in Locale.US locales, such as Thu
H The number of hours of the day (24 hour) HH represents hours generally by using HH represents hours, such as 18
h The number of hours of the day (12 hour) Hh is generally used hours of use hh denotes hours, such as 10 (note that there may be a 10-point 10, it may be 22 points)
m The number of minutes Usually the number of minutes used mm mm represents the number of minutes used, such as 29
s The number of seconds Ss represents seconds using generally used ss is the number of seconds, such as 38
S The number of milliseconds It represents the number of milliseconds generally used SSS SSS represented using the number of milliseconds, such as 156

Codeword is not easy, hope to help others -

Released five original articles · won praise 0 · Views 86

Guess you like

Origin blog.csdn.net/clsq0913/article/details/102769860