SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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();
    calFrom.setTime(fromDate);
    GregorianCalendar.calTo = new GregorianCalendar();
    calTo.setTime(toDate);

    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.

    Greg
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by GHicks View Post
    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);
    That's going to the start of the previous year.

    Quote Originally Posted by GHicks View Post
    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?
    That could be a situation where the code has been updated without updating the comment.

    Quote Originally Posted by GHicks View Post
    And finally, what would the value of (calTo.get(Calendar.YEAR) * 100 + (calTo.get(Calendar.MONTH) + 1)) result in?
    It would result in 6-digit numeric value with the year followed by the month, for example: 201211 for November of 2012
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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).

    Greg
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •