Calendar Function Question

I am rewriting an app that was previously writtein in Java/Javascript and I am unclear exactly what the following is doing/returning/setting

This section I understand, create new instances of the calendar and set the values from previous values defined in app (fromDate and toDate)
GregorianCalendar calFrom = new GregorianCalendar();
GregorianCalendar.calTo = new GregorianCalendar();

With that said, what would the following snippets of code return so I can determine the proper way to generate them using PHP
calTo.set(Calendar.DAY_OF_MONTH, 1);
calTo.add(Calendar.MONTH, 1);
calTo.add(Calendar.DAY_OF_YEAR, -1);

Another section has this:
// Make sure at least 12 months of data
GregorianCalendar yearBefore = (GregorianCalendar)calTo.clone();
yearBefore.set(Calendar.DAY_OF_MONTH, 1);
yearBefore.add(Calendar.MONTH, -5);
if (yearBefore,getTime().compareTo(calFrom.getTime()) < 0) {
calFrom = yearBefore;
} else {
calFrom.set(Calendar.DAY_OF_MONTH, 1);

Basically what I’m mostly confused about is when he listed that at least 12 months of data was needed, why does it only use MONTH, -5?

And finally, what would the value of (calTo.get(Calendar.YEAR) * 100 + (calTo.get(Calendar.MONTH) + 1)) result in?

Let me know if I need to explain any of this further.


  1. That code isn’t javaScript - it could be Java

  2. To be able to answer your questions we’d need to see the code defining GregorianCalendar() as all of your code is calling methods of that object.

Yes, it is Java mostly (I mentioned at the beginning of my post that it was a combination of Java/Javascript). From my Googling, it appears that GregorianCalendar is a class that just extends the capabilities of the default Calendar class (based on the Gregorian calendar versus Julian or any others)

I am pretty sure I “get” most of what is going on, just looking for a bit of clarification such as in the section where he lists needing 12 months minimum of dates range, but the modifier only take -5 months back?

That’s going to the start of the previous year.

That could be a situation where the code has been updated without updating the comment.

It would result in 6-digit numeric value with the year followed by the month, for example: 201211 for November of 2012

Perfect! That’s what I was curious about. And now that you mention the code changing without the comment, I do recall awhile back that the end user determined that 12 months of data was a little too far back for numbers to be accurate (it’s used as parameter to calculate the end of the current month projections and going back too many months caused a skew in values more so than the end user wanted. So you are probably exactly right on that topic.

Thank you for the feedback, now back to scratching my head over more of this code and figuring out what else he did (used several javabeans and so I am having to deconstruct a lot of things and figure out the rewrite in PHP. It’s really taxing my PHP skills, but good for the learning potential (and thankfully they aren’t on a strict timeline to get this project done).