• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

first date and last date of month

 
siraj baig
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
January is (i believe) 0, not 1.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So true. That's why you should use Calendar.JANUARY etc instead.
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All,

I should have gone through src/api before posting.
 
Sudharshan Reddy Ch
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explained in the below post.
Get first date and last date in current month

 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the correction Rob!!
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic