DEV Community

sainiankitsaini
sainiankitsaini

Posted on

Bug that can cost you Millions of Dollars!!!

ISO 8601

ISO 8601
There is a practical international standard for date-time handling, ISO 8601.
The ISO 8601 definition of a week is:
Weeks start on a Monday
Week # 1 has the first Thursday of the year
A year consists of either 52 or 53 weeks.
A year may have one or more days from the previous calendar year, and also from the following calendar year.

Types of Year

There are 2 types of Year in java which are as follows:

  1. Week Based Year
  2. Era Based Year

Week based year is in sync with WEEK_OF_YEAR cycle but era based year is the normal calendar year.

How to get Date in java

Method 1. Using Era Based Year (Using small alphabet 'y' for year)
Date date = new SimpleDateFormat("yyyy/MM/dd").parse("2019/12/31");
Method 2. Using Week based Year (Using capital alpabet 'Y' for year)
Date date = new SimpleDateFormat("YYYY/MM/dd").parse("2019/12/31");

Issue in different date getting methods

For all the weeks of a year any method of calculating date(Era/Week based) will work fine. Issue occurs only during the 1st week and last week of year.
Example: January 1, 2020 is a Wednesday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 5 (ISO 8601 standard compatible setting), then week 1 of 2020 starts on December 30, 2019, and ends on January 5, 2020. The week year is 2020 for the last two days of calendar year 2019. If, however, getFirstDayOfWeek() is SUNDAY, then week 1 of 2020 starts on January 5, 2020, and ends on January 11, 2020; the first four days of 2020 then are part of week 53 of 2019 and their week year is 2019.

Understanding Issue through code

using Era based year('y) for getting date but Week Based Year ('Y') for formatting date
Date date = new SimpleDateFormat("yyyy/MM/dd").parse("2019/12/31");
String result = new SimpleDateFormat("YYYY/MM/dd").format(date);   

//result '2020/12/31' which is unexpected

using Era based year('y) for getting date and Era based year('y) for formatting date
Date date = new SimpleDateFormat("yyyy/MM/dd").parse("2019/12/31");
String result = new SimpleDateFormat("yyyy/MM/dd").format(date);   

//result '2019/12/31' as expected

All combinations of Week based and Era based year
package codechef;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateMethodsExplore {

  public static void main(String[] args) throws ParseException {
    Date date1 = new SimpleDateFormat("yyyy/MM/dd").parse("2019/12/31");
    String result1 = new SimpleDateFormat("yyyy/MM/dd").format(date1);
    System.out.println("date 1 =>" + result1);
    System.out.println("**************");

    Date date2 = new SimpleDateFormat("YYYY/MM/dd").parse("2019/12/31");
    String result2 = new SimpleDateFormat("YYYY/MM/dd").format(date2);
    System.out.println("date 2 =>" + result1);
    System.out.println("**************");

    Date date3 = new SimpleDateFormat("yyyy/MM/dd").parse("2019/12/31");
    String result3 = new SimpleDateFormat("YYYY/MM/dd").format(date3);
    System.out.println("date 3 =>" + result3);
    System.out.println("**************");

    Date date4 = new SimpleDateFormat("YYYY/MM/dd").parse("2019/12/31");
    String result4 = new SimpleDateFormat("yyyy/MM/dd").format(date4);
    System.out.println("date 4 =>" + result4);
    System.out.println("**************");
  }
}
//output
date 1 =>2019/12/31
**************
date 2 =>2019/12/31
**************
date 3 =>2020/12/31
**************
date 4 =>2018/12/30
**************

Why Week Based year is Made

There are many usecases like if we want to see weekly stocks rate of a company during a financial year (For India: 1 april to next year 31 march) then week based can be used to plot a graph in order to better display the statistics.

Conclusion

It actually depends upon the usecase which year you want to use but in the formatting both methods should not be mixed.

Top comments (0)