I heard that the programmer who used yyyy MM DD worked overtime to change bugs a few days ago


Uncle, I’ve been in Beijing for more than ten years, but I haven’t got a license plate in Beijing. Every week, I need to apply for the “Beijing entry permit” through an app. When I apply for the last Beijing entry permit in 19 years, the app gives such a prompt:

Date display: “2020-12-31”!

A group of passengers make complaints about it. Although everyone has guessed it, it should be APP’s Bug, but it is inevitable to have to talk about it.

So what’s the reason for this bug? In fact, it is very simple to write yyyy MM DD as yyyy mm DD.

If programmers who are not so familiar with time processing see here, what is the difference between yyyy and yyyy? Knock it in the code, and their results are the same!

public class DateTest {
 public static void main(String[] args) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(2019, Calendar.AUGUST, 31);
  Date strDate = calendar.getTime();
  DateFormat formatUpperCase = new SimpleDateFormat("yyyy-MM-dd");
  System.out.println("2019-08-31 to yyyy-MM-dd: " + formatUpperCase.format(strDate));
  formatUpperCase = new SimpleDateFormat("YYYY-MM-dd");
  System.out.println("2019-08-31 to YYYY-MM-dd: " + formatUpperCase.format(strDate));

The running results are as follows:

2019-08-31 to yyyy-MM-dd: 2019-08-31
2019-08-31 to YYYY-MM-dd: 2019-08-31

But what if we change the date to December 31, 2019 and try again? The results were different

2019-12-31 to yyyy-MM-dd: 2019-12-31
2019-12-31 to YYYY-MM-dd: 2020-12-31

So what is the reason for this problem? It’s very simpleY and Y actually mean different things

  • y: Year of era; serious year;
  • Y: Week based year; as long as this week crosses the year, then this week is counted as the next year;

This is a small detail in the development process, and it falls into the pit carelessly.

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.