Was done in the base year of the Java calendar week

Trigger

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.

result

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.

What is the base year of a calendar week?

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 sdf_001.png December 29-31 is treated as 2020 because the week with a yellow background includes the day of 2020. That is.

  • 12/28 is treated as 2019.

code

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.

at the end

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.

bonus

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

Was done in the base year of the Java calendar week
Get the result of POST in Java
[Java] Get the day of the specific day of the week
The story of writing Java in Emacs
Display Japanese calendar and days of the week using java8 standard class
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
Import files of the same hierarchy in Java
[Java] Calculate the day of the week from the date (Calendar class is not used)
Judgment of the calendar
Get the URL of the HTTP redirect destination in Java
In Time.strptime,% j (total date of the year) is
[Java] Get the file in the jar regardless of the environment
Change the storage quality of JPEG images in Java
Summarize the additional elements of the Optional class in Java 9
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
A quick explanation of the five types of static in Java
Count the number of digits after the decimal point in Java
Implementation of gzip in java
How to derive the last day of the month in Java
Implementation of tri-tree in Java
When I was worried about static methods in java interface, I arrived in the order of name interpretation
Examine the system information of AWS Lambda operating environment in Java
[Java1.8 +] Get the date of the next x day of the week with LocalDate
[Java] Get the dates of the past Monday and Sunday in order
My site was thrown in a week after joining the company
Output the difference between each field of two objects in Java
Find out the list of fonts available in AWS Lambda + Java
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Spring validation was important in the order of Form and BindingResult
Examine the list of timezone IDs available in the Java ZoneId class
Get the public URL of a private Flickr file in Java
Let's create a TODO application in Java 5 Switch the display of TODO
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
Access the network interface in Java
[Java] Delete the elements of List
Guess the character code in Java
[Java version] The story of serialization
Specify the java location in eclipse.ini
Unzip the zip file in Java
Mixed Western calendar output in Java
List of members added in Java 9
Parsing the COTOHA API in Java
List of types added in Java 9
Order of processing in the program
The origin of Java lambda expressions
Implementation of like function in Java
Call the super method in Java
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
The story of forgetting to close a file in Java and failing
[Payment of work] Summarize the mistakes made in 2020 [2nd year from inexperience]
The story of acquiring Java Silver in two months from completely inexperienced.
What wasn't fair use in the diversion of Java APIs on Android
This and that of the implementation of date judgment within the period in Java