Calendar date arithmetic

Posted on 2009-05-02
Last Modified: 2013-12-29
I am doing some date arithmetic and calendar attribute setting but its not working as documented.
The code below gets a calendar, subtracts a month then its supposed to
set the last day for that month.  According to the java doc add should immediately change the value set and any dependent values in the calendar.  But when I do this on 5/2/09, I can see the the values in the calendar are month=3 and day_of_month=31. so when I call getTime I get 5/1/09.

what am I doing wrong ?

Calendar now = GregorianCalendar.getInstance();


    now.add(Calendar.MONTH, -1);

    now.set(Calendar.DAY_OF_MONTH, now.getMaximum(Calendar.DAY_OF_MONTH));

    period = now.getTime();

Open in new window

Question by:buckrodger
    LVL 17

    Accepted Solution

    The line

    now.set(Calendar.DAY_OF_MONTH, now.getMaximum(Calendar.DAY_OF_MONTH));

    does not function as your want because of the getMaximum is designed to return the maximum day for any month and not this month specifically.

    You should be using getActualMaximum(Calendar.DAY_OF_MONTH)

    Author Comment

    The documentation of that is unclear, what is the difference ???


    Author Closing Comment

    thank you
    LVL 17

    Expert Comment

    "getMaximum" returns the largest possible value that "get" can return. So for a month, that is always "31", even if the calendar happens to be February. "getActualMaximum" returns the largest possible value for the field that this particular calendar time can have. The way "getActualMaximum" works is by temporarily incrementing the field until it reaches the max. So "getActualMaximum" returns the actual number of days in the month of that calendar.
    LVL 92

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now