Results 1 to 3 of 3
  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default Calendar Date Range

    Hi All,
    I am trying to get the last month date range (1st to end of month). I have following code:

    Java Code:
    public static void main(String[] args) {
            Calendar todaysDate = Calendar.getInstance();
            Integer month = todaysDate.get(Calendar.MONTH);
            Integer days = todaysDate.get(Calendar.DAY_OF_MONTH);
            Integer year = todaysDate.get(Calendar.YEAR);
            if (month == 0) {
                year--;
                month = 12;
            }
            Calendar lastMonthDate = Calendar.getInstance();
            lastMonthDate.set(year, month, 15); //set 15 to simplyfy 30/31 check. 
            Integer lastDay = lastMonthDate.getMaximum(Calendar.DAY_OF_MONTH);
            
            Calendar startDate = Calendar.getInstance();
            Calendar endDate = Calendar.getInstance();
            
            startDate.set(year, month, 1);
            endDate.set(year, month, lastDay);        
    
            System.out.println("Date Range : " + startDate.get(Calendar.MONTH)
                    + "-"
                    + startDate.get(Calendar.DATE)
                    + "-"
                    + startDate.get(Calendar.YEAR)
                    + " to "
                    + endDate.get(Calendar.MONTH)
                    + "-"
                    + endDate.get(Calendar.DATE)
                    + "-"
                    + endDate.get(Calendar.YEAR));
        }
    I am expecting to get print result as
    Date Range : 10-1-2012 to 10-31-2012
    but I am getting
    Date Range : 10-1-2012 to 11-1-2012

    What I am doing wrong?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Calendar Date Range

    your expectation is already wrong!
    the month is 0 based, so october is 9 and not 10 ! (Calendar (Java Platform SE 6))

    and so you have to use
    startDate.set(year, month-1, 1);
    endDate.set(year, month-1, lastDay);

    and
    (startDate.get(Calendar.MONTH)+1)
    (endDate.get(Calendar.MONTH)+1)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Calendar Date Range

    Or just use the constants that come with Calendar.
    That's what they're there for.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Manually setting a date using Calendar and Date
    By ravl13 in forum New To Java
    Replies: 3
    Last Post: 10-25-2011, 11:12 PM
  2. check the sunday and saturday from a given date range
    By harsha.udupa2008 in forum New To Java
    Replies: 5
    Last Post: 07-20-2010, 08:22 AM
  3. jField date range
    By JavaTrouble in forum New To Java
    Replies: 5
    Last Post: 05-25-2010, 04:40 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. Calendar.DATE
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-04-2008, 07:51 PM

Tags for this Thread

Posting Permissions

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