Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Java Calendar Dates

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Anil Karamchandan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks henry, will let you know if incase of any issues.
 
Anil Karamchandan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry,

Hurray, this did work I am happy

thank you !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic