• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to find monday and sunday when i give a date  RSS feed

 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to fine a monday and sunday of a week if a relavant date is given?
Ex : if i give 2014-Jan-17 is the date i need to find the monday and sunday of relavant week.Can we do this using Java?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The java.util.Calendar class has everything you need for that.
 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ulf
 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i write my own code.its working fine.but when the month changing its wrong.please help.


 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you had used a Calendar object for those calculations, it would have taken care of the case where the month changes. It isn't that hard to do, either... just about every line of code there can be replaced by a similar line acting on a Calendar object.
 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be good to read this if you're able to use Java 8. Catching up with the Java Date and Time API
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1 on using the Calendar class like I suggested. It would also take care of DST, which your code doesn't. Why did you try to reinvent the wheel?
 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While the previous posters are absolutely correct about using the Calendar class to do that for you, might I suggest instead of using the built-in Calendar class maybe take a look at a third party calendar library called Joda Time. I have personally found it more useful and easier to use than Calendar class for many situations.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!