# Year Error in Date Calculation

I was printing dates along the x axis of a graph, and saw a year error. I’ve reduced the program to a minimum demo of the problem. It’s supposed to print the date for each day. The number of days in error varies from year to year. What’s going wrong?

Output:

25-Dec-2001
26-Dec-2001
27-Dec-2001
28-Dec-2001
29-Dec-2001
30-Dec-2001
31-Dec-2002 <— 2002 ???
01-Jan-2002
02-Jan-2002
03-Jan-2002
04-Jan-2002
05-Jan-2002

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

SimpleDateFormat date_fmt_date = new SimpleDateFormat("dd-MMM-YYYY");

{ // from internet somewhere
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getTime();
}

void list_dates()
{
int ix;
Date dDate;
String sDate;

dDate = new Date("25-Dec-2001");
for (ix = 0; ix < 12; ix++)
{
sDate = date_fmt_date.format(dDate);
print(sDate + " ");
println();
}
}

void setup()
{
list_dates();
exit();
}
void draw(){}
1 Like

I wonder if the extra day for leap year is a factor?

I’ve modified the program to print a few days around each new year. The number of days in error varies with a sort of cycle over a few years. Could be 8 years +/- which would be leap-year * 2.

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

SimpleDateFormat date_fmt_date = new SimpleDateFormat("dd-MMM-YYYY");

{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getTime();
}

void list_dates()
{
int ix;
Date dDate;
String sDate;
int iYr;
String sYear;

for (iYr = 0; iYr < 22; iYr++)
{
sYear = String.format("20%02d", iYr);

dDate = new Date("28-Dec-" + sYear);
for (ix = 0; ix < 8; ix++)
{
sDate = date_fmt_date.format(dDate);
print(String.format("%2d %2d  ", iYr, ix));
print(sDate + " ");
println();
}
println();
}
}

void setup()
{
list_dates();
exit();
}
void draw(){}

0 0 28-Dec-2000
0 1 29-Dec-2000
0 2 30-Dec-2000
0 3 31-Dec-2000
0 4 01-Jan-2001 Good
0 5 02-Jan-2001
0 6 03-Jan-2001
0 7 04-Jan-2001

1 0 28-Dec-2001
1 1 29-Dec-2001
1 2 30-Dec-2001
1 3 31-Dec-2002 X
1 4 01-Jan-2002
1 5 02-Jan-2002
1 6 03-Jan-2002
1 7 04-Jan-2002

2 0 28-Dec-2002
2 1 29-Dec-2002
2 2 30-Dec-2003 X
2 3 31-Dec-2003 X
2 4 01-Jan-2003
2 5 02-Jan-2003
2 6 03-Jan-2003
2 7 04-Jan-2003

3 0 28-Dec-2003
3 1 29-Dec-2004 X
3 2 30-Dec-2004 X
3 3 31-Dec-2004 X
3 4 01-Jan-2004
3 5 02-Jan-2004
3 6 03-Jan-2004
3 7 04-Jan-2004

4 0 28-Dec-2004
4 1 29-Dec-2004
4 2 30-Dec-2004
4 3 31-Dec-2004
4 4 01-Jan-2004 X
4 5 02-Jan-2004 X
4 6 03-Jan-2005
4 7 04-Jan-2005

5 0 28-Dec-2005
5 1 29-Dec-2005
5 2 30-Dec-2005
5 3 31-Dec-2005
5 4 01-Jan-2005 X
5 5 02-Jan-2006
5 6 03-Jan-2006
5 7 04-Jan-2006

6 0 28-Dec-2006
6 1 29-Dec-2006
6 2 30-Dec-2006
6 3 31-Dec-2006
6 4 01-Jan-2007 Good
6 5 02-Jan-2007
6 6 03-Jan-2007
6 7 04-Jan-2007

7 0 28-Dec-2007
7 1 29-Dec-2007
7 2 30-Dec-2007
7 3 31-Dec-2008 X
7 4 01-Jan-2008
7 5 02-Jan-2008
7 6 03-Jan-2008
7 7 04-Jan-2008

8 0 28-Dec-2008
8 1 29-Dec-2009 X
8 2 30-Dec-2009 X
8 3 31-Dec-2009 X
8 4 01-Jan-2009
8 5 02-Jan-2009
8 6 03-Jan-2009
8 7 04-Jan-2009

9 0 28-Dec-2009
9 1 29-Dec-2009
9 2 30-Dec-2009
9 3 31-Dec-2009
9 4 01-Jan-2009 X
9 5 02-Jan-2009 X
9 6 03-Jan-2009 X
9 7 04-Jan-2010

10 0 28-Dec-2010
10 1 29-Dec-2010
10 2 30-Dec-2010
10 3 31-Dec-2010
10 4 01-Jan-2010 X
10 5 02-Jan-2010 X
10 6 03-Jan-2011
10 7 04-Jan-2011

11 0 28-Dec-2011
11 1 29-Dec-2011
11 2 30-Dec-2011
11 3 31-Dec-2011
11 4 01-Jan-2011 X
11 5 02-Jan-2012
11 6 03-Jan-2012
11 7 04-Jan-2012

12 0 28-Dec-2012
12 1 29-Dec-2012
12 2 30-Dec-2012
12 3 31-Dec-2013 X
12 4 01-Jan-2013
12 5 02-Jan-2013
12 6 03-Jan-2013
12 7 04-Jan-2013

13 0 28-Dec-2013
13 1 29-Dec-2013
13 2 30-Dec-2014 X
13 3 31-Dec-2014 X
13 4 01-Jan-2014
13 5 02-Jan-2014
13 6 03-Jan-2014
13 7 04-Jan-2014

14 0 28-Dec-2014
14 1 29-Dec-2015 X
14 2 30-Dec-2015 X
14 3 31-Dec-2015 X
14 4 01-Jan-2015
14 5 02-Jan-2015
14 6 03-Jan-2015
14 7 04-Jan-2015

15 0 28-Dec-2015
15 1 29-Dec-2015
15 2 30-Dec-2015
15 3 31-Dec-2015
15 4 01-Jan-2015 X
15 5 02-Jan-2015 X
15 6 03-Jan-2015 X
15 7 04-Jan-2016

16 0 28-Dec-2016
16 1 29-Dec-2016
16 2 30-Dec-2016
16 3 31-Dec-2016
16 4 01-Jan-2016 X
16 5 02-Jan-2017
16 6 03-Jan-2017
16 7 04-Jan-2017

17 0 28-Dec-2017
17 1 29-Dec-2017
17 2 30-Dec-2017
17 3 31-Dec-2017
17 4 01-Jan-2018 Good
17 5 02-Jan-2018
17 6 03-Jan-2018
17 7 04-Jan-2018

18 0 28-Dec-2018
18 1 29-Dec-2018
18 2 30-Dec-2018
18 3 31-Dec-2019 X
18 4 01-Jan-2019
18 5 02-Jan-2019
18 6 03-Jan-2019
18 7 04-Jan-2019

19 0 28-Dec-2019
19 1 29-Dec-2019
19 2 30-Dec-2020 X
19 3 31-Dec-2020 X
19 4 01-Jan-2020
19 5 02-Jan-2020
19 6 03-Jan-2020
19 7 04-Jan-2020

20 0 28-Dec-2020
20 1 29-Dec-2020
20 2 30-Dec-2020
20 3 31-Dec-2020
20 4 01-Jan-2020 X
20 5 02-Jan-2020 X
20 6 03-Jan-2020 x
20 7 04-Jan-2021

21 0 28-Dec-2021
21 1 29-Dec-2021
21 2 30-Dec-2021
21 3 31-Dec-2021
21 4 01-Jan-2021 X
21 5 02-Jan-2021 X
21 6 03-Jan-2022
21 7 04-Jan-2022

Hello,

My quick look at this:

//SimpleDateFormat date_fmt_date = new SimpleDateFormat("dd-MMM-yyyy");

for (iYr = 1990; iYr < 2022; iYr++)
{
sYear = str(iYr);

dDate = new Date("28-Dec-" + sYear);
for (ix = 0; ix < 8; ix++)
{
sDate = date_fmt_date.format(dDate);
print(String.format("%2d %2d  ", iYr, ix));
print(sDate + " ");
println();
}
println();
}

I set the year explicitly and not by adding 20 to the counter.
It seemed to work.

I got some insight from here and changed the format and simplified it a bit:

:)

1 Like

@glv Thanks for trying this. I knew the code for the year was crude, but that’s not the problem. Clearly it runs correctly on your PC, so what’s the difference? - yyyy is correct and YYYY is different. Explained here YYYY vs yyyy . Thanks again.

1 Like