DateAxis previousStandardDate bug

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
uvoigt
Posts: 168
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

DateAxis previousStandardDate bug

Post by uvoigt » Tue Aug 23, 2016 1:01 pm

Hi David,

I've found a bug in DateAxis.previousStandardDate(Date, DateTickUnit). It returns wrong values in case the date given as parameter is already the standard date and the tick unit has a count > 1.

Testcase (DateAxis.previousStandardDate must be made public for this case):

Code: Select all

	
        public static void main(String[] args)
	{
		Locale.setDefault(Locale.ENGLISH);

		DateAxis da = new DateAxis();
		DateFormat fDateShortTime = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
		DateTickUnit tickUnit =
			new DateTickUnit(DateTickUnitType.MINUTE, 30, DateTickUnitType.MINUTE, 5, fDateShortTime);

		Date d = new Date(116,3,21,10,00,00);
		for (int m = 0; m < 60; m++)
		{
			d.setMinutes(m);
			Date dnext = da.previousStandardDate(d, tickUnit);
			System.out.println("Input: "+fDateShortTime.format(d) + "  Output: " + fDateShortTime.format(dnext));
		}
	}
You will see that all standard dates returns a "non" standard date:
Input: 2016-04-21 10:00 Output: 2016-04-21 09:59
Input: 2016-04-21 10:01 Output: 2016-04-21 10:00
Input: 2016-04-21 10:02 Output: 2016-04-21 10:00
Input: 2016-04-21 10:03 Output: 2016-04-21 10:00
...
Input: 2016-04-21 10:30 Output: 2016-04-21 10:29
Input: 2016-04-21 10:31 Output: 2016-04-21 10:30
Input: 2016-04-21 10:32 Output: 2016-04-21 10:30
Input: 2016-04-21 10:33 Output: 2016-04-21 10:30
...

There is an easy fix:
In DateAxis.previousStandardDate all occurrences of:

Code: Select all

                if (dd.getTime() >= date.getTime()) {
                    calendar.set(Calendar.SECOND, value - 1);
                    dd = calendar.getTime();
                }
must be replaced by

Code: Select all

                if (dd.getTime() >= date.getTime()) {
                    calendar.set(unit.getCalendarField(), value - count);
                    dd = calendar.getTime();
                }
Note that Calendar.SECOND has to be replace by the unit time field.

I don't have a GitHub account by hand. Maybe you can apply that yourself?
Thanks!!
Uli

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: DateAxis previousStandardDate bug

Post by david.gilbert » Wed Aug 24, 2016 7:26 am

Thanks for the report. I wrote a JUnit test to cover all the cases and will soon commit this and the fix.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

uvoigt
Posts: 168
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

Re: DateAxis previousStandardDate bug

Post by uvoigt » Wed Aug 24, 2016 7:48 am

Thanks!!

Locked