• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Adding Months to Gregorian Calendar  RSS feed

 
k caster
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm attempting to do something seemingly simple, but can't seem to figure it out or find any answers through web searches. I am creating a large dataset of time series data to use in prototyping, so that I can get several thousand data points. I want to start at a time in the past, say Feb 1, 1956, and add a day on to that date. I've attempted this by doing the following:

GregorianCalendar firstDate = new GregorianCalendar(1956, Calendar.FEBRUARY, 1);
firstDate.setLenient(true);

for (int i = 0; i < size; i++)
{
double value = Math.random()*10000;

//Month month = new Month(firstDate.get(Calendar.MONTH), firstDate.get(Calendar.YEAR));
Day firstDay = new Day(firstDate.get(Calendar.DAY_OF_MONTH), firstDate.get(Calendar.MONTH), firstDate.get(Calendar.YEAR));
// timeSeries is an object that stores time series data
timeSeries.add(firstDay, value);

}

When I run the above code I get the following exception:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid 'day' argument.
at org.jfree.date.SpreadsheetDate.<init>(SpreadsheetDate.java:135)
at org.jfree.date.SerialDate.createInstance(SerialDate.java:795)
at org.jfree.data.time.Day.<init>(Day.java:118)
at JFreeChartWYPlot.createDataset(JFreeChartWYPlot.java:333)
at JFreeChartWYPlot.init(JFreeChartWYPlot.java:71)
at JFreeChartWYPlot.<init>(JFreeChartWYPlot.java:59)
at JFreeChartWYPlotApp.main(JFreeChartWYPlotApp.java:10)

If I just try to increment the month by one (firstDate.add(Calendar.MONTH, 1)), I can get to Nov 1, 1965 and then I get the following exception:

Exception in thread "main" java.lang.IllegalArgumentException: The 'month' argument must be in the range 1 to 12.
at org.jfree.date.SpreadsheetDate.<init>(SpreadsheetDate.java:126)
at org.jfree.date.SerialDate.createInstance(SerialDate.java:795)
at org.jfree.data.time.Day.<init>(Day.java:118)
at JFreeChartWYPlot.createDataset(JFreeChartWYPlot.java:333)
at JFreeChartWYPlot.init(JFreeChartWYPlot.java:71)
at JFreeChartWYPlot.<init>(JFreeChartWYPlot.java:59)
at JFreeChartWYPlotApp.main(JFreeChartWYPlotApp.java:10)


I did firstDate.setLenient(true) because I thought it would avoid this problem? How do I add dates without running into these exceptions??

Thanks in advance,
knc
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the error occurs in that Day class, not when you try to add a day or a month to the GregorianCalendar object. Perhaps it isn't programmed to understand that firstDate.get(Calendar.MONTH) returns a number between 0 and 11 to represent months between January and December. (As bizarre as that may seem...)
 
k caster
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas on how I can work around it?
 
k caster
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind - I figured it out the issue with the component that I'm using.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

With Calendar, you can do it like, following,

try{
Calendar calendar=Calendar.getInstance();
System.out.println(calendar.getTime());
calendar.add(Calendar.MONTH,2);
System.out.println(calendar.getTime());
calendar.add(Calendar.YEAR,-2);
System.out.println(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH,2);
System.out.println(calendar.getTime());

}catch(Exception e)
{
System.out.println(e);
}


The output is :
-------------------


Thu Aug 31 13:58:14 GMT+05:30 2006
Tue Oct 31 13:58:14 GMT+05:30 2006
Sun Oct 31 13:58:14 GMT+05:30 2004
Tue Nov 02 13:58:14 GMT+05:30 2004



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!