| Author |
first date and last date of month
|
siraj baig
Ranch Hand
Joined: Jul 11, 2006
Posts: 42
|
|
hi, i have a string date String date = "06/26/2006" how can i get the first date and last date of this date. Thanks.
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3673
|
|
You can use the Calender class to do this. you will get the output - 30/04/2008 ------> here, put the year and (month-1) to get the last date of the month. Regards [ July 08, 2008: Message edited by: Vijitha Kumara ]
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
subodh gupta
Ranch Hand
Joined: Jul 23, 2007
Posts: 203
|
|
|
the code you have posted is returning the last day of the next month e.g. if i am passing the Januay date it gives the last date of the Feb
|
http://subodh-gupta.blogspot.com
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10040
|
|
|
January is (i believe) 0, not 1.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
So true. That's why you should use Calendar.JANUARY etc instead.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
subodh gupta
Ranch Hand
Joined: Jul 23, 2007
Posts: 203
|
|
Thanks All, I should have gone through src/api before posting.
|
 |
Sudharshan Reddy Ch
Ranch Hand
Joined: Jul 12, 2006
Posts: 36
|
|
Explained in the below post.
Get first date and last date in current month
|
Cheers,
Sudha C Reddi
http://techelp.in
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
No offense, but I have to say that class is a mess. First of all, you use a lot of formatting + parsing to get rid of the day, just to set it afterwards. Second, your getLastDay method does not take into account daylight savings time, when one day can have 23 hours instead of 24. Thirdly, your if-statement in the last method is just nasty. A switch would have made more sense:
That said, all your methods could have been written using only Date and Calendar; no formatting and re-parsing, just the final formatting:
Not only is it shorter (1 line each for the first two methods, and don't get me started on the last one), it is also more robust (the daylight savings time thingy, Calendar has proven itself*) and easier to understand.
* A lot of people recommend using Joda time over Calendar. I feel that their motivations are more based on usability than Calendar not working.
|
 |
Sudharshan Reddy Ch
Ranch Hand
Joined: Jul 12, 2006
Posts: 36
|
|
|
Thanks for the correction Rob!!
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
You're welcome
And don't be hard done by my comments. I've written code a lot worse than that even during the latter stages of getting my degree The thing is, you learn by practice and experience, and from corrections by others. I still learn new methods and classes in the API quite regularly.
|
 |
 |
|
|
subject: first date and last date of month
|
|
|