I have a java
app that I developed.
There is a function to register items in it, and the registered date is also registered in the DB.
I registered on 2019/12/29, but when I looked at the data registered in the DB, the registered date was 2020/12/29.
I wondered why, and investigated the cause, so I will summarize it here as a reminder.
As a result, the registered date is registered as a character string type in the yyyy / MM / dd
format.
Therefore, the date type was converted to the character string type in the yyyy / MM / dd
format with Java
.
Use Java
's SimpleDateFormat
for conversion. By defining the year part with YYYY
, the year is treated as the base year of the calendar week </ b>, and 2019/12/29 is converted to 2020/12/29 and registered in the DB.
The base year of a calendar week is the idea that "the week to which 1/1 belongs is treated as the next year even if it is December." The expression is poor and difficult to understand. Excuse me.
It looks like this on a calendar December 29-31 is treated as 2020 because the week with a yellow background includes the day of 2020. That is.
By the way, the code looked like this.
henkan.java
//* Execution date is 2019/12/29
SimpleDateFormat sdf = new SimpleDAteFormat("YYYY/MM/dd");
Calender cal = Calender.getInstance();
String yyyymmdd = sdf.format(cal.getTime()); //2020 at this timing/12/It becomes 29.
To handle it normally, just change YYYY
to yyyy
.
That was a terrible mistake.
I did java
after a long time, so I made such a terrible mistake.
Until then, I was touching python
, and in the string conversion of the date of python
, % Y
is used for the year, so I think I confused it and specified the uppercase Y
.
You should be careful.
By the way, as for the code above, you can only get the current date and time with Date
.
It is said that it reveals its low coding ability. : dizzy_face:
Recommended Posts