• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

first date and last date of month

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
January is (i believe) 0, not 1.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All,

I should have gone through src/api before posting.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explained in the below post.
Get first date and last date in current month

 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the correction Rob!!
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic