aspose file tools*
The moose likes Beginning Java and the fly likes Java Calendar Dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Calendar Dates " Watch "Java Calendar Dates " New topic
Author

Java Calendar Dates

Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
Hi all,

I am currently building a payroll software code and stuck at a point, I would do my best to explain and get any help

I have a pay period Start date and End Date this can be any period

When I enter the period as (in mm/dd/yyyy format)

Start date "04/08/2010"
End Date "05/08/2010"

then I add 1 month to a calendar handler.add(Calendar.Month,1)

the next and the corresponding dates are

Start date "05/08/2010"
End Date "06/08/2010"

Start date "06/08/2010"
End Date "07/08/2010"

corresponding as desired. But the moment I enter the start date and end date as
as

Start date "04/01/2010"
End Date "04/30/2010" which is a 1 Month period and then add 1 Month to the code
the corresponding dates are

Start date "05/01/2010"
End Date "05/30/2010"

Start date "06/01/2010"
End Date "06/30/2010" [ Even though it has 31 days in June, then from Feb 2011 its like


Start date 01/01/2011
End Date 01/30/2011

Start date 02/01/2011
End date 02/28/2011

Start date 03/01/2011
Start date 03/28/2011

everything ends on the 28th

Can any one let me know why in my first condition the 31st day was being taken care of and in the next condition I am not getting the same output.




Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40


As you have seen, in the first case, adding a month will get you to the same day of the month in the following month.

As you have seen, in the second case, if the same day doesn't exist in the following month, the class is smart enough to subtract until it is a valid day in the following month.

There is no special logic that detects the end of month, and understands that you want the end of the following month. For that, you will have to do it yourself.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
Hi Henry,

thanks for the reply, this did help, So that means that when I add Calendar.MONTH,1) the class actually does not add the actual days ? Its calculating 1 MONTH based on some different logic ?

Well then could you please suggest me a way by which I could add the number of days based on the Month the user has selected.. I am not asking for the code just the logic would help

thanks !
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40


Take a look at the get(), getActualMaximum(), and set() methods. With them, you can get the day in month, get the last day in month, determine if day is last. And if so, get the last day in following month, and set it to that day.

Henry
Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
thanks henry, will let you know if incase of any issues.
Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
Thanks Henry,

Hurray, this did work I am happy

thank you !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Calendar Dates